0

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.

4

2 回答 2

2

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

应该读

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

于 2018-06-14T12:03:35.580 回答
0

错误2:

URL没有任何构造函数使用context:. 尝试init(fileURLWithPath:)改用(它需要一个字符串,因此您需要使 filePath 成为字符串的实例而不是 NSManagedObject)。

在此处查看有关 Apple 网址的官方文档。

编辑

当您返回一个自定义 URL 对象(NSManagedObject 的子类)时,您需要更改函数的返回类型。

-> Foundation.URL-> URL。我建议将您的自定义 URL 子类重命名为其他名称,因为此名称已被 Apple 使用,并且可能会导致一些命名空间问题(编译器会感到困惑并且您会遇到错误)。

于 2018-06-14T12:09:48.807 回答