这是使用全局变量之类的最干净的方法?通常,禁止使用全局变量,但我不知道从不同类访问 NSUserDefaults 的更好解决方案。
我读了一点,想出了这个。我定义了一个 Contants.h 和一个 Constants.m 文件,并将它们包含在我需要的任何地方。
//Constants.h
#import <Foundation/Foundation.h>
@interface Constants : NSObject {
extern NSUserDefaults *settings;
}
@end
.
//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
这里的问题是我想为我的常量初始化一个值。我在 Constants.m 中没有方法。所以我的辅助变量也是全局变量?
有一件事要提:我认为全局变量也必须被释放?
谢谢你的帮助!
编辑:
@hotpaw2:
AppBundleSingleton.h:
#import <Foundation/Foundation.h>
@interface AppBundleSingleton : NSObject {
}
+ (AppBundleSingleton *)sharedAppBundleSingleton;
@end
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
在我的 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
在我的 ViewController 我查询值:
NSString *myString = [[NSUserDefaults standardUserDefaults] stringForKey:@"myKeyforString"];
那会是一个解决方案吗?