Quote:
Originally Posted by Ghigo
Sorry I made a mistake in the second solution. It is:
Code:
- (id) init {
if (self = [super init])
{
[myData addObject:obj1];
[myData addObject:obj2];
[myData addObject:obj3];
}
return self;
}
I wanted to refer directly to myData without using an intermediate array.
The problem is that if i declare myData inside the init function, it will hide the instance variable.
Should I declare the instance variable in some ways? How?
Should I refer to it with self.myData?
|
This is still wrong because myData does not yet exist. You can't call addObject on an array that does not exist. You need to use the alloc and init that you had in your first solution.
You are mistaken if you think the "a" created in your first solution was in intermediate array. "a" is just a pointer. "myData" is also a pointer. In fact, most Objective-C objects are referred to only though pointers. So when you say
Code:
NSMutableArray* a = [[NSMutableArray alloc] init];
This allocates some memory for the new array, and then puts a pointer to that memory in the pointer variable, "a". Then you added things to that array using "a" to reference the object. Then when you say
you are merely making myData point at the same array that "a" was pointing at. This does not create a second array. There is still only one array involved. The difference between the pointer "a" and the pointer "myData" is that the first one is a local automatic variable that will go away after the method exits, while the second one is an instance variable of the class and it will stick around for as long as the class sticks around. This has nothing to do with when the array itself goes away - only when these pointers go away.
So while your first solution might work, it is not optimal. That is because you don't take advantage of the fact that myData is declared to be a retained property. Using myData directly , especially setting it directly, risks leaking objects. When you make myData point somewhere, what happens to the object it was pointing at before (if any)? It becomes an orphan, with nobody pointing at it. So it can never be deallocated, and that is the definition of a memory leak. So the correct way to assign myData is to use the setter method that is created when you say
Code:
@synthesize myData;
which I hope you did at the top of your @implementation block. Then you can say:
and that will take care of all the housekeeping required for releasing old objects before assigning a new one. Of course in an init method, this is not really a problem because you only call init once for a given object, and
does the same thing as the previous code line on the very first assignment. Even though it might be OK to assign myData directly in this case, it is a bad habit to get into to use instance variables directly. You would be better off always referring to retained properties by means of their getter and setter methods, which are invoked when you use self.myData instead of myData.
Robert Scott
Ypsilanti, Michigan