Cant understand the code


#1

Can anyone please explain me what does this code do (taken from 2nd edition):

[code]#import “BNREmployee.h”
#import “BNRAsset.h”

@implementation BNREmployee

// Accessors for assets properties

  • (void)setAssets:(NSArray *)a
    {
    _assets = [a mutableCopy];
    }

  • (NSArray *)assets
    {
    return [_assets copy];
    }

  • (void)addAsset:(BNRAsset *)a
    {
    // Is assets nil?
    if (!_assets) {

      // Create the array
      _assets = [[NSMutableArray alloc] init];
    

    }
    [_assets addObject:a];
    }

  • (unsigned int)valueOfAssets
    {
    // Sum up the resale value of the assets
    unsigned int sum = 0;
    for (BNRAsset *a in _assets) {
    sum += [a resaleValue];
    }
    return sum;
    }

[/code]

Thank you!


#2

You are probably getting confused by the first two methods.

[quote][code]
// Accessors for assets properties

  • (void)setAssets:(NSArray *)a
    {
    _assets = [a mutableCopy];
    }
    [/code][/quote]
    The setAssets: method is changing the value of the _assets instance variable by setting it to a mutable copy of the passed-in array. But notice that it is not just making a direct assignment; it is assigning a mutable copy. The reason for the mutable copy is that later on asset objects will be added to it. Also making a copy implies that the two arrays become independent of each other. This is important to understand: if one is modified, the other remains intact.

[quote][code]

  • (NSArray *)assets
    {
    return [_assets copy];
    }
    [/code][/quote]
    The assets method here is giving out a copy of the _assets array. By giving out a copy, it is ensuring that _assets array remains impervious to unintended changes, which is again important to understand.

#3

Thank you ibex10 for finding time to answer my question!
I can’t understand why do we have to create for the first time the NSMutableArray *assets

[code]import “BNRPerson.h”
@class BNRAsset;

@interface BNREmployee : BNRPerson
{
NSMutableArray *_assets;
}
[/code]
and then create NSArray assets?

[code]@property (nonatomic, copy) NSArray *assets;

  • (void)addAsset:(BNRAsset *)a;
  • (unsigned int)valueOfAssets;[/code]

Why do we also need to create the copy of assets? Why we just don’t add objects in assets? Is this because we create 10 different assets and giving them 10 different labels?

[code]for (int i = 0; i < 10; i++) {
// Create an asset
BNRAsset *asset = [[BNRAsset alloc] init];

        // Give it an interesting label
        NSString *currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
        asset.label = currentLabel;
        asset.resaleValue = 350 + i * 17;[/code]

Did i get it right:
1)We create assets that is NSMutableArray type to tell the compilator that we will store the whole information in such of kind object
2)We create the pointer named assets that is NSArray type
3)With the - (void)setAssets:(NSArray *)a we say that the assets will be now mutable and it’s only the copy of asset we created in step 1
???


#4

[quote]I can’t understand why do we have to create for the first time the NSMutableArray *assets

[code]import “BNRPerson.h”
@class BNRAsset;

@interface BNREmployee : BNRPerson
{
NSMutableArray *_assets;
}[/code]
and then create NSArray assets?

@property (nonatomic, copy) NSArray *assets; ...
[/quote]
No array creation here. They are just declarations: the first one is an instance declaration and the second one a property declaration.

The reason for copying is to force the creation of a new array that is the copy of the source array.
For example:

// Set assets
anEmployee.assets = assetsArray;
// At this point anEmployee's assets array is an exact copy of the source array, the assetsArray.
...
[assetsArray addObject:anAsset];
// Now assetsArray has changed but this change does not affect the assets of anEmployee.

#5

Tank you. I guess I’m getting to undestand it, but still can’t handle with the challenge of this chapter: