【发布时间】:2017-10-19 03:52:42
【问题描述】:
我无法匹配 Core Data 书签 ID 和 JSON Web API ID,我无法将图像从 JSON API 保存到 Core Data。
主类
import UIKit
import MapKit
import CoreLocation
import SDWebImage
import CoreData
class InfoViewController: UIViewController , MKMapViewDelegate,CLLocationManagerDelegate{
var get_details : schools?
var bookmark : [Bookmark] = []
var bookmark_details : Bookmark?
override func viewDidLoad() {
super.viewDidLoad()
}
func bookmarktoCoredata(){
let alert = UIAlertController(title: "Add BookMark", message: "", preferredStyle: .alert)
let add = UIAlertAction(title: "Add", style: .default){
(action) in
// for bkms in self.bookmark
// {
// if ((bkms.name)) == ((self.get_details?.name)!){
//
// print("same name")
//
// }else{
// print("not same")
// }
// }
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let task = Bookmark(context: context)
if (task.bookmark_id) == Int16((self.get_details?.schoolid)!){
print("same id")
}
else
{
print("not same id")
task.bookmark_id = Int16((self.get_details?.schoolid)!)
task.name = self.get_details?.name
task.address = self.get_details?.address
task.district = self.get_details?.district
task.country = self.get_details?.country
task.phone = self.get_details?.phone
task.email = self.get_details?.email
task.website = self.get_details?.website
task.institution = self.get_details?.institution_type
task.establishment = self.get_details?.establishment_date
task.admission_open = self.get_details?.admission_open_from
task.admission_end = self.get_details?.admission_open_from
task.latitude = (self.get_details?.latitude)!
task.longitude = (self.get_details?.longitude)!
task.bookmark_type = "school"
// let img = UIImage(named: "App.png") //Sending direct image its working
let img = UIImage(named: (self.get_details?.logo)!) //by Json it sendong nil
let imgdata = UIImageJPEGRepresentation(img!, 1)
task.logo = imgdata! as NSData
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel){
(alert) in
// JLToast.makeText("Your Bookmark Cancel !!").show()
}
alert.addAction(add)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)
}
我不知道为什么我的代码是错误的。
【问题讨论】: