NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x165d5150' - ios

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;

Related

Fatal error when setting UIImageView in objective c

Hello i have a UIImageview and trying to set the image which was previously saved in a NSArray and then the NSArray is saved into an NSMutableDictionary. Here is the error and the code. Any help appreciated.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isSymbolImage]: unrecognized selector sent to instance 0x100e14318'
terminating with uncaught exception of type NSException
myAppDelegate *appDelegate = (myAppDelegate *)[UIApplication sharedApplication].delegate;
NSMutableArray *allKeys = [[appDelegate.localDataForIngestAndErrorVideos allKeys] mutableCopy];
for (NSString *key in allKeys) {
NSArray *object = (NSArray*)[appDelegate.localDataForIngestAndErrorVideos objectForKey:key];
NSLog(#"id object ingest: %#",[object objectAtIndex:0]);
if ( [cell.Model.ContentItem.videoUploadStatus isEqualToString:#"ingest"])
{
cell.Model.ContentItem.mediaVideoUriHls = (NSURL*)[object objectAtIndex:2];
UIImage *tempImage = [[UIImage alloc]init];
tempImage = [object objectAtIndex:1];
[cell.mediaImageView setImage:tempImage]; <=== here crashes
}
}
The error [__NSCFConstantString _isSymbolImage]: unrecognized selector sent to instance is saying this:
Let's translate first: __NSCFConstantString is a inner (optimized) class for NSString, so just consider it as NSString
So you have NSString instance and you try to call _isSymbolImage method on it. That method is hidden, it's an under the hood call from iOS SDK.
But that method doesn't exists on NSString, it doesn't know it, hence the error you are getting.
Seeing the crash line:
[cell.mediaImageView setImage:tempImage];
The inner method called makes sense, you are treating tempImage as it were an UIImage.
So [object objectAtIndex:1] is a NSString not a UIImage.
Now, I'd suggest to use custom model for your NSDictionary appDelegate.localDataForIngestAndErrorVideos. It's better than handling NSArray/NSDictionary without knowing what's inside it each time.
You could also add to it methods/compute properties like -(NSURL)ingestURL, etc to make your code easier and more readable.
Side note:
UIImage *tempImage = [[UIImage alloc]init];
tempImage = [object objectAtIndex:1];
The alloc/init is useless, since you are setting the value. You are doing an alloc/init for nothing, since you are ignoring it just after.

Crash when assigning null to NSString from JSON data

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;

Crashing with Terminating app due to uncaught exception 'NSUnknownKeyException

I am saving JSON in NSUserDefaults. When I retrieve a NSArray, it's crashing for accessing one of my JSON keys and showing *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x174676fc0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key s.'
NSDictionary *status = [[NSUserDefaults standardUserDefaults] objectForKey:#”today”];
NSArray *ch = [status valueForKey:#"c"];
NSArray *service1 = [status valueForKey:#"s"];
NSArray *eventList = [status valueForKey:#"l"];
NSArray *time = [eventList valueForKey:#"s"];
NSArray *dur = [eventList valueForKey:#"d"];
NSArray *eveid = [eventList valueForKey:#"e"];
NSArray *title = [eventList valueForKey:#"t"];
It's crashing at 5 line of code.
TL;DR; You are getting an NSString back from NSUserDefaults, not the NSDictionary you expect.
Your exception:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x174676fc0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key s.'
Indicates that status is an instance of NSString, not NSDictionary.
This line:
NSDictionary *status = [[NSUserDefaults standardUserDefaults] objectForKey:#”today”];
Is returning an NSString. When you call -valueForKey: the receiver does not support that key, so it throws an NSUnknownKeyException exception. The solution here is to validate what you get from NSUserDefaults is what you expect.
Does it respond to -valueForKey:?
Does it support the key you are using?
Is it an NSDictionary?
Are you setting the NSUserDefaults value for today with a string elsewhere in your application when you meant to use a dictionary?
All of these things are worth checking.
You don't want to use valueForKey: in combination with JSON data. Instead, use objectForKey:, which is the NSDictionary method.
NSDictionary *status = [[NSUserDefaults standardUserDefaults] objectForKey:#"today"];
NSArray *ch = [status valueForKey:#"c"];
NSArray *service1 = [status valueForKey:#"s"];
Additionally, I propose you use the modern and shorter syntax:
NSDictionary *status = [[NSUserDefaults standardUserDefaults] objectForKey:#"today"];
NSArray *ch = status[#"c"];
NSArray *service1 = status[#"s"];
BTW.: Are all elements in status of type array? That's a bit surprising.

__NSCFString objectForKeyedSubscript: exception

I take datas from server. My app work fine in Sinulator and test device iPhone 4s, but one man have problem on iPod 4. He get exception:
-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x1d263a20
I cann't use this device so I write code to know where crash was.
if (![dictionaryRest[#"compliments"] isEqual:[NSNull null]]) {
NSMutableArray *array = [NSMutableArray new];
NSMutableArray *firstArray = [NSMutableArray new];
for (NSDictionary *dic in dictionaryRest[#"compliments"]) {
Compliment *compl = [Compliment new];
if (![dic[#"ID_promotions"] isEqual:[NSNull null]])
compl.ID = [dic[#"ID_promotions"] integerValue];
So in last 2 strings this exception was. What the reason of this? So I understand that I need use
if ([dict objectForKey:[#"compliments"])
instead
if (![dict[#"compliments"] isEqual:[NSNull null]])
and in all another cases.
I test now and I have in my dictionary for ID:
You have an NSString instance in your dictionary where you expect a dictionary.
Note that your "use this instead of that" has nothing to do with the problem.
-objectForKeyedSubscript: is an instance method on NSDictionary object.
__NSCFString objectForKeyedSubscript: exception indicates that the method -objectForKeyedSubscript is somehow getting called on some NSString object.
So basically you just have to properly check for the class of the object before you can safely assume that it is actually dictionary.
if([dic isKindOfClass:[NSDictionary class]])
{
id obj = dic[#"key"];
}

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++;
}

Resources