下面斯威夫特2例給出了這樣的錯誤:如何在Swift 3中將圖像從文檔目錄顯示到UIImageView?
Value of type String has no member 'stringByAppendingPathComponent'
我需要做什麼來改變斯威夫特3?
下面斯威夫特2例給出了這樣的錯誤:如何在Swift 3中將圖像從文檔目錄顯示到UIImageView?
Value of type String has no member 'stringByAppendingPathComponent'
我需要做什麼來改變斯威夫特3?
蘋果公司試圖將每個人從路徑作爲字符串範例轉移到URL(即file:///path/to/file.text
)。 Swift API幾乎刪除了所有path
,轉而使用URL
。
你仍然可以找到它在Objective-C(NSString
):
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let getImagePath = NSString.path(withComponents: [paths, "fileName"])
更SWIFTY方式:
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: paths).appendingPathComponent("fileName")
我個人喜歡把自己從App代表這個價值。把這段代碼(像普通函數一樣)放到AppDelegate.swift中。
lazy var applicationDocumentsDirectory: URL = {
let urls = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask)
return urls[urls.count-1]
}()
所以在所有的文件,你可以使用這種方式:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let imageUrl = appDelegate.applicationDocumentsDirectory.appendingPathComponent("YourFileName")
let imageUrlString = imageUrl.urlString //if String is needed
輸入您的問題體內的錯誤,而不是鏈接的屏幕截圖嘗試在UIImageView的地方 – Shades