Naing Lin Aung Naing Lin Aung - 2 years ago 216
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
. 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.


enter image description here


override func viewDidLoad() {

var appDel :AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var context:NSManagedObjectContext = appDel.managedObjectContext!

let urlString = ""
let url = NSURL(string: urlString)

let session = NSURLSession.sharedSession()

let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
} 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")



if let split = self.splitViewController {
let controllers = split.viewControllers
self.detailViewController = controllers[controllers.count-1].topViewController as? DetailViewController

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download