简体   繁体   English

NSUserDefaults-存储基本设置

[英]NSUserDefaults - storing basic settings

I have a flipView application. 我有一个flipView应用程序。 On the FlipView I keep settings for the MainView and would like to use NSUserDefaults to store 2 integers for the MainView. 在FlipView上,我保留MainView的设置,并想使用NSUserDefaults为MainView存储2个整数。 I will use those integers in the main application. 我将在主应用程序中使用这些整数。 Is there some basic code that can implement what I'm trying to do? 是否有一些基本代码可以实现我要执行的操作?

I'd like to store the information on the FlipView (if changed) and have a default setting when the program is first run in the MainView. 我想将信息存储在FlipView上(如果已更改),并在MainView中首次运行该程序时具有默认设置。

Also, will I need to release the NSUserDefaults object once I'm finished using it? 另外,使用完我是否需要release NSUserDefaults对象?

check out below code, I had wrapped and make easy for you, 看看下面的代码,我已经打包好了,让您很轻松,

you can use below codes by: import "Settings.h" in any code where you want to use, (maybe AppDelegate.m) in your case. 您可以通过以下方式使用以下代码:在您要使用的任何代码中(可能是AppDelegate.m)导入“ Settings.h”。 then 然后

to set value, use 设定值,使用

[Settings setSetting:@"AUTOLOGIN" value:@"1"];
[Settings setSetting:@"OTHERPROPERTY" value:@"100"];

and remember that, store pointer datatype only, 并记住,仅存储指针数据类型,

to get setting value: 获取设置值:

[Settings getSetting:@"AUTOLOGIN"];

here below there are two files, Setting.h and Setting.m , make those 在下面有两个文件Setting.h和Setting.m,使它们

header file is : 头文件是:

#import <Foundation/Foundation.h>    
@interface Settings : NSObject {

}
+(id) getSetting:(NSString *)key;
+(void) setSetting:(NSString *)key value:(id)v;
@end

- and implementation file is -和实现文件是

#import "Settings.h"
@implementation GCCSettings
static NSMutableDictionary *settings = nil;

+(id) getSetting:(NSString *)key{
    if(settings == nil){
        settings = [[NSMutableDictionary alloc] initWithDictionary:
                    [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"settings"]
                    ];
    }
    return [settings objectForKey:key];
}

+(void) setSetting:(NSString *)key value:(id)v{
    [settings setValue:v forKey:key];
    [[NSUserDefaults standardUserDefaults] setObject:settings forKey:@"settings"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end

and finally don't release any objects, which you had not allocated using(new, init, retain etc). 最后不要释放任何您没有使用过的对象(新,初始化,保留等)。

You can store the integer with [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"theKey"] and retrive it with [[NSUserDefaults standardUserDefautls] integerForKey:@"theKey"]. 您可以使用[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@“ theKey”]存储该整数,然后使用[[NSUserDefaults standardUserDefautls] integerForKey:@“ theKey”]检索该整数。 That's really all you need to handle an integer with user defaults. 这实际上是使用用户默认值处理整数所需的全部。

You don't release the NSUserDefaults object. 您不会释放NSUserDefaults对象。 It's a singleton, so only one will be created for the life of your app. 这是一个单例,因此在您的应用程序生命周期内只会创建一个。 You also don't own it which means you wouldn't be releasing it. 您也不拥有它,这意味着您不会释放它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM