【发布时间】:2016-04-14 13:22:35
【问题描述】:
我有一个 Json 文件,其中有一个类似这样的项目:“image”:“https://xxxxxxxxxxx.jpg”
所以我正在尝试这个:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell : TableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as! TableViewCell
let strName : NSString=arrDict[indexPath.row] .valueForKey("name") as! NSString
let strSubtitle : NSString=arrDict[indexPath.row] .valueForKey("subtitle") as! NSString
let strLocation : NSString=arrDict[indexPath.row] .valueForKey("location") as! NSString
let strStart : NSString=arrDict[indexPath.row] .valueForKey("start") as! NSString
// let strImage : NSString=arrDict[indexPath.row] .valueForKey("image") as! NSString
// let strImage : NSData=arrDict[indexPath.row] .valueForKey("image") as! NSData
let strImage : UIImage=arrDict[indexPath.row] .valueForKey("image") as! UIImage
cell.lbName.text=strName as String
cell.lbSubtitle.text=strSubtitle as String
cell.lbStart.text=strStart as String
cell.lbLocation.text=strLocation as String
cell.lbImage.image=strImage as UIImage
return cell as TableViewCell
}
在运行此消息后显示:
无法将“__NSCFString”(0x1112ca090) 类型的值转换为“UIImage”(0x11222b800)。
谢谢。
【问题讨论】:
-
arrDict[indexPath.row] .valueForKey("image")似乎是一个NSString对象。你需要下载它:stackoverflow.com/questions/24231680/… -
停止使用
valueForKey:来简单地获取字典键的值。专用方法是objectForKey:或键下标。