My purpose: making an API call to a server, and getting back from them is an array of data named dataArr and I want to store these data to another array for later need.
What I am doing so far is
myClass.h:
@propery ( nonatomic, retain ) NSArray *dataList;
myClass.m:
@implementation myClass
-(void)receivedData:(NSArray*) dataArr {
// ???
}
To fill in at line 3, I have two options, option A:
dataList = dataArr;
or option B:
[dataList release];
[dataArr retain];
dataList = dataArr;
I think option A is the right way to do it because dataList is declared as retain in the header file. Therefore, the setter will make sure to release a current array (dataList) and reain a received array (dataArr) as well
I just want to double check that I am on the right path.
Please correct me if I have just made a mistake in the middle. Thanks
Any comments are welcomed.
this is not valid Objecitve-C. If you wanted to write
that’s still a no-go, as you’re acessing the instance variable directly, not through the property setter, that is, your array won’t be retained and it will badly crash.
is wrong again. If
dataListwas the same asdataArr, and the reference of the object (self) was the last reference to it, then it would get deallocated, breaking the followingretainmessage, and most likely crashing again.If you have a property setter (which you have), simply write
this will retain the array correctly. By the way, the implementation of the setter is something like your last method, but it checks either for inequality:
or pre-
retains the object to be set: