why iphone crashes on direct field (not by getters/setters) acces
[sorry for my weak english]
If I create some NSArray of strings (as a member field of an object - no property no synthesize),
and initialise it in -viewdidLoad with some strings by
names = [NSArray arrayWithObjects: @"jeden", @"dwa", @"trzy", nil];
and immediately (also in -viewDidLoad) will use it, it is okay.
But when I try use it and its data a little later, in button event handler, it shows that that array data is corrupted (crashes even on [names count];)
It shows that i need to add property and synthesize and use it by self.names and then it seem to work... but it is confusing and sad for me because I do not know what is the reason thet first way of using the member (no self开发者_JAVA百科. no property and synthesize) do not work
could anyone explain,
TIA, fir
names = [NSArray arrayWithObjects: @"jeden", @"dwa", @"trzy", nil];
makes an autorelease reference to the object, you need either (both of them increment the retain count by 1):
make a retain of the object, i mean:
names = [[NSArray arrayWithObjects: @"jeden", @"dwa", @"trzy", nil] retain];
or use a new allocation of them, i prefer this approach:
names = [[NSArray alloc] initWithObjects: @"jeden", @"dwa", @"trzy", nil];
Then in dealloc section don't forget to release it, in order to avoid a memory leak.
Cheers!
You should initialize it properly to have it correctly retained:
names = [[NSArray alloc] initWithObjects:@"jeden", @"dwa", @"trzy", nil];
精彩评论