<\/script>')

Swift: Updated and got error: "cannot be called"! = 'With a list argument like .. "

Everything was fine in Xcode beta 5, but now in full-fledged Xcode I get 2 similar errors in my application dedet:

  • "cannot invoke '!=' with argument list of type '(NSManagedObjectContext, NilLiteralConvertible')"

  • "cannot invoke '!=' with argument list of type '(NSPersistentStoreCoordinator, NilLiteralConvertible')"

I tried replacing! = With! == and I get another error. I do not understand what the problem is! =. Code:

func saveContext () {
        var error: NSError? = nil
        let managedObjectContext = self.managedObjectContext
        if managedObjectContext != nil { //THIS LINE
            if managedObjectContext.hasChanges && !managedObjectContext.save(&error) {
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate.
                //println("Unresolved error \(error), \(error.userInfo)")
                abort()
            }
        }
    }

    // #pragma mark - Core Data stack

    // Returns the managed object context for the application.
    // If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
    var managedObjectContext: NSManagedObjectContext {
        if _managedObjectContext !== nil {
            let coordinator = self.persistentStoreCoordinator
            if coordinator != nil { //THIS LINE
                _managedObjectContext = NSManagedObjectContext()
                _managedObjectContext!.persistentStoreCoordinator = coordinator
            }
        }
        return _managedObjectContext!
    }
    var _managedObjectContext: NSManagedObjectContext? = nil

      

I have a new error: "'NSManagedObjectContext?' does not have a member named 'hasChanges'"

In this code:

func saveContext () {
        var error: NSError? = nil
        let managedObjectContext = self.managedObjectContext
        if managedObjectContext != nil {
            if managedObjectContext.hasChanges && !managedObjectContext.save(&error) { //This line
                // Replace this implementation with code to handle the error appropriately.
                // abort() causes the application to generate a crash log and terminate.
                //println("Unresolved error \(error), \(error.userInfo)")
                abort()
            }
        }
    }

      

+3


source to share


2 answers


The reason for this is that NSManagedObjectContext and probably NSPersistentStoreCoordinator are not Equatable . To use ==

and !=

, an object must conform to this protocol.

===

and !==

do not check equality in that sense. These checks check if two objects are actually the same object. Not with the same values. They both point to the same object in memory.

You can check this on the two marked types because they are objects. This should work well for your situation.



About your second problem, your code should look like this:

func saveContext () {
    var error: NSError? = nil
    let managedObjectContext = self.managedObjectContext
    if let moc = managedObjectContext {
        if moc.hasChanges && !moc.save(&error) { //This line
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate.
            //println("Unresolved error \(error), \(error.userInfo)")
            abort()
        }
    }
}

      

The difference here if let moc = managedObjectContext

: if managedObjectContext

not nil, the value is stored in a constant moc

. Otherwise, everything inside is not executed. Read more about options here .

+3


source


self.managedObjectContext

is introduced here as NSManagedObjectContext

rather than NSManagedObjectContext?

.



You cannot compare var to nil unless it is of type Optional<>

, implements, Equatable

or zero itself.

+1


source







All Articles