我來自更多的Android開發背景,所以如果這是一個愚蠢的問題,請道歉,但這只是令我頭腦發呆,而我看不出什麼是錯的。我有一個Singleton類實現如下:使用Singleton類更改選項卡會導致SIGABRT
頭文件
@interface SingletonClass : NSObject
{
}
@property(nonatomic, retain) NSMutableArray *categoryArray;
+ (SingletonClass *)sharedInstance;
- (id) init;
- (void)setCategory: (NSMutableArray *) x;
- (NSMutableArray *)getCategory;
@end
類實現:
#import "SingletonClass.h"
@implementation SingletonClass
@synthesize categoryArray;
static SingletonClass *sharedInstance = nil;
+ (SingletonClass *)sharedInstance {
if (sharedInstance == nil) {
sharedInstance = [[super allocWithZone:NULL] init];
}
return sharedInstance;
}
- (id)init
{
self = [super init];
if (self) {
categoryArray = [[NSMutableArray alloc] init];
}
return self;
}
+ (id)allocWithZone:(NSZone*)zone {
return [self sharedInstance];
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (void)setCategory: (NSMutableArray *)category_array{
categoryArray = category_array;
}
- (NSMutableArray *)getCategory{
return categoryArray;
}
@end
我有2個選項卡每個我試圖訪問其持有的Singleton對象我需要使用的陣列:
SingletonClass* myapp = [SingletonClass sharedInstance];
categories = [myapp getCategory];
當切換選項卡時它工作時單工對象沒有被調用,但一旦我使用它,我得到SIGABRT錯誤。 (認爲這是一個內存警告)。單標籤實例是否不能跨標籤共享?
感謝您的幫助。我正在使用ARC,所以只專注於你的b點,我已經嘗試殭屍儀器與殭屍啓用,但沒有被標記我只是得到相同的錯誤,沒有控制檯輸出它只是說gbd,XCode跳轉到main.m類並顯示SIGABRT錯誤。 – SamRowley 2011-12-23 15:18:11