-1

I have a Json File and there is a item like this: "image":"https://xxxxxxxxxxx.jpg"

So I'm trying this:

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
}

And after Run this message display:

Could not cast value of type '__NSCFString' (0x1112ca090) to 'UIImage' (0x11222b800).

Thank You.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
user3623737
  • 9
  • 1
  • 5
  • `arrDict[indexPath.row] .valueForKey("image")` seems to be a `NSString` object. You need to download it: http://stackoverflow.com/questions/24231680/loading-downloading-image-from-url-on-swift – Larme Apr 13 '16 at 09:11
  • Stop using `valueForKey:` for simple getting a value for a dictionary key. The dedicated method is `objectForKey:` or key subscripting. – vadian Apr 13 '16 at 09:17

3 Answers3

2

problem is in this line let strImage : UIImage=arrDict[indexPath.row] .valueForKey("image") ..

I am sure that your arrDict[indexPath.row].valueForKey("image") returns String ... so you can not convert it directly to UIImage ..

if let strImage = arrDict[indexPath.row]["image"] as? String{
    // ... your strImage is String  ... 
     if let data = NSData(contentsOfURL: NSURL(string:strImage )) {
          cell.lbImage.image = UIImage(data: data)
     }        
}

Your first line of your question suggest that "image":"https://xxxxxxxxxxx.jpg" .. so get image from url by convert your Sting to NSURL and download image

You can use SDWebImage for display image from URL https://github.com/rs/SDWebImage or you can use NSData class with contentsOfURL

For more info check this link

Community
  • 1
  • 1
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
0

The issue is

let strImage : UIImage=arrDict[indexPath.row] .valueForKey("image") as! UIImage

You are trying to force cast a String to UIImage using as! UIImage you should only ever use ! when you are 100% certain.

instead replace it with

if let image = arrDict[indexPath.row] .valueForKey("image") as? UIImage {
    //Now you have a UIImage
}

But in your case it is not a UIImage its a String so

 if let imageString = arrDict[indexPath.row] .valueForKey("image") as? String {
        //Now you have a String
        // Now make a UIImage from URL and insert it.
    }
Sean Lintern
  • 3,103
  • 22
  • 28
  • Thank you. If you can help me because I had difficulty to make a UIImage from URL and insert... The code is to open in JsonFile or Json Url. – user3623737 Apr 13 '16 at 10:00
  • this is the example { "objects":[ { "image":"xxxxxxxxxxxxxxxx.jpg" }, { "image":"xxxxxxxxxxxxxxxx.jpg" } ] } – user3623737 Apr 13 '16 at 10:01
  • Hello. Could you please look at my last question in profile. I also have to set logo in tableVC cell from JSON. Can I solve my problem like you suggested? I tried this "if let imageString = data[indexPath.row] .valueForKey("logo") as? String" In my case data is [String:Id] array. – Abrcd18 Oct 28 '20 at 17:43
  • Looks like its all fixed now @Abrcd18 :D – Sean Lintern Oct 29 '20 at 09:59
0

The trouble was solved with the code and instructions of the friends. Thank you.

if let strImage = arrDict[indexPath.row]["image"] as? String{
// ... your strImage is String  ... 
 if let data = NSData(contentsOfURL: NSURL(string:strImage )) {
      cell.lbImage.image = UIImage(data: data)
 }        
}
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
user3623737
  • 9
  • 1
  • 5