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;
Related
Hi I am getting this data form server
NSDictionary*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed); //Outputs: feed = ( { code = yQ7j0t; "user_id" = 889445341091863; } ); }
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;
And beacuse of that I am getting below error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: selector sent to instance 0x165d5150'``
Any help will be appreciated.
The issue is, your feed key holds an array. You are not properly handling that in your code, that is why the crash occurs. When you call valueForKey: it retrieves an array of values held by that specific key.
For fixing that you can use:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSArray *referralCodes = [feed valueForKey:#"code"];
NSString *referralCode = referralCodes.count ? referralCodes[0] : #"";
NSLog(#"%#",referralCode);
But I personally prefer using objectForKey: instead of valueForKey:. So you can re-write the code like:
NSArray *feed = [saveDic objectForKey:#"feed"];
NSString *referralCode = feed.count ? [feed[0] objectForKey:#"code"] : #"";
NSLog(#"%#",referralCode);
Some where you use a variable;
yourVaribleName.length
or
[yourVaribleName length]
which should be
yourVaribleName.count
note: the crash says exactly that "yourVaribleName" is NSArray type where you wants length of the NSArray. But NSArray has not feature "length". NSArray has "Count" feature
//try with this code bellow
NSArray *referralCode = [feed valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=[referralCode componentsJoinedByString:#" "];//#"," or #"" what event you need
Your feed data is in array. So you have retrieve code value from array.Hope it will help you.
NSMutableArray*feed=[saveDic objectForKey:#"feed"];
NSLog(#"%#",feed);
NSLog(#"%#",[feed valueForKey:#"code"]);
NSString *referralCode = [[feed objectAtIndex:indexPath]valueForKey:#"code"];
NSLog(#"%#",referralCode);
self.referralCode.text=referralCode;
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 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.
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++;
}
I'm trying to replace a string with another one by using stringByReplacingOccurrencesOfString, but for some reason it's giving me this error:
-[__NSCFNumber length]: unrecognized selector sent to instance 0x6e49ef0
2012-05-14 16:30:49.741 coop[78129:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x6e49ef0'
NSString *_currentGroup;
NSString *location = [current objectForKey:#"location"];
if(_currentGroup != nil)
{
NSLog(#"_currentGroup: %#", _currentGroup);
// OUTPUT: _currentGroup: 92
location = [location stringByReplacingOccurrencesOfString:#"%group_id%" withString:_currentGroup];
}
When I try the following it just works
location = [location stringByReplacingOccurrencesOfString:#"%group_id%" withString:#"anyOtherString"];
Am I still missing something?
Your "NSString" is actually an NSNumber. That's what the error is telling you.