I have a singleton in application and need to call its function with arguements from another class, but when I call it, nil arguements are passed…here is the code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog([Singleton sharedMySingleton].test);
NSDictionary *story = [self.filters objectAtIndex: indexPath.row];
MainAppRecord *record = [[[MainAppRecord alloc]init]retain];
record.name = [story objectForKey:@"Name"];
record.searchUrl = [story objectForKey:@"SearchUrl"];
record.icon = [imageCash objectForKey:indexPath];
[[Singleton sharedMySingleton] changeMainFilterTo:record atPosition:indexOfFilterToChange];
}
Here is Singleton.h and its function:
@interface Singleton : NSObject {
NSMutableArray *mainFilters;
NSString *test;
}
@property (nonatomic, retain) NSMutableArray *mainFilters;
@property (nonatomic, retain) MainAppRecord *filterToChange;
-(void) initWithPlist;
-(void) saveToPlist;
-(void)changeMainFilterTo:(MainAppRecord*)record atPosition:(int)position;
+(Singleton *)sharedMySingleton;
@end
-(void)changeMainFilterTo:(MainAppRecord*)record atPosition:(int)position
{
[mainFilters insertObject:record atIndex:position];
}
The app crashes with
2012-10-20 16:24:52.789 TableView[1957:207] -[__NSArrayI insertObject:atIndex:]: unrecognized selector sent to instance 0x68410f0
Thanks
This is nothing to do with arguments not being passed, or singletons.
You’ve declared an NSMutableArray property in your singleton, but it looks like you’ve assigned an NSArray to it. You don’t show where you set this up but it looks like you’re loading it from a property list – this creates a immutable array by default. You then try to insert an object – which up can’t do to an immutable array.
Ensure that when you create the array, you are creating a mutable array.
The simplest way to do this is to find the code where you are currently assigning the array, and make a mutable copy instead: