Handle either a string or int in JSON response - ios

If catData is a dictionary returned from a rest API call:
NSString* catId = catData[#"id"];
This is fine if catData[#"id"] is a string. Not so much if it's an int or something else.
I tried this:
NSString* catId = [catData[#"id"] stringValue];
But that results in this:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString stringValue]: unrecognized selector sent to instance 0xa000030383130365'
How can I properly handle any type of value and get it converted to the string that I need?
I'm using AFNetworking with AFJSONResponseSerializer.

instead of using
NSString* catId = [catData[#"id"] stringValue
both case use
NSString* catId = catData[#"id"];
crash happen because you are trying to convert string to string value

Related

Extracting a value from SwiftDeferredNSDictionary by key in Objective C where key is Swift enum

Given
enum MyKey: String {
case aKey
}
let obj : [MyKey: Any] = [MyKey.aKey: "value"];
In my obj-c method I do:
[obj allKeys];
and then
for (id paramName in allKeys) {
id paramValue = [obj valueForKey:paramName]; // Crashes here
...
}
And it crashes with an exception 'NSInvalidArgumentException', reason: '-[__SwiftValue length]: unrecognized selector sent to instance
How do I properly extract value by key in this case?
Turns out that block enumerations works like a charm for Swift dictionaries in Objective c.
[obj enumerateKeysAndObjectsUsingBlock:^(id paramName, id paramValue, BOOL* stop) { }];

iOS issue with loop in NSDictionary

I have array like this:
(
{
code = "+39";
country = "Italy (+39)";
},
{
code = "+93";
country = "Afghanistan(+93)";
},
)
And i trying to get elements on loop like this discribed here
So there are my code:
int i = 0;
for (id key in self.codes) {
NSDictionary *value = [self.codes objectForKey:key];
if(row == i)
{
return [value objectForKey:#"country"];
}
i++;
}
and i have this error:
2014-10-22 15:49:56.791 Surfree[1097:60b] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x15387e00
2014-10-22 15:49:56.793 Surfree[1097:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x15387e00'
*** First throw call stack:
(0x2e385ecb 0x38b20ce7 0x2e3897f7 0x2e3880f7 0x2e2d7058 0xe51af 0x30e27bf9 0x30e2ae81 0x3112984b 0x30cea179 0x30c913db 0x30cd5c8f 0x3107ec81 0x31128d7b 0x30e29bcf 0x30e29a85 0x30e2a7eb 0x30e27159 0x30e2724f 0x30bb6d17 0x30bb6a85 0x30bb63ed 0x2ececd33 0x30bb6271 0x30bc2ffd 0x30bc2a73 0x30d8c165 0x30bb6d17 0x30bb6a85 0x30bb63ed 0x2ececd33 0x30bb6271 0x30bc2ffd 0x30bc2a73 0x30d8b979 0x30bc90b5 0x30d8b1cd 0x30d483b1 0x30c6546f 0x30c65279 0x30c65211 0x30bb72e5 0x3083331b 0x3082eb3f 0x3082e9d1 0x3082e3e5 0x3082e1f7 0x30bba99f 0x2e350faf 0x2e350477 0x2e34ec67 0x2e2b9729 0x2e2b950b 0x332286d3 0x30c1a871 0xe6bd9 0x3901eab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
How i can to fix it? and Why i have error?
Whe i do debug i see that error in this line :
NSDictionary *value = [self.codes objectForKey:key];
It looks like self.codes is an array; you can't access that like a dictionary. The correct way to use your code is like this:
int i = 0;
for (NSDictionary *value in self.codes) {
if(row == i)
{
return [value objectForKey:#"country"];
}
i++;
}
However, it really just looks like you're trying to access this:
self.codes[row][#"country"]
The error is that you call a NSDictionary method on the NSArray method.
self.codes is a NSArray, it needs to be a NSDictionary.

how to access single JSON value for key in Objective-C

I am new to Obj-C and somehow I can get the output of a JSON-Request due to difficulties with arrays, dictionareis and syntax. It would be great if someone could get me on my way.
NSLog(#"JSON Feed: %#", self.classified);
NSDictionary *test = self.classified;
NSLog(#"andy %#", [test objectForKey:#"text"]);
this throws an exception :
JSON Feed: (
{
text = "test text";
}
)
2014-06-05 17:25:28.170 Nerdfeed[2321:4107] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x10b988da0
2014-06-05 17:25:28.173 Nerdfeed[2321:4107] * Terminating app due to uncaught exception
I now tried to find a solution for hours but cant fix it. Thank you so much for any help.
self.classified seems to be a NSArray with a single item (and not a NSDictionary). Try [[self.classified firstObject] objectForKey:#"text"]

Xcode call stack

I'm getting an exception while running iOS app in simulator
2013-09-16 18:03:44.346 DEV[26529:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDecimalNumber isEqualToString:]: unrecognized selector sent to instance 0xf98fc70'
*** First throw call stack:
(0x17bf012 0x1c38e7e 0x184a4bd 0x17aebbc 0x17ae94e 0x1b813 0x1c4c705 0x653920 0x6538b8 0x714671 0x714bcf 0x713d38 0x917213 0x1787afe 0x1787a3d 0x17657c2 0x1764f44 0x1764e1b 0x29db7e3 0x29db668 0x65065c 0x25c9 0x2505 0x1)
UPD:
Exception is caused by the following code:
if([self.media.idtype isEqualToString:IMAGETYPE])
Where idtype is #property(retain, nonatomic) NSString *idtype; and IMAGETYPE is static NSString *VIDEOTYPE = #"2";
How can I see function names instead of addresses?
isEqualToString can compare two NSstrings not the other type of data that u are comparing which is NSDecimalNumber
convert your NSDecimalNumber to nsstring then do this
NSDecimalNumber* dec1;
NSString* str;
str = dec1.stringValue;
NSString *str2=#"world";
[str isEqualToString:str2];
There is some where in your code you are setting the value of property idtype (self.media.idtype). This must be the source of the problem:
Possible Problems could be Extracting contents from Webservice and while parsing you might be assigning it to the self.media.idtype object.

AFNetworking does not returns NSString if the value is contains only digits

I'm trying to receive a certain value in a json using AFNetworking. The value only contains digits and I want to receive it as a NSString. When I compare the received value, I get a exception (exception is also mentioned later in this post)
here is the coding producing the error.
NSArray *overallGameResultsArray = [resultDictionary valueForKey:#"overall_game_results"];
winCountDictionary = [[NSDictionary alloc] init];
for (NSDictionary *gameResultsDictionary in overallGameResultsArray)
{
NSString *userId = [gameResultsDictionary valueForKey:#"winner_user_id"];
NSString *winCount = [gameResultsDictionary valueForKey:#"win_count"];
if ([winCount isEqualToString:#"0"])
{
NSLog(#"0 wins");
}
}
I don't get the exception if the "if comparison" is commented. The win_count only consists of digits in the jSON response.
Following is the exception error I'm getting.
2012-10-10 15:41:35.086 FMB[3549:c07] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x7230af0 2012-10-10 15:41:35.086 FMB[3549:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x7230af0'
*** First throw call stack: (0x1638012 0x1345e7e 0x16c34bd 0x1627bbc 0x162794e 0x11e03 0x20583 0x1fab4 0x245053f 0x2462014 0x24527d5 0x15deaf5 0x15ddf44 0x15dde1b 0x26997e3 0x2699668 0x28d65c 0x2512 0x2445) libc++abi.dylib: terminate called throwing an exception
i think you'll have to do :
NSString *winCount=[NSString stringWithFormat:#"%d",[[gameResultsDictionary valueForKey:#"win_count"]intValue];
With this, you get the number value into the winCount NSString, and then you can use isEqualToString method
I suppose you use JSON. Then the type of the object is determined by JSON encoding. If there are " around, it's a string. If it's a number and you want string there, change how the data is encoded on the server.
However, in this context, using a number makes more sense. You should change the implementation in the following way.
NSNumber* winCount = [gameResultsDictionary valueForKey:#"win_count"];
if ([winCount intValue] == 0) { //this would actually work for both NSNumber and NSString
NSLog(#"0 wins");
}
Comparing numbers using string comparison is always a hint that you are doing something wrong.
The exception is thrown because you expect an object to be an NSString but actually is an NSNumber, so you try to call isEqualToString: on the NSNumber which doesn't implement the method.
Since you know that winCount is always a number and apparently the JSON deserializer that AFNetworking uses box the numbers in the response into NSNumber objects, you could easily grab a string out of the object like this:
NSString *winCount = [[gameResultsDictionary valueForKey:#"win_count"] stringValue];
Now you have a string representation of your number and you can therefor perform isEqualToString:
Also note that a more elegant solution might be to receive your objects as ids, find their class and do a comparison based on the class determined.

Resources