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

iOS Objective-C Basics (Retired) Introduction to Objective-C Inheritance

Matt Kalebic
Matt Kalebic
1,247 Points

Why do we need to refer to a pointer and class "Ruby *ruby", unlike Circle's "float radius"?

Whereas in Circle we defined a property as: @interface Circle : Shape @property(nonatomic) float radius; @end

Here we define it as: @interface Bling : NSObject @property(nonatomic, strong) Ruby *ruby; @end

Why do we use a class (Ruby) instead of a type (float), and why are we referring to a pointer? I feel like I'm not quite understanding properties despite watching the video - it's taking a while to get used to Obj-C! If I understand a property correctly, it's a way to describe characteristics of an object (radius of a circle, color of a dog, etc.) so I'm not sure how "Ruby *ruby" makes sense.

Thank you!

Matt

1 Answer

A pointer points to an object on the heap. In the case of float, it is a primitive type, and in this case not on the heap; so a pointer is not needed to reference it because the object housing it owns the value. The property declaration does not decide whether or not you use a pointer or not. A property is just a shortcut to creating your method in the .h and .m file and it also creates instance variables for those methods. When creating a property for *ruby, you will get a getter and setter methods getRuby and ruby (the getter), but also an instance variable _ruby. Basically, it saves you time on typing the methods getRuby and setRuby, and making the implementations for them. Ruby *ruby makes sense because in Objective-C, Bling cannot own Ruby. Bling can only point to the location in memory when Ruby exists, and grab the information it needs from there.