Why do I get the error "Ambiguous reference to member 'subscript'"? - arrays

The following code produces the compiler error:
Ambiguous reference to member 'subscript'
if let library = json as? [String: Any] {
if let books = library["books"] as? Array { // On this line
}
}

You need specify the type of your Array object.
if let books = dic["books"] as? Array<Dictionary<String, Any>> { //Or as? Array<Any>
//Or
if let books = dic["books"] as? [[String: Any]] { //Or as? [Any]

Related

Cannot subscript a value of type '[AnyHashable : Any]' with an index of type 'Any?'

I'm trying to convert an objective c function to swift which is.
+(NSString *)extractNameWithDictionary:(NSDictionary *)dictionary
{
NSDictionary *assets = dictionary[#"assets"];
NSDictionary *item = dictionary[#"item"];
NSArray *facilityAssets = item[#"assets"];
NSDictionary *facilities = facilityAssets[0];
NSDictionary *asset = assets[facilities[#"content"]];
return asset[scs_name] ? : #"";
}
To
func extractNameWithDictionary(dictionary: [AnyHashable : Any]) -> String {
guard let assets = dictionary["assets"] as? [AnyHashable : Any],
let item = dictionary["item"] as? [AnyHashable : Any],
let facilityAssets = item["assets"] as? [Any],
let facilities = facilityAssets[0] as? [AnyHashable : Any],
let asset = assets[facilities["content"]] else { return "" }
return asset[scs_name] ?? ""
}
but in the last constant: let asset = assets[facilities["content"]] I'm getting:
Cannot subscript a value of type '[AnyHashable : Any]' with an index of type 'Any?'
Any help with this?
You can try
func extractNameWithDictionary(dictionary: [String : Any]) -> String {
guard let assets = dictionary["assets"] as? [String : Any],
let item = dictionary["item"] as? [String : Any],
let facilityAssets = item["assets"] as? [Any],
let facilities = facilityAssets[0] as? [String : Any],
let asset = assets[facilities["content"] as? String ?? "" ] as? [String:Any] else { return "" }
return asset[scs_name] as? String ?? ""
}
This
facilities["content"]
returns Any that you can't use in dictionary named assets as it's key is of type AnyHashable
The simplest approach is to just continue your system of unloading to include the content step:
...
let facilities = facilityAssets[0] as? [AnyHashable : Any],
let content = facilities["content"] as? String,
let asset = assets[content] as? [AnyHashable: String]
else { return "" }
You might move on from there to tighten up you types a bit:
func extractNameWithDictionary(dictionary: [String : Any]) -> String {
guard let assets = dictionary["assets"] as? [String : Any],
let item = dictionary["item"] as? [String : Any],
let facilityAssets = item["assets"] as? [[String : Any]],
let content = facilityAssets.first?["content"] as? String,
let asset = assets[content] as? [String: String],
let name = asset[scs_name]
else { return "" }
return name
}
As you work more on this project, however, you'll want to get rid of that NSDictionary entirely, and replace it with a proper struct/class that has typed properties rather than having to do all of this as? madness. That's only showing up because of the use of a dictionary rather than a struct or class.

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?
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 { ... }

Swift Array sort with JSON

Data:
tried get data from JSON
has code with Swift run on Playground
var roadWayStatusArray = [AnyObject]()
let url = URL(string: "http://od.moi.gov.tw/data/api/pbs")
let data = try? Data(contentsOf: url!)
let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
if let results = json as? [String : AnyObject] {
if let result = results["result"] as? [[String : AnyObject]] {
for data in result {
let happendate = data["happendate"] as? String
roadWayStatusArray.append(data as AnyObject!)
}
}
}
I has tried using roadWayStatusArray.sort(by: >) but Xcode report me Ambiguous reference member '>'
How to create sort by hapendate or happentime
Your roadWayStatusArray is an array of AnyObject. There is no > operator defined for AnyObject.
The objects in roadWayStatusArray are actually dictionaries that look like this:
{
UID = "10602090007-0";
areaNm = "\U4e2d\U5c71\U9ad8\U901f\U516c\U8def-\U570b\U9053\Uff11\U865f";
comment = "\U5317\U4e0a.\U4e2d\U58e2\U670d\U52d9\U5340 \U51fa\U53e3\U531d\U9053\U4e2d \U53f3\U5074 \U5c0f\U5ba2\U8eca\U505c\U653e\Uff0c\U99d5\U99db\U7591\U4f3c\U7761\U8457\U4e86";
direction = "\U5317\U4e0a";
happendate = "2017-02-09";
happentime = "01:08:00.0000000";
modDttm = "2017-02-09 01:15:43.603";
region = N;
road = "";
roadtype = "\U5176\U4ed6";
srcdetail = "\U71b1\U5fc3\U807d\U773e";
x1 = "121.73558";
y1 = "25.12263";
}
You need to call sort(by:) with a closure that determines the sort order. For example, if you want to sort by happendate and then happentime:
roadWayStatusArray.sort(by: { (lhsAny, rhsAny) -> Bool in
let lhs = lhsAny as? [String: AnyObject]
let rhs = lhsAny as? [String: AnyObject]
let lhsKey = (lhs?["happendate"] as? String ?? "", lhs?["happentime"] as? String ?? "")
let rhsKey = (rhs?["happendate"] as? String ?? "", rhs?["happentime"] as? String ?? "")
return lhsKey < rhsKey
})

How to access nested JSON in brackets in Swift?

I have a JSON response which is in brackets and I struggle to access the inner fields e.g. display_name with Swift. How can I do that?
Optional(["result": {
user = {
"display_name" = "Max Test";
email = "test.max#gmail.com";
"fb_id" = 10209982554704497;
roles = (
stu
);
schools = "<null>";
};
}])
The code I used to access the JSON:
self.restApi.getProfileDetails() {responseObject, error in
//parse down the first layer of array
let response = responseObject as? [String:AnyObject]
print("response object when MyDetailsController opened")
print(response)
let result = response!["result"] as? [AnyObject]
print("result object")
print(result)
//parse down the second layer of JSON object
if let result = response!["result"] as? [AnyObject] {
print("result object when MyDetailsController opened")
// work with the content of "result", for example:
if let user = result[0] as? [String:AnyObject]{
print(user)
let displayName = user["display_name"]
print("displayName")
print(displayName)
}
}
It seems that I address the result in the wrong way as it is always nil:
The console output:
result object
nil
In response result is dictionary not array, try to get like this
let result = response!["result"] as? [String:AnyObject]
1-Get the result object from response
2-Get the user object from result object
3-Get the user info as String from user object .
let response = responseObject as? [String: AnyObject]
let result = response!["result"] as? [String: AnyObject]
if let user = result!["user"] as? [String: AnyObject] {
let displayName = user["display_name"] as? String
let email = user["email"] as? String
}

How can I get data from JSON array in SWIFT?

from this link I would like to get the commonName
I tried this but it didn't work?!
let commonName = object["toLocationDisambiguation"][0]["disambiguationOptions"][1]["place"][2]["commonName"].stringValue
Option 1 (Normal)
if let toLocationDisambiguation = object["toLocationDisambiguation"] as? Dictionary<String, AnyObject> {
if let disambiguationOptions = toLocationDisambiguation["disambiguationOptions"] as? Array<AnyObject> {
if let first = disambiguationOptions.first as? [String: AnyObject] {
if let place = first["place"] as? [String: AnyObject] {
let commonName = place["commonName"] as! String
print("Common Name: ", commonName)
}
}
}
}
Option 2 (Type Aliases)
typealias MyDictionary = [String: AnyObject]
typealias MyArray = [MyDictionary]
if let toLocationDisambiguation = object["toLocationDisambiguation"] as? MyDictionary {
if let disambiguationOptions = toLocationDisambiguation["disambiguationOptions"] as? MyArray {
if let first = disambiguationOptions.first {
if let place = first["place"] as? MyDictionary {
let commonName = place["commonName"] as! String
print("Common Name: ", commonName)
}
}
}
}
Option 3 (SwiftyJSON for Objective-C like syntax)
Take a look at SwiftyJSON.
let object = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, AnyObject>
let json = JSON(object)
let commonName = json["toLocationDisambiguation"]["disambiguationOptions"][0]["place"]["commonName"].stringValue
print("Common Name: ", commonName)

Resources