如何定义NSUserDefaults类型的全局变量并初始化值?

tes*_*ing 0 iphone xcode objective-c globals

这是使用类似全局变量的最干净的方法吗?通常,禁止使用全局变量,但我不知道从不同类访问NSUserDefaults的更好解决方案.

我读了一下并想出了这个.我定义了一个Contants.h和一个Constants.m文件,并将它们包含在我需要的任何地方.

 //Constants.h
 #import <Foundation/Foundation.h>


 @interface Constants : NSObject {
  extern NSUserDefaults *settings;
 }

 @end
Run Code Online (Sandbox Code Playgroud)

.

 //Constants.m
 @implementation Constants

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
 NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
 [[NSUserDefaults standardUserDefaults] registerDefaults:settingsDict];
 NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];

 @end
Run Code Online (Sandbox Code Playgroud)

这里的问题是我想将值初始化为常量.我在Constants.m中没有方法.所以我的辅助变量也是全局变量?

有一点需要提及:我认为全局变量也必须发布?

谢谢你的帮助!

编辑:

@ hotpaw2:

AppBundleSingleton.h:

#import <Foundation/Foundation.h>


@interface AppBundleSingleton : NSObject {

}

+ (AppBundleSingleton *)sharedAppBundleSingleton;

@end
Run Code Online (Sandbox Code Playgroud)

AppBundleSingleton.m:

#import "AppBundleSingleton.h"


static AppBundleSingleton *sharedAppBundleSingleton = nil;


@implementation AppBundleSingleton

#pragma mark -
#pragma mark Singleton methods

+ (AppBundleSingleton *)sharedAppBundleSingleton {
    @synchronized(self) {
        if (sharedAppBundleSingleton == nil) {
            sharedAppBundleSingleton = [[self alloc] init];
        }
    }
    return sharedAppBundleSingleton;
}

+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (sharedAppBundleSingleton == nil) {
            sharedAppBundleSingleton = [super allocWithZone:zone];
            return sharedAppBundleSingleton;  // assignment and return on first allocation
        }
    }
    return nil;  // on subsequent allocation attempts return nil
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;  //denotes an object that cannot be released
}

- (void)release {
    //do nothing
}

- (id)autorelease {
    return self;
}

-(id)init {
    self = [super init];
    sharedAppBundleSingleton = self;
    // Initialization code here
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
    NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
    [[NSUserDefaults standardUserDefaults] registerDefaults:settingsDict];

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

在我的AppDelegate.m中,我有以下内容:

// ...
#include "AppBundleSingleton.h"


@implementation MyAppDelegate

// ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the navigation controller's view to the window and display.
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];

    [AppBundleSingleton sharedAppBundleSingleton];

    return YES;
}
// ...
@end
Run Code Online (Sandbox Code Playgroud)

在我的ViewController中,我查询值:

NSString *myString = [[NSUserDefaults standardUserDefaults] stringForKey:@"myKeyforString"];
Run Code Online (Sandbox Code Playgroud)

这会是一个解决方案吗?

Toa*_*tor 5

您不需要使用全局变量 - 您可以从项目中的任何类或对象访问userdefaults,如下所示:

BOOL foo = [[NSUserDefaults standardUserDefaults] boolForKey:@"bar"];
Run Code Online (Sandbox Code Playgroud)

只要您在每次更改后同步userdefaults,以这种方式获取的数据都是一致的.