2012年7月30日 星期一

iOS Preferences and Settings Programming

Set Default Value

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (int i = 0; i < 5; i++) {
    NSString *itemKey = [NSString stringWithFormat:@"item%d", i];
    NSNumber *itemValue = [NSNumber numberWithInt:0]; 
    NSDictionary *defaultItem = [NSDictionary dictionaryWithObject:itemValue forKey:itemKey];
    [defaults registerDefaults:defaultItem];
}

Get current value and Increment by one

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (int i = 0; i < 5; i++) {
    if (NeedToIncrementByOne[i]) {
        NSString *itemKey = [NSString stringWithFormat:@"item%d", i];
        NSInteger itemValue = [defaults integerForKey:itemKey] + 1;
        [defaults setInteger:itemValue forKey:itemKey];
    }
}

Types Support

setBool:forKey:
setDouble:forKey:
setFloat:forKey:
setInteger:forKey:
setObject:forKey:
setURL:forKey:
setValue:forKey:

Set Nil

setNilValueForKey:


沒有留言:

張貼留言