无法将类型 CKAsset 的值分配给类型 UIImage
Cannot assign value of type CKAsset to type UIImage
将我的 table 单元格的图像设置为我的 "Picture" 时,我遇到了此代码中的错误。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "foodcell") as! FoodTableCell
let restaurant: CKRecord = restaurantArrayList[indexPath.row]
cell.name?.text = restaurant.value(forKey: "Name") as? String
cell.picture?.image = restaurant.value(forKey: "Picture") as? CKAsset
//the error is here ^
return cell
我如何才能成功地将我的 "Picture" 密钥设置为我的单元格中的图像?
CKAsset
不是图像格式,而更像是指向资产的 URL 的包装器。
假设您的 restaurant
记录在键 "Picture" 下确实有一项资产:
let asset = resturaunt["picture"] as! CKAsset
// 'resturaunt["picture"]' is just another way of writing 'restaurant.value(forKey: "Picture")'
let data = try! Data(contentsOf: asset.fileURL)
// this contains the binary data from the asset url
let image = UIImage(date: data)
// now make an image with that binary data
上面有很多强制解包选项,你可能想要采取的更安全的方法是这样的:
if let asset = resturaunt["picture"] as? CKAsset,
let data = try? Data(contentsOf: asset.fileURL) {
cell.picture?.image = UIImage(date: data)
}
使用try?
Data(contentsOf: asset.fileURL)
可能会引发您需要从中恢复的错误,因此您需要使用 try
关键字并管理它可能引发的可能错误。
Swift 3 添加了 do-try-catch
错误处理语法,采用这种一般形式:
do {
// statements that might throw an error need to be called within here
try callSomeMethodThatMightThrow()
let value = try callMethodReturnValueMightThrow()
}
catch {
// statements in this block will get called if an error was encountered
}
try?
是一种 shorthand 处理方式,它避免了 do-catch 语法。当你使用它时,你是在说,"if an error occurs, then assign the value to nil
"
这里有更详细的post,更详细地解释了try
、try?
以及try!
https://cocoacasts.com/error-handling-in-swift-with-the-try-keyword/
的使用
将我的 table 单元格的图像设置为我的 "Picture" 时,我遇到了此代码中的错误。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "foodcell") as! FoodTableCell
let restaurant: CKRecord = restaurantArrayList[indexPath.row]
cell.name?.text = restaurant.value(forKey: "Name") as? String
cell.picture?.image = restaurant.value(forKey: "Picture") as? CKAsset
//the error is here ^
return cell
我如何才能成功地将我的 "Picture" 密钥设置为我的单元格中的图像?
CKAsset
不是图像格式,而更像是指向资产的 URL 的包装器。
假设您的 restaurant
记录在键 "Picture" 下确实有一项资产:
let asset = resturaunt["picture"] as! CKAsset
// 'resturaunt["picture"]' is just another way of writing 'restaurant.value(forKey: "Picture")'
let data = try! Data(contentsOf: asset.fileURL)
// this contains the binary data from the asset url
let image = UIImage(date: data)
// now make an image with that binary data
上面有很多强制解包选项,你可能想要采取的更安全的方法是这样的:
if let asset = resturaunt["picture"] as? CKAsset,
let data = try? Data(contentsOf: asset.fileURL) {
cell.picture?.image = UIImage(date: data)
}
使用try?
Data(contentsOf: asset.fileURL)
可能会引发您需要从中恢复的错误,因此您需要使用 try
关键字并管理它可能引发的可能错误。
Swift 3 添加了 do-try-catch
错误处理语法,采用这种一般形式:
do {
// statements that might throw an error need to be called within here
try callSomeMethodThatMightThrow()
let value = try callMethodReturnValueMightThrow()
}
catch {
// statements in this block will get called if an error was encountered
}
try?
是一种 shorthand 处理方式,它避免了 do-catch 语法。当你使用它时,你是在说,"if an error occurs, then assign the value to nil
"
这里有更详细的post,更详细地解释了try
、try?
以及try!
https://cocoacasts.com/error-handling-in-swift-with-the-try-keyword/