sayguh
sayguh

Reputation: 2550

Access the property of an object within an object?

I have a custom "brain" class that has a custom "recipe" object as one of it's properties.

The recipe class has four "ingredient" objects as properties.

If I try and do:

brain.myRecipe.ingredient1 = myIngredient;
self.displayLabel.text = brain.myRecipe.ingredient1.ingredientName;

The label is blank (although I get no errors)

but if I do

Ingredient * temp = myIngredient;
self.displayLabel.text = temp.ingredientName;

That one works... Are you not able to drill down through properties like that with the dot operator?

Thanks!

Upvotes: 0

Views: 77

Answers (2)

Oliver
Oliver

Reputation: 23540

Check if brain is not nil.

If not :

Check myrecipe and ingredient1 properties ? Are they set on retain ?
If not, put retain.

Check @synthesize for both. Aren't there any type mistake so their name would not match the one set fo the properties and the ivars ?
If there are mistakes (lokk ate upper/lowercases), correct.

I also guess that Ingredient inherits from NSObject (at least) and have [super init] on the begining of its init method ?
If not, do you class inherit NSObject, and init it first.

If nothing works... then, just put some more code. How do you want us to solve your problem with such a little piece of code ?

You should have something like :

Brain : NSObject {
   MyReceipe* receipe;
}

@property (nonatomic, retain) MyReceipe* receipe;



MyReceipe : NSObject {
   Ingredient* ingredient1;
}

@property (nonatomic, retain) Ingredient* ingredient1;



Ingredient : NSObject {
   NSString* ingredientName;
}

@property (nonatomic, retain) NSString* ingredientName;

in all the .m, add @synthsize the_property_name

and an init method like

- (id) init {
   self = [super init];
   if (!self) return nil;

   self.the_ivar = nil; (or whatever you want)

   return self;
}

Upvotes: 1

progrmr
progrmr

Reputation: 77251

Yes, you can do that with the dot operator. Most likely one of those properties is nil.

Upvotes: 1

Related Questions