SWIFT - Unable to infer complex closure return type; add explicit type to disambiguate

got following error with this code:



Code:

lazyvar documentDir = URL {
        let allUrls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return allUrls[0]
    }()



Fehler:

Unable to infer complex closure return type; add explicit type to disambiguate

Insert ' () -> URL in ' FIX



When I'm clicking on Fix another error occurs:

Cannot invoke initializer for type 'URL' with an argument list of type '(() -> URL)'



How to solve this Problem? I'm using a CoreData Project

Answered by OOPer in 350487022

I guess you may want to do something like this:

    lazy var documentDir: URL = {
        let allUrls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return allUrls[0]
    }()


`URL {...}()` does not make sense as an expression.

Accepted Answer

I guess you may want to do something like this:

    lazy var documentDir: URL = {
        let allUrls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return allUrls[0]
    }()


`URL {...}()` does not make sense as an expression.

SWIFT - Unable to infer complex closure return type; add explicit type to disambiguate
 
 
Q