Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Christopher Ruggles
Christopher Ruggles
1,253 Points

How can property be designated to a class?

I am trying to answer this challenge. Now sure how to designate a class for a property.

Finally, we have a class named 'Bling' which needs to have a property called 'ruby'. Switch over to 'Bling.h' and add a property named 'ruby' that belongs to the class 'Ruby'.

2 Answers

A property can be created with @property, and it goes between the @interface and @end similar to this video at around 1:35.

Here is another video about @property.

Michael Hulet
Michael Hulet
47,843 Points

Above the @interface declaration, you need to tell the compiler what you're talking about when you declare your @property later on. This is done using @class, in this case. When you do that, the compiler will know what you're talking about later on. This code for that file passes:

//The below line is where you tell the compiler about the Ruby class. It's kinda like using #import "Ruby.h", but this way, you don't unnecessarily get a bunch of stuff from Ruby
@class Ruby;

@interface Bling : NSObject
//Here is where you declare your @property. From left to right, saying it's a property automatically gives you an instance variable, and setter and getter methods for that variable. What's in the parentheses says that it's not thread-safe (which has to do with multithreading) and tells the system to hold a strong pointer to it. You'll learn more about what all that means later. Then, you're saying that it's an instance of the Ruby class, and finally naming the property "ruby"
@property (nonatomic, strong) Ruby *ruby;

@end