Adding NSNumber Object to NSMutableArray [duplicate] - objective-c

This question already has answers here:
NSMutableArray can't be added to
(2 answers)
Closed 10 years ago.
This piece of code causes an error but it should be correct.
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *rowNumber = [[NSNumber alloc] initWithInt:indexPath.row];
[genreArray addObject:rowNumber];
[self.tableView reloadData];
}
genreArray is a NSMutableArray
But on touch on a cell I get this error
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** First throw call stack:
(0x1c9c012 0x10d9e7e 0x1c9bdeb 0x1d1c9a5 0x1d1c8b0 0x7b4c 0xcd285 0xcd4ed 0xad75b3 0x1c5b376 0x1c5ae06 0x1c42a82 0x1c41f44 0x1c41e1b 0x1bf67e3 0x1bf6668 0x1dffc 0x271d 0x2645)
libc++abi.dylib: terminate called throwing an exception
Has anyone got an idea why this isn't working?

Because your array is immutable...
Check the code where you instantiated genreArray : both type declaration, AND instantiation must be done with NSMutableArray
//valid declaration + instantiation
NSMutableArray *genreArray = [NSArray array];
//valid at compile-time : genreArray is supposed to be mutable
//however will result in runtime error : genreArray is actually immutable
[genreArray addObject:anObject];

Related

return Dictonary from Objective c file to Swift?

I am working with open cv and swift. While returning NSDictionary from Objective-C file to Swift getting an error.
2018-10-10 12:43:25.972927+0530 OPencvwithSwift[2430:618249] -[__NSFrozenDictionaryM length]: unrecognized selector sent to instance 0x1c022b6e0
2018-10-10 12:43:25.973704+0530 OPencvwithSwift[2430:618249] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSFrozenDictionaryM length]: unrecognized selector sent to instance 0x1c022b6e0'
*** First throw call stack:
(0x183eb6d8c 0x1830705ec 0x183ec4098 0x183ebc5c8 0x183da241c 0x1053c23d4 0x105360aa0 0x10585294c 0x104aec9a0 0x104ae287c 0x105cd51dc 0x105cd519c 0x105cd9d2c 0x183e5f070 0x183e5cbc8 0x183d7cda8 0x185d62020 0x18dd9c758 0x104af984c 0x18380dfc0)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here is Objective c code -
- (NSDictionary *)predict:(UIImage*)img confidence:(double)confidence {
cv::Mat src = [img cvMatRepresentationGray];
int label;
NSLog(#"%d",label);
std::cout<<_labelsDictionary;
self->_faceClassifier->predict(src, label, confidence);
NSLog(#"%f",confidence);
NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
[dict setObject:[NSNumber numberWithInt:confidence] forKey:_labelsDictionary[#(label)]];
NSLog(#"%#",dict);
return dict;
}
Calling this function from Swift:
let result = facemodel?.predict(greyimage, confidence: confidence) // crash on this line
Your application crash while executing Objective-C method probably. Use break point inside Objective-C method and continue step-by-step so you will find your crash.
There is no way to crash assign pretict method's return dict to result variable. It gives [AnyHashable: Any] to result, so you will have a [AnyHashable: Any] type result object.
If I understand you correctly, before you use the dictionary,In other places it has been pointed to a string. so crash

Crash when removing an object from NSMutableArray

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.

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?

When I am replacing or inserting an object into nsmutable array, I am getting an Exception

While replacing or inserting into an NSMutable array, I am getting exception as:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object'
[list replaceObjectAtIndex:indexRow withObject:editcontacts];
//or
[list insertObject:editcontacts atIndex:indexRow];
You are still using an NSArray instead of an NSMutableArray. You need to allocate list as such:
NSMutableArray *list = [[NSMutableArray alloc] init];
See this question

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];