MattSaedi
(Matt Saedi)
1
I have👇
Type 'Any' has no subscript members
error in 
self.imageList = jsonDictionary["imageList"] as! [String]
line
this is my all code 

let session = URLSession.shared
let url = URL(string: "http://onlinegallery.local/")
let task = session.dataTask(with: url!) { (data, response, error) in
do{
let jsonDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
self.imageList = jsonDictionary["imageList"] as! [String]
}catch{
print("Error")
}
}
task.resume()
You need to do:
let jsonDictionary = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any]
eskimo
(Quinn “The Eskimo!”)
3
Alternatively you could model the JSON as a Codable structure and use the JSONDecoder.
import Foundation
struct Gallery: Codable {
var imageList: [String]
}
func test(data: Data) throws -> [String] {
let gallery = try JSONDecoder().decode(Gallery.self, from: data)
return gallery.imageList
}
// A simple test for the above.
let json = """
{
"imageList": [
"Hello.png",
"Cruel.png",
"World.png"
]
}
"""
let jsonData = json.data(using: .utf8)!
print(try! test(data: jsonData))
// -> ["Hello.png", "Cruel.png", "World.png"]
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple