I'm trying to get to the url
of thumbnail_images
from the JSON response I got as shown in this picture below:
So in order to reach to the
url
I made a class called Posts
in which
class Posts: NSObject {
var title: String?
var excerpt: String?
var content: String?
var thumbnail_images: [ThumbImages] = []
init(dict: [String: AnyObject])
{
super.init()
self.title = dict["title"] as? String
self.excerpt = dict["excerpt"] as? String
self.content = dict["content"] as? String
if let postImage = dict["thumbnail_images"] as? [String: AnyObject] {
for img in postImage {
self.thumbnail_images.append(ThumbImages(dict: img))
}
}
}
}
On the line self.thumbnail_images.append(ThumbImages(dict: img))
I get this error:
Cannot convert value of type '(key: String, value: AnyObject)' to expected argument type '[String : AnyObject]'
That is it for my questions but I think it's relevant to share more code as in order to reach to url
I had to make to more classes ThumbImages
and MedLargeImage
. Which has the following code:
class ThumbImages: NSObject {
var medium_large: [MedLargeImage] = []
init(dict: [String: AnyObject]) {
super.init()
if let imgURL = dict["medium_large"] {
for iURL in imgURL as! [AnyObject] {
medium_large.append(MedLargeImage(dict: iURL as! [String : AnyObject]))
}
}
}
As you can see in the screenshot. So finally I'm in the room where I can get the URL:
class MedLargeImage: NSObject {
var imageUrl: String?
init(dict: [String: AnyObject]) {
super.init()
self.imageUrl = dict["url"] as? String
}
}
UPDATE:
If I change if let postImage
in Posts class to:
if let postImage = dict["thumbnail_images"] as? [[String: AnyObject]] {
for img in postImage {
self.thumbnail_images.append(ThumbImages(dict: img))
}
}
The error is gone but for the if condition never gets true.