@界面
//
// Created by macbook on 31.05.12.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import <Foundation/Foundation.h>
@interface CESettings : NSObject
+ (CESettings *)sharedInstance;
- (void)save;
@end
@执行
//
// Created by macbook on 31.05.12.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import "CESettings.h"
@interface CESettings ()
@property(nonatomic, strong) NSUserDefaults *userDefaults;
@end
@implementation CESettings
@synthesize userDefaults = _userDefaults;
#pragma mark - Singleton
static CESettings *_instance = nil;
+ (CESettings *)sharedInstance {
@synchronized (self) {
if (_instance == nil) {
_instance = [self new];
}
}
return _instance;
}
- (id)init {
self = [super init];
if (self) {
self.userDefaults = [NSUserDefaults standardUserDefaults];
}
return self;
}
#pragma mark - Methods
- (void)save {
[self.userDefaults synchronize];
}
@end
我有一个用于应用程序设置的类。该类有一个创建单例的方法和一个 init 方法。两者有什么用..?我认为如果存在 sharedInstance 方法,则不需要 init ......如果我错了,请纠正我.. 任何帮助表示赞赏。