0
在UserDefault文件中,我有六個字段。在某些情況下,我總想堅持兩場,並重置四場。處理這個問題的最佳方法是什麼?將NSUserDefaults當作兩個plist文件?
在UserDefault文件中,我有六個字段。在某些情況下,我總想堅持兩場,並重置四場。處理這個問題的最佳方法是什麼?將NSUserDefaults當作兩個plist文件?
如果想四場的默認值重置。
,默認值可能是一個empty string
,
#define KDefault @""
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (standardUserDefaults)
{
[standardUserDefaults setObject:KDefault forKey:@"first"];
[standardUserDefaults setObject:KDefault forKey:@"two"];
[standardUserDefaults setObject:KDefault forKey:@"third"];
[standardUserDefaults setObject:KDefault forKey:@"four"];
[standardUserDefaults synchronize];
}
編輯: ,拆卸場,而不是重置:使用下面
[standardUserDefaults removeObjectForKey:@"first"];
[standardUserDefaults removeObjectForKey:@"two"];
[standardUserDefaults removeObjectForKey:@"third"];
[standardUserDefaults removeObjectForKey:@"four"];
[standardUserDefaults synchronize];
。你可以使用NSFileManager..sample代碼
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
NSString *documentsDirectory = [paths objectAtIndex:0]; //2
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"user.plist"]; //3
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) //4
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"user" ofType:@"plist"]; //5
[fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
}
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
//load from savedStock example int value
NSString* userName;
NSString* password;
NSString* pin;
userName = [savedStock objectForKey:@"name"];
password = [savedStock objectForKey:@"password"];
if([userName isEqualToString:@"some"] && [userName isEqualToString:@"some"] && [pin isEqualToString:@"some"])
{
[self setAdd];
}
else
{
[self pinAdd];
}
@ jhaliya-感謝的答覆是沒有任何方式刪除四個領域而不是重置??? – 12345 2011-04-11 07:28:46