Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alex Atwater
2,624 PointsQuestion with "*" in code and ()
@interface Sphere : NSObject {
NSArray *_center;
float _radius;
}
-(void)setCenter:(NSArray *)center; -(NSArray *)center; -(void)setCenter:(NSArray *)center radius:(float)radius;
-(void)setRadius:(float)radius; -(float)radius;
@end
There is some code from the video. Questions:
Why is _center being declared as NSArray *_center with a *? Why isn't float _radius have one? What's the purpose of it being there?
In the declarations of methods for center, why is the return type (NSArray )? does that mean the return type is going to be a pointer of an NSArray? or a location of an NSArray? and why is there space? could it be (NSArray)?
3 Answers
Justin Johnson
Courses Plus Student 24,793 PointsI don't know if anyone can answer better but... to my knowledge _center has a pointer because NSArray is an object where as float _radius is a C primitive data type so it doesn't require a pointer ( because it is not an object );
Justin Johnson
Courses Plus Student 24,793 Pointsoh man I ended a sentence with a semi-colon. I'm literally turning into a robot.
christo
3,525 Points(meant to post this as an answer, not a comment, but can't edit/delete the comment)
I'm no expert at this yet, but I'll have a go based on my understanding. Someone please correct me if I'm wrong!
- Justin is right, but I'll expand a little more: an * can have a few different functions. For example, an * is actually an operator to de-reference a pointer. Only during variable declaration does * mean, "Look at me, I'm a pointer!!"
Foo *foo // foo in this case is a pointer to a Foo object
- The method you reference in your question:
- (NSArray *) center;
This method does not have a space, it has an *. The code would not compile without an *, so no, (NSArray) would not work, but to answer you question, yes, what will be returned is a pointer to an NSArray that was created in the "center" method in the Sphere class. You asked, "or a location of NSArray"...sort of. It's pointing to a particular memory address location. So if you have code like:
Sphere *mySphere = [[Sphere alloc] init]; // creates a Sphere object (assuming it's initialized properly)
NSArray *objectCenter = [mySphere center];
you have a Sphere object called "mySphere." You can then perform the instance method "center" that will return an NSArray with something in it. In this case, "objectCenter" is an NSArray pointing to the same memory address that's storing the NSArray that was created in the "center" method. But overall, only one NSArray was created, but we have two pointers to it:
- [mySphere center]
- objectCenter
You could just do [mySphere center] every time you wanted to access that NSArray, but it kind of looks funny to do that all the time and doesn't do much for readability. By assigning [mySphere center] to another NSArray pointer (in this case objectCenter), you can do things like:
[objectCenter objectAtIndex: 0];
rather than
[[mySphere center] objectAtIndex:0];
Hope what I said made sense. Good luck!
-Christo
Scott Magdalein
1,667 PointsTesting something in the forum again. Please disregard.
christo
3,525 Pointschristo
3,525 PointsI'm no expert at this yet, but I'll have a go based on my understanding. Someone please correct me if I'm wrong!
an * can have a few different functions. For example, an * is actually an operator to de-reference a pointer. Only during variable declaration does * mean, "Look at me, I'm a pointer!!"
```Foo *foo // foo in this case is a pointer to a Foo object
objectivec -(NSArray *)center;This method does not have a space, it has an *. The code would not compile without an *, so no, (NSArray) would not work, but to answer you question, yes, what will be returned is a pointer to an NSArray that was created in the "center" method in the Sphere class. You asked, "or a location of NSArray"...sort of. It's pointing to a particular memory address location. So if you have code like:you have a Sphere object called "mySphere." You can then perform the class method "center" that will return an NSArray with something in it. In this case, "objectCenter" is an NSArray pointing to the same memory address that's storing the NSArray that was created in the "center" method. But overall only on NSArray was created, but we have two pointers to it:
You could just do [mySphere center] every time you wanted to access that NSArray, but it kind of looks funny to do that all the time. By assigning [mySphere center] to another pointer (in this case objectCenter), you can do things like:
[objectCenter objectAtIndex: 0];rather than
[[mySphere center] objectAtIndex:0];Hope what I said made sense. Good luck!
-Christo