オブジェクトの一連のプロパティがある場合は、それらを NSUserDefaults の辞書に保存できます。各プロパティを設定するたびに保存するには、これを使用して各プロパティのカスタム セッター メソッドを作成できます。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"yourKey"] == nil) {
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"yourObject" forKey:@"keyForYourObject"];
// Add stuff to the dictionary
[defaults setObject:dict forKey:@"yourKey"];
}else{
NSDictionary *dict = [defaults objectForKey:@"yourKey"];
// Add stuff to the dictionary
[defaults setObject:dict forKey:@"yourKey"];
}
これにより、すべてのプロパティとともに UserDefaults に保存された単一の辞書が得られます。次に、標準メソッドを使用してキーのリストを取得します。
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"yourKey"];
NSArray *keys = [dict allKeys];