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.
Related
I am experiencing a crash that I have narrowed down to this line of code but I dont understand how I can avoid it.
NSString *variantImageUrl = variantEdge[#"node"][#"image"][#"src"];
If the value of src is null the app crashes with the following error message
2017-11-21 17:23:27.023988+0800 NWMPos[3218:2124447] -[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1b2a01650
2017-11-21 17:23:27.026199+0800 NWMPos[3218:2124447] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1b2a01650'
As long as the value of src is a proper string it works fine
But I cannot check the value for null before I assign it so how can avoid the crash?
Add check for null:
if(![variantEdge[#"node"][#"image"][#"src"] isEqual:[NSNull null]])
{
NSString *variantImageUrl = variantEdge[#"node"][#"image"][#"src"];
}
Clumsy as hell, but
NSDictionary *dict = variantEdge;
for (NSString *key in #[#"node", #"image"]) {
dict = dict[key];
if (![dict isKindOfClass:NSDictionary.class]) {
dict = nil;
break;
}
}
NSString *url = dict ? dict["src"] : nil;
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];
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.
I'm getting an exception after attempting to remove an object from a NSMutableDictionary. The relevant code follows. The 'settings' is passed to the method and can be a NSDictionary or a NSMutableDictionary.
NSMutableDictionary *mutableSettings = nil;
if ([settings isKindOfClass:[NSMutableDictionary class]])
mutableSettings = (NSMutableDictionary *)settings;
else
mutableSettings = [[[NSMutableDictionary alloc] initWithDictionary:settings] autorelease];
[mutableSettings removeObjectForKey:#"akey"];
This crashes with
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object'
Whats wrong with this? Thanks.
The problem here is that both NSDictionary and NSMutableDictionary return __NSCFDictionary as their class, due to the fact that NSDictionary is a class cluster.
I think you will just have to make a mutable copy of the settings dictionary whether it is mutable or not.
NSMutableDictionary *mutableSettings = [settings mutableCopy];
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.