Crash when removing an object from NSMutableArray - objective-c

I have a NSMutableArray that I've initialised in viewDidLoad:
self.titlesTagArreys = [#[#"Dollar", #"Euro", #"Pound",#"Dollar longString", #"Euro longStringlongString", #"Pound",#"Dollar", #"Euro", #"PoundlongStringlongString"]mutableCopy];
in .h:
#property(nonatomic, copy) NSMutableArray* titlesTagArreys;
When I try to delete one item, the app crashes:
-(void)removeButtonWasPressed:(NSString*)tagTitle{
NSLog(#"tagTitle - %#",tagTitle);
NSLog(#"self.titlesTagArreys - %#",self.titlesTagArreys);
[self.titlesTagArreys removeObject:tagTitle];
}
Here is the log:
2013-08-06 16:15:03.989 EpicTv[6378:907] tagTitle - Dollar
2013-08-06 16:15:03.991 EpicTv[6378:907] self.titlesTagArreys - (
Dollar,
Euro,
Pound,
"Dollar longString",
"Euro longStringlongString",
Pound,
Dollar,
Euro,
PoundlongStringlongString
)
[__NSArrayI removeObject:]: unrecognized selector sent to instance 0x1c53bbd0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI removeObject:]: unrecognized selector sent to instance 0x1c53bbd0'
*** First throw call stack:
(0x327162a3 0x3a5c197f 0x32719e07 0x32718531 0x3266ff68 0x20ad55 0x20c9a5 0x20bf5d 0x346090c5 0x34609077 0x34609055 0x3460890b 0x34608e01 0x345315f1 0x3451e801 0x3451e11b 0x362295a3 0x362291d3 0x326eb173 0x326eb117 0x326e9f99 0x3265cebd 0x3265cd49 0x362282eb 0x34572301 0xafb89 0xa4d68)
libc++abi.dylib: terminate called throwing an exception

It seems that titlesTagArrays list not an NSMutableArray because removeObject can not be called.
Maybe you passed an NSArray earlier in the code to titlesTagArreys.
try to init your Array with
self.titlesTagArreys = [NSMutableArray arrayWithArray:#[#"...",#"...",...]];
#property(nonatomic, copy) makes a not mutable copy of you NSMutableArray. try #property(nonatomic, retain) instead of copy

I also think that you titlesTagArreys is not mutable array because of some code changes
Try to add: NSLog(#"%#", NSStringFromClass(self.titlesTagArreys.class)); to check what class do you use
-(void)removeButtonWasPressed:(NSString*)tagTitle{
NSLog(#"%#", NSStringFromClass(self.titlesTagArreys.class));
[self.titlesTagArreys removeObject:tagTitle];
}

I had the same problem and learned that you must override the setter for the mutable array property and call mutableCopy. You'll find the answer in Stack Overflow here.

titlesTagArray is not NSMutableArray. This is because , in the logs we can see [__NSArrayI removeObject:] unrecognized selector sent to instance 0x1c53bbd0.
Also NSArrayI is used for NSArray and NSArrayM is used for NSMutableArray.
You must have initialised the mutablearray with an NSArray hence the exception.

Related

How to set the value of UILabel in iphone?

I'm doing this to set the value in UILabel:
NSMutableDictionary *responseDict = [responseString objectFromJSONString];
self.pkrLbl.text= [responseDict objectForKey:#"amount"];
and I'm getting this error:
[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9440310
2013-05-16 15:23:34.281 EasyLoadPakistan[20341:19a03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9440310'
Please help me how can I set value in UIlabel
A UILabel expects a string (specifically NSString or one of its subclasses), and you are passing it a number (an NSNumber instance).
Convert the number to a string first, then set the label to that string:
NSNumber *n = [responseDict objectForKey:#"amount"];
NSString *stringVersion = [n stringValue];
self.pkrLbl.text = stringVersion;
If you prefer to have it all on one line, you can omit the variables and just chain the stringValue call. The approach shown above tends to facilitate debugging, though (you can for instance set a breakpoint and have a look at your variables).

Can't add a UIViewController subclass to NSDictionary

I wan't to add a subclass of UIViewController to a NSMutableDictionary which should be no problem since UIViewController is a NSObject.
This is my code:
NSArray *requestingPathes = [[NSArray alloc] initWithObjects:indexPath, nil];
requestingPair = [[NSMutableDictionary alloc] initWithObjectsAndKeys:requestingPathes, dele, nil];
The app crashes when the delegate "dele" (which happens to be the mentioned subclass) is used to initialize the Dictionary "requestingPair".
The error is:
-[MainViewController copyWithZone:]: unrecognized selector sent to instance 0x7446b30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController copyWithZone:]: unrecognized selector sent to instance 0x7446b30
0x7446b30 is the address of the subclass "MainViewController.
Why is this happening?
-initWithObjectsAndKeys: expects a list of objects and keys, in that order—alternating object, key, object, key, nil. What you’re doing is trying to give it your dele object as a key from which the value requestingPathes can be retrieved; NSDictionary objects copy their keys when you set them, and your view controller doesn’t support being copied, so that’s failing. Presumably what you want to do is something more like this:
requestingPair = [[NSMutableDictionary alloc] initWithObjectsAndKeys:requestingPathes, #"paths", dele, #"delegate", nil];
You can then retrieve the delegate object by calling [requestingPair objectForKey:#"delegate"] and the paths with [requestPair objectForKey:#"paths"].
Your dictionary key must conform to the NSCopying protocol, which UIViewController (and your MainViewController) doesn't.
Do you want dele to be the key or the value? It's currently the key.

Unrecognized selector sent to instance - why?

OK so I have a code with an class object called "game". Every frame (60 FPS) I update that object with function that gets a string. After like 5 seconds of running the game I'm getting the unrecognized selector sent to instance error.
The update:
[game updatePlayersAndMonsters:#"0" monsters:#"0"];
The function:
-(void)updatePlayersAndMonsters:(NSString*)players monsters:(NSString*)monsters {
CCLOG(#"%#.%#", players, monsters);
}
I don't understand what's going on.
The error:
2011-07-03 12:13:19.175 app[65708:207] -[NSCFString updatePlayersAndMonsters:monsters:]: unrecognized selector sent to instance 0xc4e95b0
2011-07-03 12:13:19.176 app[65708:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString updatePlayersAndMonsters:monsters:]: unrecognized selector sent to instance 0xc4e95b0'
What should I do? Thanks. Also IDK if any other details you need, so just write if I forget something, I just don't have an idea.
UPDATE:
Gmae is object of class GameNode:
+(id) GmameNodeWithMapID:(int)MapID_ scene:(SomeScene*)MainScene_ players:(NSString*)Cplayers_ monsters:(NSString*)Cmonsters_ monsterCount:(NSString*)monsterCount_
{
return [[[self alloc] GmameNodeWithMapID:MapID_ scene:MainScene_ players:Cplayers_ monsters:Cmonsters_ monsterCount:monsterCount_] autorelease];
}
-(id) GmameNodeWithMapID:(int)MapID scene:(SomeScene*)MainScene players:(NSString*)Cplayers monsters:(NSString*)Cmonsters monsterCount:(NSString*)monsterCount
{
if( (self=[super init])) {
I create it with:
game = [GameNode GmameNodeWithMapID:ChoosenMapID scene:self players:Thing[5] monsters:Thing[6] monsterCount:Thing[4]];
UPDATE 2
I create the SomeScene:
+(id) scene {
CCScene *s = [CCScene node];
id node = [SomeScene node];
[s addChild:node];
return s;
}
-(id) init {
if( (self=[super init])) {
I use it:
[[CCDirector sharedDirector] replaceScene: [CCTransitionRadialCW transitionWithDuration:1.0f scene:[LoginScene scene]]];
Since you imply that the update function [game updatePlayersAndMonsters:#"0" monsters:#"0"]; is called for the first 5 seconds of your game and then you get the error, my guess is that the game object is not correctly retained, so it gets deallocated and the successive attempt of sending a message to it fails because some NSString object has been reusing its memory (and it does not have a updatePlayersAndMonsters:monsters selector).
Please share how game is created (alloc/init) and how it is stored in your classes to help you further.
Activating NSZombies tracking could also help to diagnose this.
EDIT: after you adding the code
It seems to me that in the line:
game = [GameNode GmameNodeWithMapID:ChoosenMapID scene:self players:Thing[5] monsters:Thing[6] monsterCount:Thing[4]];
you are setting either a local variable or an ivar to your autoreleased GameNode.
Now, since you are not using a property, nor I can see any retain on your autoreleased GameNode, my hypothesis seems confirmed. Either assign to a retain property:
self.game = [GameNode ...];
being game declared as:
#property (nonatomic, retain)...
or do a retain yourself:
game = [[GameNode GmameNodeWithMapID:ChoosenMapID scene:self players:Thing[5] monsters:Thing[6] monsterCount:Thing[4]] retain];
'NSInvalidArgumentException', reason: '-[NSCFString updatePlayersAndMonsters:monsters:]: means tht you are trying to send updatePlayersAndMonsters to a String object. Are you reassigning game to point to something else?

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.

Trouble with initializing NSMutableArray in my Singleton

I am getting a weird error, and I can't figure it out. This takes place inside of a class that is created with the singleton pattern:
- (NSMutableArray *) getCurrentClasses
{
NSMutableArray *current_classes = [[NSMutableArray init] alloc];
NSLog([NSString stringWithFormat:#"%d", [current_classes count]]);
...
}
When I run this, even though I literally just initialized current_classes, it gives me this error in log:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray count]: method sent to an uninitialized mutable array object'
Does anyone know what this is happening? I initialized it literally last line.
Thanks
You mixed up the alloc/init calls. alloc comes first. It should be:
NSMutableArray *current_classes = [[NSMutableArray alloc] init];