Array not retaining properly? - objective-c

I get the following error when running my App:
2011-09-02 15:38:44.157 TheApp[9973:207] -[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b28990
2011-09-02 15:38:44.160 TheApp[9973:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x4b28990'
And Xcode marks the line in this function in green:
- (void)oneCheckAndSetStrokes {
playerOneScoreNum.text = [playerOneScore objectAtIndex:(11)]; }
Therefore I'm guessing something is messed up with the Array. After som research I came across a lot of posts like this one: NSMutableArray : unrecognized selector sent to instance which seems to inticate that the error occurs when the Array is not being retain properly (hence my title).
So I'm trying to retain the Array in the file in wich it is initiated (which by the way is not the same file as the code above. The Array is also defined in another file, Globals.h, and then imported), in the following way:
- (void)viewDidLoad {
[super viewDidLoad];
playerOneScore = [[NSMutableArray alloc] initWithCapacity:19];
[playerOneScore retain]; }
This would solve the problem according to the post refered to earlier, but in my case it does not. Has anyone encountered something similar? It seems like I'm missing something trivial here.

The error you got has nothing to do with retain, you got that error because at this line:
playerOneScoreNum.text = [playerOneScore objectAtIndex:(11)];
you are trying to set a string property using a number object! You have to use "stringValue", in this way:
playerOneScoreNum.text = [[playerOneScore objectAtIndex:11] stringValue];
ps: wrapping the index (11) with parenthesis is useless :P

Related

Stupid crash when setting collection cell frame

It might be something stupid and i am doing this for years, but i just cant figure this one out .
In my collection view cell class i have this :
- (void)layoutSubviews
{
self.title.frame=CGRectMake(50, 50, 100, 100);
}
the cells are ok only if i remove this line of the frame settings, if not, i get :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString setFrame:]: unrecognized selector sent to instance 0x7fdcbbd0afb0'
everything is set, and i didn't miss a thing, i already done that many times.
What is this ??
EDIT:
#property(nonatomic,strong) UILabel *title;
Oh no... thanks to the help of the good people here, i found out that i do this
cell.title=[dic objectForKey:#"title"];
instead of this
cell.title.text=[dic objectForKey:#"title"];
so it turned to a string ! (??)

Why am I getting this: _cfurl: unrecognized selector

My init starts like this:
- (id) init {
[super init];
sounds = makeDictFromArrayOfURLs(getNoiseFileURLs());
[sounds retain];
NSURL *theFirstNoise = [[sounds allKeys] objectAtIndex:0];
CFURLRef uref = (CFURLRef)theFirstNoise;
OSStatus ret = AudioServicesCreateSystemSoundID(uref, &chosenNoise);
When we get to that last line, it throws this:
2011-06-09 23:19:18.744 SuperTimer[94516:207] -[NSPathStore2 _cfurl]: unrecognized selector sent to instance 0x940cfb0
2011-06-09 23:19:18.746 SuperTimer[94516:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSPathStore2 _cfurl]: unrecognized selector sent to instance 0x940cfb0'
Yeah, it's a bit uncompact for debugging.
Just before I get the dump, theFirstNoise contains the expected (sort of) data. (It's description method prints a weird form, but I am informed that's normal.)
Off the top of my head, it looks like theFirstNoise is actually an NSPathStore2 (a private subclass of NSString) instead of an NSURL.
Edit: NSPathStore2 objects will contain file paths. If you need to turn these into NSURLs, you can simply pass them to +[NSURL fileURLWithPath:].
This line:
NSURL *theFirstNoise = [[sounds allKeys] objectAtIndex:0];
is the problem: [sounds allKeys] returns an NSArray of keys, and objectAtIndex: therefore is returning an NSString, and not the URL. I wish the compiler would have been a little more helpful.

NSInvalidArgumentException... how do I define the arguments correctly?

I'm getting this exception on the following code. I think it's because I have not defined the two incoming parameter types. They are local; so how do I define them (and where).
Error: 2011-04-27 11:18:03.226
PointPeek[174:707] * Terminating app
due to uncaught exception
'NSInvalidArgumentException', reason:
'+[SQLiteDB addRecordToDatabase::]:
unrecognized selector sent to class
0x1fe70'
Here's the calling line of code:
[SQLiteDB addRecordToDatabase:
symbol.data: symbol.typeName];
and here's the method I'm calling:
- (void) addRecordToDatabase:data: typeName {
NSString *insertCommand = [NSString stringWithFormat:#"INSERT INTO CardData (CARD_ID, CARD_NAME, CODE_VAL) VALUES ('/%#', '/%#', '/%#')", data, #"Test Card", typeName];
if(sqlite3_open_v2(cDatabasePath, &db, SQLITE_OPEN_READWRITE, NULL) == SQLITE_OK) {
}
Error: 2011-04-27 11:18:03.226
PointPeek[174:707] * Terminating app
due to uncaught exception
'NSInvalidArgumentException', reason:
'+[SQLiteDB addRecordToDatabase::]:
unrecognized selector sent to class
0x1fe70'
Basically, the "unrecognized selector sent to..." message means you tried to tell an object (or class) to do something it doesn't know how to do. ("selector" is another name for method).
You defined your method of the SQLiteDB class as an instance method:
- (void) addRecordToDatabase:data: typeName;
We know that because of the - in the method name (see Methods and Messaging and Class Interface). In the error message you got, notice that it began with a +, which means you attempted to call a method on the SQLiteDB class itself, rather than on an instance of that class.
In other words, you attempted to do this:
[SQLiteDB addRecordToDatabase: symbol.data: symbol.typeName];
when you needed to do something like this:
SQLiteDB *db = [[[SQLiteDB alloc] init] autorelease]; // an instance
[db addRecordToDatabase: symbol.data: symbol.typeName];
(Note that the previous 2 lines of code aren't all that useful in and of themselves. Presumably, instead of creating an instance of SQLiteDB in this method, you'd have it as an instance variable).
[SQLiteDB addRecordToDatabase: symbol.data: symbol.typeName];
That'd assume that addRecordToDabase:: is a class method, not an instance method.
Furthermore, that is an awful name for a method. Try something like:
- (void)addRecordWithData:(NSData*)aData andType:(NSString*)aType;
That is, bare :s are to be avoided and you should always specify the type of the parameter (and not fall back to id as you did here).
Finally, why aren't you using Core Data or, at the very least, FMDB? Raw SQLite is a waste of time.
SQLite is harder to write code for than Core Data, most likely. If you are a newbie to both, Core Data is a better return on investment of your time.
In any case, the questions in your comment indicate that you really need to start by understanding Objective-C. Apple provides an excellent language guide.

Unrecognized Selector Sent to Instance [NSCFString subarrayWithRange:]

I have the following code which is producing this error. I cannot understand why the subarrayWithRange message is being sent to a string? When it is clearly an array?
static const int kItemsPerView = 20;
NSRange rangeForView = NSMakeRange( page * kItemsPerView, kItemsPerView );
NSMutableArray *temp = [[APP_DELEGATE keysArray] mutableCopyWithZone:NULL];
NSArray *itemsForView = [temp subarrayWithRange:rangeForView];
for (int loopCounter = 0;loopCounter < r*c;loopCounter++){
NSLog(#"%i: %# ", loopCounter, [itemsForView objectAtIndex:loopCounter]);
}
Error:
-[NSCFString subarrayWithRange:]: unrecognized selector sent to instance 0x6b071a0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: [NSCFString subarrayWithRange:]:
Thanks
These kinds of errors are usually memory-management-related. Essentially, you're sending a message to an address that's now occupied by some other object because the previous occupant has unexpectedly disappeared. Since that address space could be occupied by anything, you just happen to be asking an NSCFString something to which it doesn't respond.
If you pause the debugger right after you create the temp array, what do you see assigned to temp? I'm guessing something's not quite right with whatever -keysArray returns. You might want to double-check how the memory is handled in whatever that's supposed to return. By the name, I suppose your app delegate has an array called "keysArray" as an instance variable. Perhaps that's not being properly retained when it's created or assigned?
So I had this one. I did something stupid. I assigned the UITextView to a string instead of it's text property. ie:
myObj.txtbxThing = [NSString stringWithFormat:#"%#", stuffString];
instead of:
myObj.txtbxThing.text = [NSString stringWithFormat:#"%#", stuffString];

[UILabel setAnswerText:]: unrecognized selector sent to instance 0x4916830'

so i am getting an uncaught exception and im not sure why. this is the error i get:
2010-08-22 10:39:25.080 MayanGlyphs[10903:207] * -[UILabel setAnswerText:]: unrecognized selector sent to instance 0x4916830
2010-08-22 10:39:25.081 MayanGlyphs[10903:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UILabel setAnswerText:]: unrecognized selector sent to instance 0x4916830'
also i am getting a warning that UILabel may not respond to setAnswerText (answerText is the name of the label) even though i have created the outlet, connected it in IB, and synthesized it.
does anyone know why this might be happening?
answerText is the name of the label? then what are you trying to do?
If you want to access the answerText object (that being an instance of UILabel) you need to use self.answerText not [UILabel setAnswerText];
If you want to change the text in the label use:
answerText.text = #"Hello, this is my text";
or similar.
You need to do [setAnswerText setText:#"my text"]; What you are doing now is wrong. The reason you get that exception is that UILabel does not have a class method called setAnswerText:.