Trouble with initializing NSMutableArray in my Singleton - objective-c

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

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

Adding NSNumber Object to NSMutableArray [duplicate]

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

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.

Objective C/NSMutableDictionary - [NSCFSet objectForKey:]: unrecognized selector

I do not understand what I am doing wrong. I have a dictionary as a property of a singleton class:
#interface CABResourceManager : NSObject
{
....
NSMutableDictionary* soundMap;
}
#property (retain) NSMutableDictionary *soundMap;
Then I add an object to this dictionary in one class method:
+ (void)loadSoundFromInfo:(ABSoundInfo)sound
{
static unsigned int currentSoundID = 0;
CABSound* newSound = [[CABSound alloc] initWithInfo:(ABSoundInfo)sound soundID:++currentSoundID];
[[CABResourceManager sharedResMgr].soundMap setObject:newSound forKey:sound.name];
}
And try to get it in another method:
+ (ALuint)playSoundByName:(NSString*)name
{
NSMutableDictionary* map = [CABResourceManager sharedResMgr].soundMap;
CABSound *sound = [map objectForKey:name]; // here comes the exception
and the app exits on exception by that.
2011-03-27 20:46:53.943 Book3HD-EN[5485:207] *** -[NSCFSet objectForKey:]: unrecognized selector sent to instance 0x226950
2011-03-27 20:46:53.945 Book3HD-EN[5485:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException'
I guess it might have something with memory management, but hier it looks clear for me: CABSound object is retained in dictionary by doing setObject(), it should not be released at this time.
I'd check that soundMap is properly initialized. It looks like soundMap is a bad pointer at the time you get the error. It might happen to be nil in +loadSoundFromInfo, which wouldn't produce an error right away.
Make sure that you've initialized your soundMap in designated initializer:
// - (id) init... or something else
soundMap = [[NSMutableDictionary alloc] init];
Dont forget to override default dealloc implementation:
// class implementation file
- (void)dealloc {
[soundMap release];
//...release other objects you own...
[super dealloc];
}

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