Cannot convert value of type String to specified type NSManagedObjectContext, While converting from Swift 2.3 -> 3.2

946 Views Asked by At

I need Help. While conversion from Swift 2.3 -> 3.2 I received below error. I'm not able to resolve this error.

Below is my coding stuff, where I'm facing some issues.

Error1 : Cannot convert value of type String to specified type NSManagedObjectContext**

Error2 : Cannot convert return expression of type URL to return type URL.

 class func persistentFileURL(_ name: String, enclosingDirectoryName: String) -> Foundation.URL {
        let directoryURL = self.directoryForPersistentStorage(enclosingDirectoryName)
        let urlPath = directoryURL.path
        let filePath: NSManagedObjectContext = (urlPath as NSString).appendingPathComponent(name) //Error1 : Cannot convert value of type String to specified type NSManagedObjectContext 
        
        return URL(context: filePath) // Error2 : Cannot convert return expression of type URL to return type URL.
    }

Note : URL is separate Class declared to handle this : URL_Class

Please help me. I'm very new to iOS. Not able to understand this type of error.

2

There are 2 best solutions below

1
lostInTransit On BEST ANSWER

let filePath: NSManagedObjectContext = (urlPath as NSString).appendingPathComponent(name)

should read

let filePath: String = (urlPath as NSString).appendingPathComponent(name)

4
oyvindhauge On

Error 2:

URL doesn't have any constructor using context:. Try to use init(fileURLWithPath:) instead (which expects a string, so you need to make filePath an instance of string instead of an NSManagedObject).

See official docs on URL from Apple here.

EDIT

Seeing as you are returning a custom URL object (subclass of NSManagedObject), you need to change the return type of your function.

From -> Foundation.URL to -> URL. I'd suggest to rename your custom URL subclass to something else, since this name is already used by Apple and will probably cause some namespace issues (compiler will get confused and you will get errors).