-[__NSCFNumber isEqualToString] error - objective-c

I'm getting this crash, but, in my code I am using a string. I've been working on this one piece of code for 2 hours now and I just can't see what I'm missing! Any ideas?
NSString *codeR = [NSString stringWithFormat:#"%#", [[object objectForKey:#"code"] stringValue]];
if([codeR isEqualToString:#"200"])
Error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x181cf0'
I would be very grateful input, this is confusing the hell out of me!
Thanks.

Get rid of silly redundancy, see what happens.
NSString *codeR = [[object objectForKey:#"code"] stringValue];
// mysterious missing code
if ([coreR isEqualToString:#"200"]) // etc
Also, are you sure the error is raised from the if statement you posted? It could be coming from elsewhere.

NSString *codeR = [[object objectForKey:#"code"] stringValue];
if ([codeR isEqualToString:#"200"])
{
// Do stuff...
}

Related

iOS App NSInvalidArgumentException

I have been trying to work through a bug for many hours in my iOS App involving rangeOfString and have finally been able to track down where my app crashes. My app sets TextViewA to the first half of my MainTextView. What if statement should be added should be added to check for nil?
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFString rangeOfString:options:range:locale:]: nil argument'
*** First throw call stack:
.
[[myViewController tvTextFirstHalf] setText:[[mainTextView.text componentsSeparatedByString:[[myViewController tvTextMiddle]text]] objectAtIndex:0]];
..............................................................................................
No memory available to program now: unsafe to call malloc
2013-01-04 14:30:58.138 Type[7279:c07] separator: (null)
2013-01-04 14:30:58.138 Type[7279:c07] mainTextView: ; layer = ; contentOffset: {0, 0}
>
2013-01-04 14:30:58.139 Type[7279:c07] mainTextView.text: TESTTTTTTTT f f.
2013-01-04 14:30:58.142 Type[7279:c07] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSCFString rangeOfString:options:range:locale:]: nil argument'
* First throw call stack:
(0x1a5d012 0x1882e7e 0x1a5cdeb 0xf78688 0xf87d88 0xfd05df 0x165ec 0x15dc6 0x1fd7b 0x56d8d5 0x56db3d 0xf74e83 0x1a1c376 0x1a1be06 0x1a03a82 0x1a02f44 0x1a02e1b 0x221d7e3 0x221d668 0x4be65c 0x2c5d 0x2b85)
libc++abi.dylib: terminate called throwing an exception
(gdb)
This oneliners are not considered a good practice in obj-C (i know, it's arguable).
Try to break:
[[myViewController tvTextFirstHalf] setText:[[mainTextView.text componentsSeparatedByString:[[myViewController tvTextMiddle]text]] objectAtIndex:0]];
Into some more debugging-friendly code:
NSString *separator = [[myViewController tvTextMiddle]text];
NSLog (#"separator: %#", separator);
NSLog (#"mainTextView: %#",mainTextView); //is this one nil?
NSLog (#"myViewController.tvTextMiddle: %#", myViewController.tvTextMiddle); //what about this one??
NSLog (#"mainTextView.text: %#",mainTextView.text);
NSArray *components = [mainTextView.text componentsSeparatedByString:separator];
NSLog (#"we found %d components", components.count);
NSString *result = (NSString *)[components objectAtIndex:0];
NSLog (#"result should be: %#", result);
[[myViewController tvTextFirstHalf] setText:result];
The code will break ofcourse but at least you'll know where the problem lies.
You could change last four lines then in something like:
if (components.count >= 1)
{
NSString *result = (NSString *)[components objectAtIndex:0];
NSLog (#"result should be: %#", result);
[[myViewController tvTextFirstHalf] setText:result];
}
else
{
[[myViewController tvTextFirstHalf] setText:#"empty!"];
}
EDIT:
From the NSLog you posted it is obvious that your separator [[myViewController tvTextMiddle]text] is null. Note that null is not the same as empty string. It is unallocated string.
My guess is that actually your tvTextMiddle is null (or if you are using XIB files it is not properly connected to the IBOutlet).
You can check that by adding (somewhere before the breaking-line):
NSLog (#"myViewController.tvTextMiddle: %#", myViewController.tvTextMiddle);
The crash is due to this call componentsSeparatedByString.
If your textview is nil or no text then crash'll occur.
So add a condition like:
if(mainTextView.text.length != 0 && [myViewController tvTextMiddle]text].length != 0)
{
[[myViewController tvTextFirstHalf] setText:[[mainTextView.text componentsSeparatedByString:[[myViewController tvTextMiddle]text]] objectAtIndex:0]];
}

Problems passing data between different classes with a method

I have this in a class:
NSString *globalMidiData = #"30a0a00\n";
switch (IndicatorCheckNXT) {
case 1:
[testRobot checkTestRobot:globalMidiData];
break;
default:
break;
}
And in another class I have this:
-(void) checkTestRobot: (NSString *)midiDataGlobal{
bool pressed;
bool pressed2;
NSString *miawmiaw =[NSString alloc];
miawmiaw=midiDataGlobal;
}
And I got this message:
-[AppDelegate checkTestRobot:]: unrecognized selector sent to instance 0x18acb0
2012-11-23 20:45:31.755 Exemple1[477:707] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AppDelegate checkTestRobot:]: unrecognized selector sent to instance 0x18acb0'
What I am doing wrong?
Obviously you send checkTestRobot to the wrong object. testRobot seems to point to the AppDelegate and not to an instance of your class.
Also you should replace this:
NSString *miawmiaw =[NSString alloc];
miawmiaw=midiDataGlobal;
with:
NSString *miawmiaw = [midiDataGlobal copy];

how to solve run time error of [NSURL fileURLWithPath:error:]: unrecognized selector sent to class?

I am beginner in IPhone Developing. I want play sound. so, that's why I have apply this code
(void)viewDidLoad
{
NSError* err;
path=[[NSBundle mainBundle] pathForResource:#"Animalfile" ofType:#"plist"];
dict=[NSDictionary dictionaryWithContentsOfFile:path];
NSArray *animalaudio=[dict valueForKey:#"audio"];
NSString *audiolist=[animalaudio objectAtIndex:currentsound];
AVAudioPlayer *audio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:audiolist error:&err]];
audio.delegate=self;
[audio play];
}
and I have got the run time error of
[NSURL fileURLWithPath:error:]: unrecognized selector sent to class 0x182121c
2012-07-14 14:51:21.711 plistdemo[1236:10703] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '+[NSURL fileURLWithPath:error:]:
unrecognized selector sent to class 0x182121c'
terminate called throwing an exceptionsharedlibrary apply-load-rules all
so, give any suggestion and source code which apply in my code
current documentation for NSURL doesn't list any such method like 'fileURLWithPath:error:'... seems like its either deprecated or incorrect.
instead try using something like:
[NSURL fileURLWithPath:audioList]
hope it helps...

Why CCLabelAtlas[1] refuses to update?

I have 7 CCLabelAtlas labels declared in .h file like this CCLabelAtlas *numberStat[7]. Then I then initialized them in a for loop in .m file:
for (int i = 1; i <=7; i++) {
NSString* statName = [NSString stringWithFormat #"Number %d", i];
numberStat[i] = [[CCLabelAtlas labelWithString: [self loadThisValue:statName] charMapFile:#"digitalNumbers.png" itemWidth:26 itemHeight:37 startCharMap:'0'] retain];
[self addChild: numberStat[i]];
}
The problem comes when I try to update the label. I can update from 2 to 7 just fine, but when I try to update numberStat[1]'s string (numberStat[1].string = #"111";), it crashes. The exact same code works for 2-7.
Here's the crash log:
-[CCSprite setString:]: unrecognized selector sent to instance 0x897cbd0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite setString:]: unrecognized selector sent to instance 0x897cbd0'
One possibility is that somewhere in your code, numberStat[1] is being over-released, and by the time you come to assign a string to it, a CCSprite has moved into the memory that it occupied.

Error saving to iOS keychain using KeychainItemWrapper

I get an error every time I try to store data into the keychain
The error comes from the dictionaryToSecItemFormat method at these lines
NSString *passwordString = [dictionaryToConvert objectForKey:(id)kSecValueData];
[returnDictionary setObject:[passwordString dataUsingEncoding:NSUTF8StringEncoding] forKey:(id)kSecValueData]'
The error is
I'm calling the KeychainItemWrapper methods like this
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:#"credentials" accessGroup:nil];
[keychain setObject:username forKey:kSecAttrAccount];
[keychain release];
The error is
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'-[NSConcreteData dataUsingEncoding:]: unrecognized selector sent to instance"
dataUsingEncoding is a method from the NSString class, make username a NSString.