Naing Lin Aung Naing Lin Aung - 6 months ago 47
Swift Question

EXC_BREAKPOINT (code=EXC_i386_BPT, subcode=0x0)

I was trying to insert to CoreData after retrieving from API. But when I tried it, It show as

EXC_BREAKPOINT (code=EXC_i386_BPT, subcode=0x0)
. In Log, it's only show as
(lldb)
. I don't think it's not related with outlet coz I didn't set any special outlet aside from default ones during my writing. I also tried to solve other one solutions but none of it work on me. Please help a way to solve it.

Screenshot

enter image description here

Code

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
}
}

Answer

You are trying to cast to the wrong type. The result of the insert will be an NSManagedObject (or your subclass of NSManagedObject), not NSManagedObjectContext. So, if your subclass is BlogItem change the insert as follows:

newBlogItem = NSEntityDescription.insertNewObjectForEntityForName("BlogItem", inManagedObjectContext: context) as BlogItem

If you haven't subclassed, just use NSManagedObject at the end of the line, in place of BlogItem.

Comments