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

送分小仙女□ 提交于 2020-01-06 04:33:34

问题


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.


回答1:


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

should read

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




回答2:


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).



来源:https://stackoverflow.com/questions/50856991/cannot-convert-value-of-type-string-to-specified-type-nsmanagedobjectcontext-wh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!