I have a simple value in an NSSTRING that I want to convert to an NSNumber. I do this all the time in my code and for some reason, this time it is not working. Do you see anything wrong with this?
NSNumberFormatter * num_formatter = [[NSNumberFormatter alloc] init];
[num_formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *score;
NSString *mystr = [[player ScoresArray] objectAtIndex:currentKeeper - 1];
NSLog(#"here is my string: -%#-", mystr);
score = [num_formatter numberFromString:mystr]; // crash occurs on this line. see error below...
NSLog(#"now it is: %d", [score intValue]); // it never gets to this line...
Here is the output from the above code:
here is my string: -3-
Here is the error I get:
2013-02-26 17:21:48.912 Golf Whiz[50407:c07] -[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xcb5fa90
2013-02-26 17:21:48.912 Golf Whiz[50407:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0xcb5fa90'
*** First throw call stack:
Make sure the objects you're adding to ScoresArray are in fact strings, not NSNumbers.
Thanks, all, as suspected, it turns out that I did have nsnumber objects in the scoresArray afterall.
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];
With these lines of code:
PayPalPayment *payment = [[PayPalPayment alloc] init];
NSLog(#"Price %#",[self.product objectForKey:#"Price"]);
NSString *string = [self.product objectForKey:#"Price"];
payment.amount = [[NSDecimalNumber alloc] initWithString:string];
my app is crashing with this log:
[NSDecimalNumber length]: unrecognized selector sent to instance
The NSLog is returning:
Price 96.67
Whay is it crashing???
I would guess that you have a type problem, most likely with the object at
[self.product objectForKey:#"Price"];
My guess is that this object is already an NSDecimalNumber, and when you treat it as a string and then call the initWithString method using it, bad things happen
put a breakpoint at:
payment.amount = [[NSDecimalNumber alloc] initWithString:string];
and then type po [string class] into the debugger.
This will show you the type of object stored at [self.product objectForKey:#"Price"];
It looks like your string object isn't an NSString. Looks like it's actually an NSDecimalNumber - set a breakpoint there and inspect the object. What kind of object is self.product?
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.