NSArray valueForKey ObjectAtIndex - ios

I have an NSArray that i want a single value of.
_membersArray = [_detailItem valueForKey:#"members"];
When i use this code:
NSLog(#"%lu", (unsigned long)[[_membersArray valueForKey:#"name"] count ]);
i get: 1
When i use this one:
NSLog(#"%#", [_membersArray valueForKey:#"name"] );
i get
{(
"Name Lastname"
)}
But i need only "Name lastname"
And when i use:
NSLog(#"%#", [[_membersArray valueForKey:#"name"] objectAtIndex:0]);
i get:
[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x10bf5caf0
2014-03-13 12:35:39.261 Givem[2454:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSetI objectAtIndex:]: unrecognized selector sent to instance 0x10bf5caf0'
i guess this is a easy one but i have a codeblock. Thanks for helping me out

Then try this..
[[[_membersArray valueForKey:#"name"] allObjects]objectAtIndex:0];

Look at exception ... It says ...
[__NSSetI objectAtIndex:] ...
... it means, that you're trying to send objectAtIndex: message to NSSet, not NSArray. And NSSet doesn't have objectAtIndex: method.

Related

Crashing while Adding NSMutableDictionary to NSMutableArray in iOS?

I was trying to save some data into fire base.So the format is like am adding contents into NSMutableDictionary and then am adding that into NSMutableArray and sending to FireBase.For first time it was working when i tried to add second contents its crashing with following message
2014-12-08 14:36:43.388 ChangeText[1634:576618] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580
2014-12-08 14:36:43.392 ChangeText[1634:576618] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580'
*** First throw call stack:
(0x184db259c 0x1954c00e4 0x184db9664 0x184db6418 0x184cbab6c 0x1000fce74 0x189598d34 0x189581e48 0x1895986d0 0x18959835c 0x1895918b0 0x189564fa8 0x189803f58 0x189563510 0x184d6a9ec 0x184d69c90 0x184d67d40 0x184c950a4 0x18de3f5a4 0x1895ca3c0 0x1000fd7ec 0x195b2ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException
My code
Viewdidload
arr=[[NSMutableArray alloc]init];
Firebase *fire=[[Firebase alloc]initWithUrl:#"http://del.firebaseio.com/users"];
[fire observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
// self.status.text=snapshot.value;
NSLog(#"snapshot%#",snapshot.value);
arr=[snapshot.value copy];
NSLog(#"copied%#",arr);
}];
Button to add values into Firebase
// arr=[[NSMutableArray alloc]init];
dict=[[NSMutableDictionary alloc]init];
[dict setValue:#"test1" forKey:#"username"];
[dict setValue:#"test2" forKey:#"password"];
[dict setValue:#"test3" forKey:#"profilepic"];
[dict setValue:#"test4" forKey:#"profile"];
[dict setValue:#"test5" forKey:#"details"];
[arr addObject:dict];
NSLog(#"added new elements%#",arr);
Firebase *f=[[Firebase alloc]initWithUrl:#"http://del.firebaseio.com/users"];
[f setValue:arr ];
My console logs
2014-12-08 14:36:32.017 ChangeText[1634:576618] snapshot(
{
details = test5;
password = test2;
profile = test4;
profilepic = test3;
username = test1;
}
)
2014-12-08 14:36:32.019 ChangeText[1634:576618] copied(
{
details = test5;
password = test2;
profile = test4;
profilepic = test3;
username = test1;
}
)
2014-12-08 14:36:43.388 ChangeText[1634:576618] -[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580
2014-12-08 14:36:43.392 ChangeText[1634:576618] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI addObject:]: unrecognized selector sent to instance 0x17023e580'
*** First throw call stack:
(0x184db259c 0x1954c00e4 0x184db9664 0x184db6418 0x184cbab6c 0x1000fce74 0x189598d34 0x189581e48 0x1895986d0 0x18959835c 0x1895918b0 0x189564fa8 0x189803f58 0x189563510 0x184d6a9ec 0x184d69c90 0x184d67d40 0x184c950a4 0x18de3f5a4 0x1895ca3c0 0x1000fd7ec 0x195b2ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException
Please help me to fix this error
[arr addObject:dict]; is creating the crash in this case, since arr is Immutable.
arr=[snapshot.value copy]; gives you an immutable copy of object. You could try with
arr=[snapshot.value mutableCopy];
arr = [snapshot.value copy];
The above line assigns NSArray type instance to your arr object. That's the reason when you try to add objects in your array, it gets crashed because the current instance is of type NSArray, not NSMutableArray. Replace above line with the following line:
[arr addObjectsFromArray:snapshot.value];

setValue:forUndefinedKey this class is not key value coding-compliant

I have an nsmutablearray that looks like this:
(
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=6FC0C2DC-69BB-4FAD-9709-63E03182BEE1&ext=JPG";
},
{
caption = "";
urlRep = "assets-library://asset/asset.JPG?id=324E4377-0BCD-431C-8A57-535BC0FC44EB&ext=JPG";
}
)
And im trying to set the value of caption like this:
[[[self.form valueForKey:#"photos"] objectAtIndex:indexPath.row] setValue:#"hi" forKey:#"caption"];
([self.form valueForKey:#"photos"] is the array)
but I get :
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0xa68ec40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key caption.'
EDIT:
If I use setObject forKey I get:
-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0xa6a88f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0xa6a88f0'
How do I fix this?
CORRECT CODE TO FIX THIS:
NSMutableDictionary *m = [[[self.form valueForKey:#"photos"] objectAtIndex:indexPath.row ] mutableCopy];
NSMutableArray *array = [self.form valueForKey:#"photos"];
[m setObject:textField.text forKey:#"caption"];
[array replaceObjectAtIndex:indexPath.row withObject:m];
The reason you are getting the exception is because you have an array of NSDictionary objects, which don't respond to setObject:forKey: (or setValue:forKey:).
You probably want to convert them all to NSMutableDictionary objects as soon as you receive them.

__NSCFConstantString objectForKey unrecognized selector sent to instance error

I basically get this error
'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x581f0'
on my program. I think it refers to this call I make,
if (data != nil) {
if([data objectForKey:#"username"]){
// NSArray *check= [[NSArray alloc]init];
//check=[data allValues];
[dict setObject:[data allValues] forKey:#"args"];
}else{
[dict setObject:[NSArray arrayWithObject:data] forKey:#"args"];
}
at the setObject:[data allValues]. I don't know why it gives that error but data is an NSDictionary and I'm getting all the values and placing it in an array.
Is the error happening here:
if([data objectForKey:#"username"]){
I assume so, as that is the only place objectForKey seems to be called. You are calling it on a variable called 'data', which i'm guessing simply is not a dictionary. You should NSLog its type to see.

Why do i get an error comparing NSString's? (-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance)

I have a NSMutableArray (_theListOfAllQuestions) that I am populating with numbers from a file. Then I compared the objects in that array with qNr (NSString) and I got error. I even casted the array to another NSString, _checkQuestions, just to be sure I am comparing NSStrings. I tested using item to compare also.
-(void)read_A_Question:(NSString *)qNr {
NSLog(#"read_A_Question: %#", qNr);
int counter = 0;
for (NSString *item in _theListOfAllQuestions) {
NSLog(#"item: %#", item);
_checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
NSLog(#"_checkQuestions: %#", _checkQuestions);
if ([_checkQuestions isEqualToString:qNr]) {
NSLog(#">>HIT<<");
exit(0); //Just for the testing
}
counter++;
}
When running this code i get the following NSLog:
read_A_Question: 421
item: 1193
_checkQuestions: 1193
...and error:
-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9246d80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber
isEqualToString:]: unrecognized selector sent to instance 0x9246d80'
I do believe that I still comparing NSString with a number of some sort but to me it looks like I am comparing NSString vs. NSString?
I could really need some help here to 1) understand the problem, 2)solve the problem?
Replace this line
if ([_checkQuestions isEqualToString:qNr])
with
if ([[NSString stringWithFormat:#"%#",_checkQuestions] isEqualToString:[NSString stringWithFormat:#"%#",qNr]])
Hope it helps you..
Your _theListOfAllQuestions array has NSNumber objects and not NSString objects. So you cant use isEqualToString directly.
Try this,
for (NSString *item in _theListOfAllQuestions) {
NSLog(#"item: %#", item);
_checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
NSLog(#"_checkQuestions: %#", _checkQuestions);
if ([[_checkQuestions stringValue] isEqualToString:qNr]) {
NSLog(#">>HIT<<");
exit(0); //Just for the testing
}
counter++;
}

NSArray SIGABRT

NSString* year = [self.years objectAtIndex:[indexPath section]];
//get algorithms for that year
NSArray* algorithmSection = [self.algorithmNames objectForKey:year];
NSLog(#"%#", indexPath);
//get specific algorithm based on that row
cell.textLabel.text = [algorithmSection objectAtIndex:indexPath.row];
return cell;
For whatever reason, when I compile this, I get a SIGABRT error. It happens on the
cell.textLabel.text
line. Error:
2011-08-29 19:26:21.273 xxxxxxxxxxx[1431:b303] 2 indexes [0, 0]
2011-08-29 19:26:21.274 xxxxxxxxxxxxx[1431:b303] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4ba2370
2011-08-29 19:26:21.277 xxxxxxxxx[1431:b303] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4ba2370'
terminate called throwing an exception
Your algorithmSection variable, which your code expects to be an NSArray, is actually an NSDictionary, which does not respond to the selector -objectAtIndex:.
As lemnar and mjisawai said, you are actually dealing with an NSDictionary.
The way to fix this depends on the context of your app.
If you happen to receive either, NSArrays or NSDictionary objects then you may determine this by texting the object's class.
i.e.
if ([algorithmSection isKindOfClass:[NSArray class]]) {
...
} else if ([algorithmSection isKindOfClass:[NSDictionary class]]) {
...
}
Could the object at key "year" in your algorithmSections dictionary be a dictionary instead of an array? That's what it looks like is happening here.

Resources