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

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

Chase Marchione
Chase Marchione
155,055 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!