Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

childoftheko4n

macrumors 6502
Original poster
Oct 18, 2011
270
83
So i sat down and redid this and wanted to post an update. This all seems to be working as planned now:

Main:
Code:
#import <Foundation/Foundation.h>
#import "Asset.h"
#import "Employee.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSMutableArray *listOfEmployee = [[NSMutableArray alloc] init];
        
        int employeeCount = 0;
        
        for (employeeCount = 0; employeeCount < 10; employeeCount ++)
        {
        //create asset and employee isntances
        Employee *employee = [[Employee alloc] init];
        Asset *asset = [[Asset alloc] init];
        
        //set employeeID and asset number to ia
        employee.employeeID = employeeCount;
        asset.assetNumber = employeeCount;
       
        employee.employeeAsset = asset;
        
        [listOfEmployee addObject:employee];
        }
    
        //print listOfEmployee
       NSLog(@" %@", listOfEmployee);
   
    }
    return 0;
}

Employee.h
Code:
#import <Foundation/Foundation.h>
#import "Asset.h"


@interface Employee : NSObject
@property int employeeID;
@property Asset *employeeAsset;

@end

Employee.M
Code:
#import "Employee.h"
#import "Asset.h"

@implementation Employee
@synthesize employeeID;
@synthesize employeeAsset;


-(NSString *) description
{
    return [NSString stringWithFormat:@"Employee ID:%i with %@", employeeID,employeeAsset];
}

@end

Asset.h:
Code:
#import <Foundation/Foundation.h>

@interface Asset : NSObject

@property NSString *assetLabel;
@property int assetNumber;

@end

Asset.m:
Code:
#import "Asset.h"

@implementation Asset
@synthesize assetLabel;
@synthesize assetNumber;

-(NSString *) description
{
    return [NSString stringWithFormat:@"Laptop Number:%i",assetNumber];
}

@end

Besides everything appearing to be working now, how does it look? I know utilizing data encapsulation was recommended a few times so i wanted to look into that more. Would you hide any of my code in main in their respective classes?

I was thinking if i did an Employee method something along the lines of
(Employee*) configEmployee
Would let me alloc/init the employee, along with set the employeeID, then return that employee to main where configAsset would do similar. They woudl then be connected in main.

However i couldn't figure out how to actually return an employee instance from an Employee method, or if that would even be recommended over my code above?

Thanks again! you all have be such a help!
 

hollersoft

macrumors regular
Feb 10, 2013
100
64
Looks pretty good to me :)

You could possibly hide some more stuff in the Employee and Asset classes, but what you've got looks OK. if you did do more hiding, you'd make so-called "factory methods" in those classes. You declare such static methods with a + instead of a -. This pattern is used a fair bit in the foundation classes, e.g.

+(NSString *)stringWithFormat( ... )

In the NSString class.
 

childoftheko4n

macrumors 6502
Original poster
Oct 18, 2011
270
83
Looks pretty good to me :)

You could possibly hide some more stuff in the Employee and Asset classes, but what you've got looks OK. if you did do more hiding, you'd make so-called "factory methods" in those classes. You declare such static methods with a + instead of a -. This pattern is used a fair bit in the foundation classes, e.g.

+(NSString *)stringWithFormat( ... )

In the NSString class.

ah, ok that would make sense. I'll probably just leave it as is for purposes of learning and keeping on track with my book for now :)

Still have to add a few things to it to test out but you have been major help in sorting this concept out in my head.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.