objective-ciosarraysretain

Array not retaining properly?


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.


Solution

  • 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