MJ000111
MJ000111

Reputation: 29

Cannot convert the expression's type '()' 'NilLiteralConverible'?

the following code build error:

var err:NSError?
_persistentStore = _persistentStoreCoordinator!.addPersistentStoreWithType(
            NSSQLiteStoreType,
            configuration: nil,
            URL: _coreDataPathURL,
            options: nil,
            error: err
    )

build error:Cannot convert the expression's type '()' 'NilLiteralConverible' ?

_persistentStoreCoordinator is a optional var.

I don't understand why build error?!

Upvotes: 0

Views: 366

Answers (1)

Airspeed Velocity
Airspeed Velocity

Reputation: 40965

The problem is the error type needs to be an NSErrorPointer, i.e. a pointer to an NSError?.

To implicitly convert an NSError? to one of those, you can just stick a & in front of err:

var err:NSError?
_persistentStore = _persistentStoreCoordinator!.addPersistentStoreWithType(
            NSSQLiteStoreType,
            configuration: nil,
            URL: _coreDataPathURL,
            options: nil,
            error: &err // <--
    )

(see the docs for more info)

The build error is because Swift is attempting to use NSError’s NilLiteralConvertible initializer, but failing because that needs a type of () as its argument (beware, when Swift can’t make any of the possible overloads work, it often gives you a compiler error about one specific possibility, which can be misleading).

Upvotes: 1

Related Questions