开发者

Storing preferences in NSUser Defaults to recall later

Been working on some code streamlining and have realised that it would be really helpful if my app had a preferences system.

Now here's how my code works.

A method runs based upon an integer stored in NSUserDefaults

e.g.

if ([[NSUserDefaults standardUserDefaults] integerForKey:@"scifi1"] == 040){
            [self spaceDown];
        }
        else if ([[NSUserDefaults standardUserDefaults] integerF开发者_如何学PythonorKey:@"scifi1"] == 10040){
            [self ctrldown];
            [self spaceDown];
        }

Now what I want to do is when I exit the view (via a specific button) is to dump the value of @"scifi1" into a new preference, say for example - an integer named @"savedscifi1"

Now I know how to save integers into NSUserDefaults,

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setInteger:VALUEHERE forKey:@"savedscifi1"];
    [userDefaults synchronize];

However - I'm not sure how I can substiture in the value of scifi1 instead of (in this case) 'VALUEHERE' - can anyone help with this? I feel it's really simple but I can't help but think I'm being a bit thick...sleep deprived and approaching a deadline! I know I can't just call up @"scifi1"but beyond that....??


NSInteger value = [[NSUserDefaults standardUserDefaults] integerForKey: ...];
[[NSUserDefaults standardUserDefaults] setInteger: value forKey: ...];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜