Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 12, 2012, 09:01 AM   #1
tradingjam
macrumors newbie
 
Join Date: Nov 2008
How to create property thats also a custom made class?

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:

Code:
@interface Person : NSObject

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

-(id)init;

@end
Where...

Code:
@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:

Code:
@interface BankAccount : NSObject

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

-(id)init;

@end
Where...

Code:
@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!
tradingjam is offline   0 Reply With Quote
Old Aug 12, 2012, 12:55 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by tradingjam View Post
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:
Code:
-(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.

Last edited by chown33; Aug 12, 2012 at 01:05 PM.
chown33 is offline   0 Reply With Quote
Old Aug 12, 2012, 01:26 PM   #3
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
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.
__________________
My App: CameraTime - Time lapse photography for novice and advanced users.

Last edited by xStep; Aug 12, 2012 at 01:36 PM.
xStep is online now   0 Reply With Quote
Old Aug 12, 2012, 03:12 PM   #4
tradingjam
Thread Starter
macrumors newbie
 
Join Date: Nov 2008
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
tradingjam is offline   0 Reply With Quote
Old Aug 12, 2012, 03:51 PM   #5
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by tradingjam View Post
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.
chown33 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
objective c, property

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How do I reference a subview in a custom UIView class? moonman239 iPhone/iPad Programming 1 Nov 7, 2013 01:38 PM
How to have user create class instance Abrexas iPhone/iPad Programming 21 Mar 28, 2013 12:54 PM
new objective C-class won't create .h file OriginalJef iPhone/iPad Programming 2 Mar 3, 2013 03:24 PM
call custom objc class from python ruchi830 Mac Programming 1 Jan 14, 2013 08:33 PM

Forum Jump

All times are GMT -5. The time now is 06:49 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC