Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object != nil works in Beta 5 but not in Beta 6?

Tags:

swift

I left an project unattended for months, only last week installed Beta 5, it worked after fixing a ton of 'nil' errors in AppDelegate.swift and today tried Beta 6, 'nil' errors again?

func saveContext () {
    var error: NSError? = nil
    let managedObjectContext = self.managedObjectContext
    if managedObjectContext  != nil {
        if managedObjectContext.hasChanges && !managedObjectContext.save(&error) {

            abort()
        }
    }
}

var managedObjectContext: NSManagedObjectContext {
    if _managedObjectContext == nil {
        let coordinator = self.persistentStoreCoordinator
        if coordinator != nil {
            _managedObjectContext = NSManagedObjectContext()
            _managedObjectContext!.persistentStoreCoordinator = coordinator
        }
    }
    return _managedObjectContext!
}

The errors says, specifically at this line

if managedObjectContext  != nil {

'Cannot invoke '!=' with an argument list of type (NSManagedObjectContext, NilLiteralConvertable)'

and

if coordinator != nil {

'Cannot invoke '!=' with an argument list of type (NSPersistantStoreCoordinator, NilLiteralConvertable)'

It worked in Beta 5, so did I miss anything in the patch notes?

like image 777
Chow Jun Liang Avatar asked Feb 13 '23 04:02

Chow Jun Liang


1 Answers

This is due to a few changes in Xcode 6 beta 6 (see the release notes).

In particular:

Non-optional types may no longer be compared to nil, and ImplicitlyUnwrappedOptional no longer conforms to BooleanType.

Your managedObjectContext variable is of non-optional type NSManagedObjectContext. It cannot be nil and so the check was useless and is now forbidden.

I don't know about the return type of your self.persistentStoreCoordinator, but most likely it's also a non-optional.

The solution is simple: remove the if checks. They aren't needed.

BTW, also of interest is this change, which is slightly related:

A large number of Foundation APIs have been audited for optional conformance, removing a significant number of implicitly unwrapped optionals from their interfaces. This clarifies the nullability of their properties and arguments / return values of their methods. This is an ongoing effort since beta 5.

like image 192
DarkDust Avatar answered Mar 17 '23 17:03

DarkDust