-(void)saveDictionary:(int)counter
{
NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSString *test = [NSString stringWithFormat:@"%d", counter];
[theDictionary setObject:test forKey:@"Counter"]; <---- Error
[theDictionary writeToFile:path atomically:YES];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self saveDictionary:[_viewController counter]];
}
エラー "認識されていないセレクタは、インスタンスに送信":NSMutableDictionary
-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setObject:forKey:]: unrecognized selector sent to instance
私はplistのキーから "カウンタ" の値をロードすることができます。 同じキー "カウンタ"の新しい値を保存する場合は、エラーです。 ヘルプ、時間を要しました。
さようならここ
がtheDictionary
を初期化するコードです:
-(void)initDictionary {
if (theDictionary == nil) {
NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
theDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
theString = [theDictionary objectForKey:@"Counter"];
}
}
はそれを見つけました!
theString = [[NSString alloc] initWithFormat:[theDictionary objectForKey:@"Counter"]];
ありがとうございます! theDictionary
に格納されたオブジェクトが実際NSString
とNSString
-setObject:forKey
呼び出されたメソッドが含まれていないあるため