无法将String类型的值转换为指定类型NSManagedObjectContext Swift 4

时间:2018-06-15 06:25:22

标签: ios swift swift2.3 swift3.2

我需要帮助。从Swift 2.3转换 - > 3.2我收到以下错误。我无法解决此错误。

错误: 无法将String类型的值转换为指定类型NSManagedObjectContext

以下是我的编码内容,我正面临一些问题。

class FileManager {
    class func directoryForPersistentStorage(_ directoryName: String) -> Foundation.URL {
        var urlToReturn: Foundation.URL!

        let libraryDirectory = (NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true) as [String]).first
        if let cLibraryDirectory = libraryDirectory {
            let expectedDirectoryPath = (cLibraryDirectory as NSString).appendingPathComponent(directoryName)
            let fileManager = Foundation.FileManager.default
            var isDirectory = ObjCBool(false)
            if (!fileManager.fileExists(atPath: expectedDirectoryPath, isDirectory: &isDirectory) || !isDirectory.boolValue) {
                do {
                    try fileManager.createDirectory(atPath: expectedDirectoryPath, withIntermediateDirectories: true, attributes: nil)
                } catch _ {
                }
            }
            urlToReturn = URL(context: expectedDirectoryPath) // Above mentioned #Error is Here

            if fileManager.fileExists(atPath: expectedDirectoryPath) {
                do {

                    try urlToReturn.setTemporaryResourceValue(true as AnyObject?, forKey: URLResourceKey.isExcludedFromBackupKey)

                } catch _ {
                }
            }
        }
        return urlToReturn
    }

    class func persistentFileURL(_ name: String, enclosingDirectoryName: String) -> Foundation.URL {
        let directoryURL = self.directoryForPersistentStorage(enclosingDirectoryName)
        let urlPath = directoryURL.path
        let filePath: String = (urlPath as NSString).appendingPathComponent(name)

        return URL(context: filePath) // Above mentioned #Error is Here
    }
}

错误1:无法将String类型的值转换为指定类型NSManagedObjectContext **

注意:URL是声明处理此问题的单独类:URL_Class& extension of URL Class

请帮帮我。我对iOS很新。无法理解这种类型的错误。

0 个答案:

没有答案