Please help with understanding how #import works without using inheritance.
So I have created a Class named Person and another Class named Body (both classes are inherited from NSObject).
The Body class has 3 ivars (set with properties)
int hands;
int feet;
int legs;
I then imported and created an instance of the Body class ( *body) into the Person class. My understanding is that I have now made a ‘type’ from the Body class. This ivar was then set with properties.
In main, I then created an instance from the Person class named person.
Main recognizes person.body.feet but I cannot get and set its value directly. The only way it can be done is passing its value into a new int variable and then print out the new variable.
Obviously I’m a struggling newbe but I really want to understand this problem –
#import <Foundation/Foundation.h>
#import "Person.h"
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Person *person = [[Person alloc]init];
// this works and NSLog prints out the value is 2
int feet = person.body.feet = 2;
NSLog(@"person.body = %i",feet);
// this does not work - NSLog prints out the value is 0;
[person.body setFeet:2];
NSLog(@"person.body = %i", person.body.feet );
[person release];
[pool drain];
return 0;
}
#import <Foundation/Foundation.h>
#import "Body.h"
@interface Person : NSObject {
Body *body;
}
@property (assign) Body *body;
@end
#import "Person.h"
@implementation Person
@synthesize body;
@end
#import <Foundation/Foundation.h>
@interface Body : NSObject {
int hands;
int feet;
int legs;
}
@property int hands;
@property int feet;
@property int legs;
@end
#import "Body.h"
@implementation Body
@synthesize hands;
@synthesize feet;
@synthesize legs;
@end
My guess is that you haven’t assigned a
Bodyobject toperson.bodyanywhere.All objects in Objective-C are allocated on the heap. All those asterisks denote that the variables are pointers, which are mere numbers pointing to memory locations where the objects live. When you declare a property that holds an object, all you are really doing is creating a place to store a pointer to the object. You aren’t creating the object itself.
So somewhere (most likely in
Person‘s designated initialiser), you’ll want to actually create aBodyobject and assign it to thebodyproperty.If you don’t do this, then the memory location will remain at
0, which is equivalent tonil. When you chain the assignment here:…you’re assigning
2to bothfeetandperson.body.feet, but sinceperson.bodyisnil, the setter message to it is ignored. When later you try to logperson.body.feet, again, it will beniland returnnil/0.Edit: Having looked at your update, yes, this is your problem.
This line does not create a
Bodyobject:It creates an instance variable for a pointer to a
Bodyobject.This line does not create a
Bodyobject:It declares a property, which is syntactic sugar for getter and setter method declarations.
This line does not create a
Bodyobject:It synthesises your previously declared property by creating the getter and setter methods.
You need to actually create and initialise a
Bodyobject and then assign it to yourPersonobject’sbodyproperty. For instance:Also, as you aren’t using ARC, you should almost certainly declare that property to be
retainnotassign, and release it in yourPerson‘sdeallocmethod.