我在从API检索后尝试插入CoreData。但是当我尝试它时,它显示为EXC_BREAKPOINT (code=EXC_i386_BPT, subcode=0x0)
。在日志中,它仅显示为(lldb)
。我不认为这与插座没关系,因为在写作过程中我没有设置任何特殊的插座。我也试图解决其他一个解决方案,但没有一个对我有用。请帮助解决它。
屏幕截图
代码
override func viewDidLoad() {
super.viewDidLoad()
var appDel :AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext!
let urlString = "https://public-api.wordpress.com/rest/v1/sites/mmotaku.net/posts/"
let url = NSURL(string: urlString)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
println(error)
} else {
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
let posts = jsonResult["posts"] as NSArray
var newBlogItem:NSManagedObjectContext
var items = [[String:String]()]
var item = [String:String]()
for var i = 0; i < posts.count; i++ {
newBlogItem = NSEntityDescription.insertNewObjectForEntityForName("BlogItem", inManagedObjectContext: context) as NSManagedObjectContext
var authorDict = posts[i]["author"] as NSDictionary
newBlogItem.setValue(authorDict["name"], forKey: "name")
newBlogItem.setValue(posts[i]["title"], forKey:"title")
newBlogItem.setValue(posts[i]["content"], forKey: "content")
newBlogItem.setValue(posts[i]["date"], forKey: "date")
context.save(nil)
println("completed")
}
}
})
task.resume()
if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController
}
}
答案 0 :(得分:0)
您正在尝试转换为错误的类型。插入的结果将是NSManagedObject
(或NSManagedObject的子类),而不是NSManagedObjectContext
。因此,如果您的子类是BlogItem
,请按如下所示更改插入:
newBlogItem = NSEntityDescription.insertNewObjectForEntityForName("BlogItem", inManagedObjectContext: context) as BlogItem
如果您还没有子类,只需在该行末尾使用NSManagedObject
代替BlogItem
。