Malik
Malik

Reputation: 45

Compare String and Array in iOS

I have hired a iOS developer to create an app which will be backed by the REST API. Now I'm stuck with a problem with one output.

There are Public and Private Groups, if group is Private, the API will return following in json format:

privacy":{"value":"1"}, 

and if the group is Public, the API will return following in json format:

"privacy":[]

The iOS developer says that this output is incorrect while on the other hand API developer believe this is correct output. Can anyone please tell me is this output correct to be used in iOS app or it's not correct?

iOS Developer days he can't compare String and Array.

Upvotes: 0

Views: 157

Answers (3)

Kevin Mac
Kevin Mac

Reputation: 320

Yes, iOS developer can check response.
But there should be consistency in JSON response.
It is not correct way that one API give response in array and other in dictionary.
It should be either array or string that would be preferable for iOS developer.

Output should be:

{  
    "privacy":[{"value":1}]  
}

For validating JSON response you can use http://jsonlint.com/

Upvotes: 2

igraczech
igraczech

Reputation: 2447

The API is designed incorrectly, as it provides various data types for the privacy key (and no schema defines how this should behave). Once it's a dictionary, once it's an empty array. I'd suggest using an array in any case.

Private: privacy : [ {"value" : true} ]

Public: privacy : []

However, it's possible to concatenate array to string and then compare with string (using let stringRepresentation = ",".join(array))

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

Yes it's correct, given there is no such thing as incorrect with JSON, as there is no schema to conform to. As long as it's legal, it's OK.

The iOS developer can test the type of the "privacy" value after it's been deserialised:

id value = jsonDict[@"privacy"];
if ([value isKindOfClass:[NSDictionary class]]) {
    // Value is dictionary
    NSDictionary *dictValue = (NSDictionary *)value;
    NSString *number = dictValue[@"value"];   // This should be a number, not a string!
} else if ([value isKindOfClass:[NSArray class]]) {
    // Value is array

} else {
    // Value is illegal. Report error.
}

I will say that it should be:

{"value":1}

as 1 is a number, not a string.

Upvotes: 3

Related Questions