[英]Cannot subscript a value of type NSDictionary with an index of type String. While converting from Swift 2.3 -> 3.2
[英]Cannot convert value of type String to specified type NSManagedObjectContext, While converting from Swift 2.3 -> 3.2
我需要幫助。 從Swift 2.3-> 3.2轉換時,出現以下錯誤。 我無法解決此錯誤。
以下是我的編碼資料,我遇到了一些問題。
錯誤1:無法將字符串類型的值轉換為指定的類型NSManagedObjectContext **
錯誤2:無法將URL類型的返回表達式轉換為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.
}
注意:URL是聲明為處理此問題的單獨類: URL_Class
請幫我。 我是iOS的新手。 無法理解此類錯誤。
let filePath: NSManagedObjectContext = (urlPath as NSString).appendingPathComponent(name)
應該讀
let filePath: String = (urlPath as NSString).appendingPathComponent(name)
錯誤2:
URL
沒有使用context:
構造函數。 嘗試改用init(fileURLWithPath:)
(它需要一個字符串,因此您需要使filePath成為字符串的實例,而不是NSManagedObject)。
在此處查看有關Apple URL的官方文檔。
編輯
在返回自定義URL對象(NSManagedObject的子類)的過程中,您需要更改函數的返回類型。
從-> Foundation.URL
到-> URL
。 我建議將自定義URL子類重命名為其他名稱,因為Apple已經使用了該名稱,並且可能會引起一些名稱空間問題(編譯器會感到困惑,並且會出錯)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.