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

How to create a property that belongs to another class

I've tried @property (nonatomic) float ruby;

and I've tried:

#import "Ruby.h"

@interface Bling : NSObject {

@property float ruby;
@end
Gem.h
@interface Gem : NSObject
  @end
Ruby.h
@interface Ruby : Gem
  @end
Bling.h
#import "Ruby.h"

@interface Bling : NSObject {
  float ruby;
}
@property float ruby;
@end

1 Answer

cm21
cm21
150,854 Points

Hi Shaun,

You want ruby to be a pointer (to give an example of how pointers work: a pointer holds a memory address--thus, creating a pointer allows the programmer to indirectly operate on the data that is stored.)

You'll also want to specify nonatomic and strong for the ruby property. The reasons for doing this are a bit more complex, but to provide generalized reasons as to why we might sometimes do this: Apple's Documentation (https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html) specifies that having at least one strong reference helps keep an object alive, and that accessing a nonatomic property is faster of a process than accessing an atomic one (there are reasons to specify atomic, but there's no need to do that here.)

@interface Bling : NSObject

@property (nonatomic, strong) Ruby *ruby;
@end

Hope this helps!