ryanwaite28
ryanwaite28

Reputation: 2050

ios swfit - assign list of AnyObject to dictionary key

so i want to assign a list of AnyObject to a key of a dictionary with the structure [String:AnyObject]

here is my code:

let list = try JSONSerialization.jsonObject(with: newData, options: .allowFragments) as! [AnyObject]
                let parsedResult = [String:AnyObject]()
                parsedResult["dataList"] = list
                callback(false, parsedResult)

im parsing data from an http request. i get this error:

Cannot assign value of type [AnyObject] to type AnyObject?

isn't a list of AnyObject, still an object?

Upvotes: 0

Views: 134

Answers (1)

Rashwan L
Rashwan L

Reputation: 38833

Change:

let parsedResult = [String:AnyObject]()

To:

var parsedResult: [String: Any] = [ : ]

And by the way declare parsedResult as var to be able to do parsedResult["dataList"] = list.

For your other issue that you placed a comment on, do this instead:

if let account = data?["account"] as? [String: Any], let accountKey = account["key"] as? Int { ... } 

Upvotes: 1

Related Questions