我創建了一個單例類來跟蹤我的iPhone應用程序中的數據。我知道singleton只需要實例化一次,但是實例化它的最佳位置是什麼?這應該在appDelegate中完成嗎?我想能夠從多個類中調用這個單例(包含一個NSMutableArray),以便我可以訪問該數組。實例化單例類
這裏是我的課我寫道:
#import "WorkoutManager.h"
static WorkoutManager *workoutManagerInstance;
@implementation WorkoutManager
@synthesize workouts;
+(WorkoutManager*)sharedInstance {
if(!workoutManagerInstance) {
workoutManagerInstance = [[WorkoutManager alloc] init];
}
return workoutManagerInstance;
}
-(id)init {
self = [super init];
if (self) {
workouts = [[NSMutableArray alloc] init];
}
return self;
}
@end
看看這個:http://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like – 2012-04-11 00:46:12
或這樣的:HTTP:// stackoverflow.com/questions/7568935/how-do-i-implement-an-objective-c-singleton-that-is-compatible-with-arc – 2012-04-11 00:47:03
鏈接似乎是關於實施一個單身人士。我已經有了我的書面和運作。我在我的一個類中的一個IBAction中實例化它,但是我很困惑我是否應該在別處實例化它以便它是全局的? – TopChef 2012-04-11 00:49:17