I have a tab-bar application, im using sing开发者_如何转开发leton class to share some data through out my application. In my application delegate class in didFinishLaunchingWithOptions method I am getting my data from server and storing it in singleton class, and I can see that its been stored correctly in my singleton class using NSLog.
But now when, I am trying to access that data in my second tab-bar click its giving me “EXC_BAD_ACCESS”. my viewDidLoad code
- (void)viewDidLoad {
SingleTon *get = [SingleTon sharedInstanceHname];
result = [get getHname:result];
list = [[NSArray alloc] initWithArray:result];
NSLog(@"List%@", list);
[super viewDidLoad];
}
I can see that error (“EXC_BAD_ACCESS”) is at line
list = [[NSArray alloc] initWithArray:result];
I want to use this data to populate in second tab bar view...any suggestions how to resolve this error????
sharedInstance method
static SingleTon *_sharedInstanceHname;
- (id) init
{
if (self = [super init])
{
// custom initialization
//memset(board, 0, sizeof(board));
hname = [[NSMutableArray alloc] init];
index =1;
}
return self;
}
+ (SingleTon *) sharedInstanceHname
{
if (!_sharedInstanceHname)
{
_sharedInstanceHname = [[SingleTon alloc] init];
}
return _sharedInstanceHname;
}
- (NSMutableArray *) getHname:(NSMutableArray *)x
{
return hname;
}
What is the message getHname returning?
I suspect the 'result' is not a member of NSArray.
Another thought is perhaps 'result' is nil. I don't think the method initWithArray: like been given a nil value.
Do sharedInstanceHname
or gHname
return autoreleased objects? You may need to retain them for your use like so:
- (void) viewDidLoad
{
SingleTon *get = [SingleTon sharedInstanceHname];
[get retain];
result = [get gHname:result];
[result retain];
[get release];
list = [[NSArray alloc] initWithArray:result];
[result release];
[super viewDidLoad];
}
I wouldn't use the above code though; it serves only the purpose of demonstrating to you the concept of autorelease and retain/release ownership. Figure out what sharedInstanceHname
or gHname
return, and program accordingly.
EDIT: More importantly, does the sharedInstanceHname
method in the SingleTon class retain ownership of the object returned by that method? If it does return an autoreleased object, it isn't strictly a Singleton object and rather a constructor of sorts.
精彩评论