PDA

View Full Version : How to create property thats also a custom made class?




tradingjam
Aug 12, 2012, 09:01 AM
Hi all,

Does anyone know how to add a property in objective c - but that property is also a custom made class?

For instance, I made this class:

@interface Person : NSObject

@property NSString *personName;
@property NSNumber *personAge;

-(id)init;

@end

Where...

@implementation Person

@synthesize personAge, personName;

-(id)init{
self = [super init];

if(self){
self.personAge = [NSNumber numberWithInt:26];
self.personName = @"Jamie";
}
return self;
}

@end

So basically whenever I init & alloc the Person class, it gets setup with personAge as 26 and personName as Jamie.

Now I want to create a bank account class which contains a person property:

@interface BankAccount : NSObject

@property NSNumber *bankAccNumber;
@property (nonatomic) Person *thePerson;

-(id)init;

@end

Where...

@implementation BankAccount

@synthesize thePerson = _thePerson;
@synthesize bankAccNumber;

-(id)init{

self = [super init];

if(self){
bankAccNumber = [NSNumber numberWithInt:999];
}

return self;
}
@end

Now - my issue is this:

1) In the BankAccount class, where do I alloc & init the Person class?

Many thanks for your help in advance!



chown33
Aug 12, 2012, 12:55 PM
So basically whenever I init & alloc the Person class, it gets setup with personAge as 26 and personName as Jamie.

Why would you do that? Is every Person named Jamie, with an age of 26?

Also, an init method sets up an object. Specifically, it sets up a newly alloc'ed Person object. It does not setup the Person class. There's a huge difference between a class and an object, and if you don't see the distinction, you need to study that until it becomes clear.


The usual way to make a new object with different values is to define the class with an init method that takes parameters. Example:
-(id)initWithName:(NSString *)name andAge:(int)age;

Then you write code for that method so it does the normal [super init], then sets the values of the properties.

The same approach applies to your BankAccount class: define a method that takes a parameter. The parameter should be an object of the Person type.


If you're not using ARC, then your properties need either a copy or retain attribute, otherwise the memory management won't work correctly, and your program will fail (crash or simply not work).


The posted code seems like an exercise from a book or tutorial. Which one? Title, author, and book edition; or URL of tutorial.

If you're not learning from a book or tutorial, what are you learning from? I ask because the posted code suggests a lack of some important fundamentals. If you don't have the fundamentals (like memory management or how to write methods with parameters), you need to get a firm grasp of those before proceeding.

xStep
Aug 12, 2012, 01:26 PM
In your extremely simplified example I'd create the person object immediately after the line where you assign the account number. You could also do it after the line that creates the bank account object. Either if fine.

In a real system there are workflows that would influence the whole process. Too much to discuss here I think.

EDIT: By the way, you wouldn't enter age, you'd use birthday and calculate age off of that. First and last names and gender and, well more would be separated out.

I see chown33 couldn't stand your sample and added more than I chose too. I decided to guess your very new to programming and learning perhaps from an introduction course that hasn't gotten to parameters just yet. I agree that you also need to understand the distinction between class and object.

tradingjam
Aug 12, 2012, 03:12 PM
Thank you both for your replies.

In answer to your question I am fairly new to objective c / mac programming. The above is me coding to teach myself how everything fits together.

It is all very new to me - Im kind of learning as Im going along - the above is a combination from two books.

Once again, thanks for your help

chown33
Aug 12, 2012, 03:51 PM
It is all very new to me - Im kind of learning as Im going along - the above is a combination from two books.

Use one book at a time. Follow it through, in order. Do every exercise. In order. Don't skip around.

If you finish one book and feel you need the other, then do the second book in order.

The reason for not skipping around, or for not using two books at once is simple: presentation order.

Explanations and exercises are given in a specific order, so no exercise should require knowledge that hasn't been presented yet. Books are intentionally written this way. If you skip around, there's no logical order, and a random exercise may require you to know a concept you haven't read yet. Same thing for combining two books: the books have different presentation orders. So again you encounter exercises from one book that may require knowledge from that book, which hasn't been presented in the other.