1

I have an array result

["https://woo.demoapp.xyz/wp-content/uploads/2017/10/sunglasses.jpg"]

i try to convert to string url in tableview and show image with SDWebImage

    let dic = productArray[indexPath.row] as! NSDictionary
    let image = dic.object(forKey: "images") as! NSArray
    let imageUrl = image.value(forKey: "src") as! NSArray as! [String]

    cell.pinImage.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "pin1"))
2
  • 1
    It's recommended in Swift 3 to avoid NSStuff, like NSDictionary, NSArray when there is the Swift Equivalent available. What the result you gave ?imageUrl? If that's the case URL(string: imageUrl) will clearly fail, because imageUrl is said to be as! NSArray as! [String], not a String. URL(string: imageUrl.first)? But your parsing seems wrong (there isn't one, try to use custom class/struct). It should be corrected. Commented Oct 27, 2017 at 8:47
  • What is the issue that you are facing? You only told about your requirement not the problem. Commented Oct 27, 2017 at 9:31

1 Answer 1

2

If you have getting array then you used below code.

In Swift objectAtIndex: 0 looks like this:

let srcImage:NSDictionary = image[0] as! NSDictionary
let strImageUrl:NSString = srcImage.value(forKey: "src") as! NSString

Updated:

let srcImage = image[0] as! [String: Any]
let strImageUrl = srcImage["src"] as! String
Sign up to request clarification or add additional context in comments.

3 Comments

Do not use NSDictionary in Swift. Do not use NSString in Swift. Do not annotate types the compiler can infer in Swift. Do not use valueForKey at all in this case.
@vadia in question used NSDictionary, NSString then i use it.
This is no reason to adopt bad habits. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.