iOS issue with loop in NSDictionary - ios

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.

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) { }];

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance

I'm trying to alter the following array like this.
Actual Response:
errFlag = 0;
errMsg = "Got Slot successfully.";
errNum = 128;
slots = (
{
date = "";
slot = (
{
booked = 1;
end = 1510822800;
"end_dt" = "09:00:00";
id = 5a02a0372279511b1c968a10;
locId = "";
radius = 1;
"slot_price" = 20;
start = 1510815600;
"start_dt" = "07:00:00";
}
);
}
);
The response I want it like...
{
"start_dt" = "07:00:00";
}
{
"start_dt" = "11:00:00";
}
{
"start_dt" = "14:00:00";
}
I Attempted this on following way but I ended up with an excemption.
if (requestType == RequestTypeGetAllMetSlots)
{
// altering the response into an array
NSDictionary *slotResultsDict = response[#"slots"];
NSLog(#"Priniting timeSlotResponseDict%#",slotResultsDict);
timeSlotResponseArray = [[slotResultsDict objectForKey:#"slot"] valueForKey:#"start_dt"];
NSLog(#"Priniting timeSlotResponseArray%#",timeSlotResponseArray);
}
Following is the Exeception I got.. Please help me..
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance
Crashing because response is an array not dictionary. And slot is also an array not dictionary.
Try this.
NSArray *slots = response[#"slots"]
NSDictionary *slotResultsDict = [slots firstObject];
NSLog(#"Priniting timeSlotResponseDict%#",slotResultsDict);
NSDictionary *subSlot = [[slotResultsDict objectForKey:#"slot"] firstObject];
timeSlotResponseArray = [subSlot valueForKey:#"start_dt"];
NSLog(#"Priniting timeSlotResponseArray%#",timeSlotResponseArray);
Or in one line.
NSString *startDate = [[[[response[#"slots"] firstObject] objectForKey:#"slot"] firstObject] objectForKey:#"start_dt"];

Handle either a string or int in JSON response

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

Loop through array then compare current object to string

I'm trying to iterate through an NSArray and check if an objectAtIndex is equal to a string.
NSLog(#"%#", myArray) // 3 items. 1 of them is "a"
for (id object in myArray)
{
NSLog(#"What"); // 3 times
if ([object isEqual:#"a"]) {
NSLog(#"Hello"); // Never gets executed
}
}
The problem is, the NSLog in the if statement never gets executed?
Edit
(
(
a
),
(
01
),
(
a
),
(
03
)
)
When I set it to isEqualToString, I get this error:
2015-03-30 14:42:54.206 MyApp[1575:50954] -[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x7fe721ce3bf0
2015-03-30 14:42:54.215 MyApp[1575:50954] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM isEqualToString:]: unrecognized selector sent to instance 0x7fe721ce3bf0'
Your problem is that you have an array that contains three sub-arrays, each of which contain, what is presumably, a single string. You can tell this because of the extra ()'s around the strings in the log output, and by the fact that it's telling you that you tried to send a selector to __NSArrayM.
Here is a quick fix:
NSLog(#"%#", myArray) // 3 items. 1 of them is "a"
for (NSArray *array in myArray)
{
NSLog(#"What"); // 3 times
if ([array.firstObject isEqualToString:#"a"])
{
NSLog(#"Hello"); // Never gets executed
}
}
But as others have pointed out you probably want to use isEqualToString: since it will be more performant.
You may also want to reconsider the code that is generating this nested array structure, or the schema that you're using in general, because it seems... unnecessary. Without further information there's not much to be done.

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"]

Resources