Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2009, 12:16 PM   #1 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Italy
Posts: 11
Question Problems with property

Hello everybody, I just started doing my first steps with objC and this is my first post here.

I made a new class with a property:
Code:
@interface DataProvider : NSObject {
	NSMutableArray* myData;
}

@property (retain) NSMutableArray* myData;

@end
Now I want to put some data in the array:
Code:
- (id) init {
	if (self = [super init])
	{
		NSMutableArray* a = [[NSMutableArray alloc] init];
		[a addObject:obj1];
		[a addObject:obj2];
		[a addObject:obj3];

		myData = a;
	}
	return self;
}
why it doesn't work if i write?
Code:
- (id) init {
	if (self = [super init])
	{
		NSMutableArray* a = [[NSMutableArray alloc] init];
		[myData addObject:obj1];
		[myData addObject:obj2];
		[myData addObject:obj3];
	}
	return self;
}
Is there a better way to obtain the same result?
Thanks!
Ghigo is offline   Reply With Quote
Old 11-11-2009, 12:53 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

Quote:
Originally Posted by Ghigo View Post
...why it doesn't work if i write?
Code:
- (id) init {
	if (self = [super init])
	{
		NSMutableArray* a = [[NSMutableArray alloc] init];
		[myData addObject:obj1];
		[myData addObject:obj2];
		[myData addObject:obj3];
	}
	return self;
}
This does not work because myData has not yet been instantiated. That is, there is no object in myData yet. You did an alloc and init for a NSMutableArray and then put it in a local variable called "a". But then you did not use that object. You cannot add objects to a NSMutableArray unless you first create the object. That is why the code block before this one works. You created "a", added things to it, and then assigned the result to myData.

Robert Scott
Ypsilanti, Michigan
RLScott is offline   Reply With Quote
Old 11-11-2009, 01:03 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Italy
Posts: 11
Default

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?

Thanks
Ghigo is offline   Reply With Quote
Old 11-11-2009, 05:13 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,526
Default

Quote:
Originally Posted by Ghigo View Post
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
Code:
myData = a;
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:
Code:
  self.myData = a;
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
Code:
myData = a;
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
RLScott is offline   Reply With Quote
Old 11-11-2009, 05:43 PM   #5 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 52
Default

Code:
self.myData = a;
You should follow this up with releasing "a" to eliminate a leak.

Code:
          self.myData = a;
          [a release]
Iphoneer is offline   Reply With Quote
Old 11-12-2009, 08:49 AM   #6 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Italy
Posts: 11
Default

Thank you very much!

Quote:
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.
I'll take note of this!
Ghigo is offline   Reply With Quote
Reply

Bookmarks

Tags
array, property

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 249
21 members and 228 guests
ADY, beleg_1998, Dani77, diyora, FAED, fredidf, F_Bryant, iDifferent, JamesCahall, JasonR, mer10, Oral B, prchn4christ, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:09 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0