我知道关于这个话题有很多问题,但他们中没有一个对我有用,因为我有点奇怪。NSMutableDictionary EXC_BAD_ACCESS
首先我创建一个静态单例类。和声明的NSMutableDictionary
的可变static NSMutableDictionary* mydic
@implementation mySingleton
-(mySingleton*)getInstance
{
static mySingleton *sharedInstance;
@synchronized(self)
{
if(!sharedInstance)
{
sharedInstance = [[mySingleton alloc] init];
mydic = [[NSMutableDictionary alloc] initWithCapacity:1];
}
return sharedInstance;
}
}
-(NSMutableDictionary*)getDictionary
{
return myDic;
}
然后我把这种的NSMutableDictionary从像下方的另一类。
NSMutableDictionary* singletonDictionary = [[mySingleton getInstance] getDictionary];
MyOtherClass* myclass = [singletonDictionary objectForKey:key];// Key is NSString
// Here I can see whole the values I added to myClass for that key
NSArray *checkKey = [singletonDictionary allKeys];
for(int i = 0; i < [singletonDictionary count]; i++)
{
NSLog(@"%@",[checkKey objectAtIndex:i]);// here I can see my key is there
}
[singletonDictionary removeObjectForKey:key];// here it crashes EXC_BAD_ACCESS
我会为这个问题疯狂。如果有人有想法,请与我分享。
编辑:
MyOtherClass * myinstance = [[MyOtherClass alloc] init];
// Fill up the instance with the desired variable here
// Forexample
// myinstance.name = [NSString stringWithFormat:@"myInstanceName"];
.
.
.
[[[mySingleton getInstance] getDictionary] setObject:myinstance forKey:key]// key is an NSString*
[myinstance release];
感谢您的帮助。
Omer Faruk KURT
你是否也请将你的代码添加到字典中的对象? – xuzhe