iosswiftswift2nspersistentstore

Value of type 'NSPersistentStore' can never be nil, comparison isn't allowed


let managedModel:NSManagedObjectModel = NSManagedObjectModel.mergedModelFromBundles(nil)!


        var storeCoordinator:NSPersistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedModel)

        if storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) != nil{
            if error != nil{
                print(error!.localizedDescription)
                abort()
            }
        }

How i can fix it in latest swift2 syntax?
Thanks

Value of type 'NSPersistentStore' can never be nil, comparison isn't allowed
Call can throw, but it is not marked with 'try' and the error is not handled

enter image description here


Solution

  • This is how i handle it with Swift 2 :

    let managedModel:NSManagedObjectModel = NSManagedObjectModel.mergedModelFromBundles(nil)!
    var storeCoordinator:NSPersistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedModel)
    
    do {
        try storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
    } catch let error as NSError {
        print(error!.localizedDescription)
        abort()
    }