Understanding Creating the Relationships:


#1

So i was having some troubles with this chapter in getting a clear understanding of creating the relationships between values in difference classes. What i tried to do was reproduce a section of the sample code that pertained to only that function, and create it in a dumb down way almost.

This is what i have:

main.m:

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

    @autoreleasepool {
        
        //create employee list array
        NSMutableArray *employeeList = [[NSMutableArray alloc] init];
        
//create 10 employees, assign their employee ID, and add to employee list array
        for (int i = 1; i <11; i++)
        {
            Employee *employeeInstance = [[Employee alloc] init];
            [employeeInstance setEmployeeID:i];
            [employeeList addObject:employeeInstance];
            
            
        }
        
        
      //create 10 assets (laptops)
        for (int i =1; i<11; i++)
        {
            Asset *assetInstance = [[Asset alloc] init];
            NSString *assetNumber =[NSString stringWithFormat:@"Laptop:%i", i];
            [assetInstance setAssetLable:assetNumber];
            
            //find employee ID number that matches asset number (i?)
            Employee *employeeNumbered = [employeeList objectAtIndex:i];
            
            //assign the asset to the employee
            ????????????????

Employee Interface

@interface Employee : NSObject
@property int employeeID;

Employee Implementation

@implementation Employee
@synthesize employeeID;

Asset Interface

@interface Asset : NSObject
@property NSString *assetLable;

Asset Implementation

@implementation Asset
@synthesize assetLable;

So when it comes time to linking the asset object at index i, to the employee at index i, i would have to create an asset pointer within the employee class. This would have to be an NSString rigtht?

Im not quite sure on the coding for the function to be hoenst.
@property Asset *laptop; ?


#2

[quote]So when it comes time to linking the asset object at index i, to the employee at index i, i would have to create an asset pointer within the employee class. This would have to be an NSString rigtht?
[/quote]
No.

[quote]I’m not quite sure on the coding for the function to be hoenst.
@property Asset *laptop; ?
[/quote]
Yes, you meant an Asset property:

...
@class Asset;
@interface Employee : NSObject
@property int employeeID;
@property (strong) Asset *laptop;
...

However, keeping an asset list rather than a single asset affords much more flexibility:

...
@interface Employee : NSObject
@property int employeeID;
@property (strong) NSMutableArray *assetList;
...

#3

So i tried to update things alittle, and add an asset property to employee class (the first route you listed, just for ease of testing first). But i am still having some issues. Currently this set up is getting me “Employee ID has Laptop null”, so im not setting the asset labels correctly, or pointing correctly:

Main.M

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


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

    @autoreleasepool {
        
        //create employee list array
        NSMutableArray *employeeList = [[NSMutableArray alloc] init];
        
     //create 10 employees, assign their employee ID, and add to employee list array
        for (int i = 0; i <11; i++)
        {
            
            Employee *employeeInstance = [[Employee alloc] init];
            [employeeInstance setEmployeeID:i];
            [employeeList addObject:employeeInstance];
            
            
        }
        
        
      //create 10 assets (laptops)
        for (int i =0; i<11; i++)
        {
            Asset *assetInstance = [[Asset alloc] init];
            NSString *assetIdentifier =[NSString stringWithFormat:@"Laptop:%i", i];
            [assetInstance setAssetLabel:assetIdentifier];
            

            
             Employee *employeeToGetAsset = [employeeList objectAtIndex:i];
            [employeeToGetAsset addAssetToEmployee:assetInstance];
            
            
            
        }
 
        
        for (Employee *employee in employeeList)
        {
            NSLog(@"Employee ID: %i has %@", employee.employeeID, employee.assetLabel);
        }
        

        
    }
    return 0;
}

Employee.h

[code]#import <Foundation/Foundation.h>
#import “Asset.h”

@interface Employee : NSObject
@property int employeeID;

@property (strong) Asset *assetLabel;

@property NSMutableArray *arrayOfOneEmployeeAssets;
-(void) addAssetToEmployee: (Asset *)a;

@end
[/code]

Employee.m

[code]#import “Employee.h”
#import “Asset.h”

@implementation Employee
@synthesize employeeID;
@synthesize arrayOfOneEmployeeAssets;

-(void) addAssetToEmployee: (Asset *)a
{
if (!arrayOfOneEmployeeAssets)
{
arrayOfOneEmployeeAssets = [[NSMutableArray alloc] init];
}
[arrayOfOneEmployeeAssets addObject:a];
}

@synthesize assetLabel;

@end
[/code]

Asset.h

[code]#import <Foundation/Foundation.h>

@interface Asset : NSObject
@property NSString *assetLabel;

@end
[/code]

Asset.M

[code]#import “Asset.h”

@implementation Asset
@synthesize assetLabel;

@end
[/code]

Any pointers on the corrections i need to make?


#4

That’s because you are not setting the assetLabel. Although you are adding the asset to the list, you are not setting the assetLabel property.

...
            Employee *employeeToGetAsset = [employeeList objectAtIndex:i];
            [employeeToGetAsset addAssetToEmployee:assetInstance];
            employeeToGetAsset.assetLabel = assetInstance;  // <--- This is missing
...

Your property names in Employee are confusing. Why not use asset instead of assetLabel, and assetArray or assetList instead of arrayOfOneEmployeeAssets.


#5

Oh that definitely makes sense. So now when i add that i get
Employee ID 1 has <Asset: ox10010b510>

What variable would formatting need to be applied to inorder for that to read
Employee ID 1 has Laptop 1


#6

Hey Ibex,

I hate to ask, but i was reading a topic on this chapter where you were helping someone out create a to-one relationship between couple - members. The way you did it and kept it simple really made a lot of sense.

Is there any chance you could do the same for a simple to-many relationship? Maybe a single parent - children amongst group of children. Or if to compare to my problem a single employee - multiple assets in a group of even more assets?


#7

A simple one-to-many relationship:

@interface Employee: Person
...
@property (strong) NSMutableArray *assets;
...
@end

A not-so-simple one-to-many relationship:

@interface Employee: Person
...
@property (strong) NSMutableDictionary *assetGroups; 
                            // {key: nameOfAssetGroup, value: NSMutableArray *}
...
@end