私はこのトピックについて多くの質問があることを知っていますが、私のために働いていないものは私のために少し奇妙です。NSMutableDictionary EXC_BAD_ACCESS
まずはスタティックシングルトンクラスを作成します。その後、私は以下のように別のクラスからこのNSMutableDictionaryを呼び出す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* 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
私はこの問題について夢中になります。誰かがアイデアを持っているなら、それを私と共有してください。
EDIT:助けを
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];
感謝。
オマールファルクKURT開始する
オブジェクトを辞書に追加したコードも投稿してください。 – xuzhe