Please explain the relationship between this 2 line of code


#1

[code]{
NSMutableArray *_assets;
}

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

Please tell me what these 2 lines of code does. I think my mind is messing up stuff here.
Also @class BNRAssets only declare that there is a type BNRAssets because we will be writing this line of code right?

Please explain and correct me if im wrong


#2

[quote]{ NSMutableArray *_assets; } @property (nonatomic, copy) NSArray *assets;
Please tell me what these 2 lines of code does.[/quote]

NSMutableArray *_assets; This line declares an instance variable named _assets for a type named NSMutableArray.
Whereas this line:

@property (nonatomic, copy) NSArray *assets;declares a property for a type named NSArray.

But these are all explained in the book. Did you read all the chapters in the book up until this chapter, Chapter 19?

You can also read the Introduction: Programming with Objective-C in Xcode.


#3

I did read all the chapters in the book all the way to this chapter. I don’t know why but this chapter does confuse me quite a bit.
I am afraid I asked the wrong question.
What i really wanted to know is the relationship between the 2 lines of code.
Why do we declare instance variable NSMutableArray and also declare a properties of array?
Also why do we use copy in the property?
what does copy do?


#4

[quote]What i really wanted to know is the relationship between the 2 lines of code.
Why do we declare instance variable NSMutableArray and also declare a properties of array?
Also why do we use copy in the property?
what does copy do?

{ NSMutableArray *_assets; } @property (nonatomic, copy) NSArray *assets;
[/quote]
Most properties require a backing store object. The _assets instance variable is used to hang on to the backing store object, which is an NSMutableArray, for the assets property. As for the copy attribute in the property declaration, it says that the array should be copied and assigned to the instance variable when you set through the property an array containing assets; it also says that when you ask for the array, again through the property, a copy of the array should be made and handed over to you, not the array that is stored in the _assets instance variable.

This is all explained in Introduction: Programming with Objective-C in Xcode.


#5

Ibex, if you say most properties need backing store object why did the book only declare one instance variable while there are many properties declared in the class. For example there are properties of employeeID, hireDate and more.


#6

That’s because if you don’t provide one, one will be automatically provided (you can access it but can’t see it.)


#7

I was also confused with “copy”. As I get- this is the most problematic chapters in this book, there was no explanation why we do this, and when we use this. Try to read Immutable objects, it comes later in 24 chapter, maybe it will help you to understand it.
Thanks for ibex10, for making it a little clear.


#8

So what are the purpose of providing one back storage for this particular situation?
Wouldn’t it be better if we let property do all the things for us, as you say it will automatically make one back storage for us.


#9

[quote]So what are the purpose of providing one back[ing] storage for this particular situation?
Wouldn’t it be better if we let property do all the things for us, as you say it will automatically make one back[ing] storage for us.
[/quote]

[code]
@interface Company: NSObject
{
NSMutableArray *_assets;
}
@property (nonatomic, copy) NSArray *assets;

@end
[/code]The above code is intended to satisfy the following two requirements.

Requirement 1: Allow only the Company objects to modify the assets.
Requirement 2: Don’t allow any other object to modify the assets.

These two requirements can’t be satisfied by relying on the property alone.

[code]
@interface Company: NSObject

@property (nonatomic, copy) NSArray *assets;

@end[/code]Requirement 1: not satisfied.
Requirement 2: satisfied.

[code]
@interface Company: NSObject

@property (nonatomic, copy) NSMutableArray *assets;

@end[/code]Requirement 1: satisfied.
Requirement 2: not satisfied.


#10

[code]@interface Company: NSObject
{
NSArray *_assets;
}
@property (nonatomic, copy) NSMutableArray *assets;

@end[/code]

does it make any difference if i do it this way?


#11

You should try it and see what happens. Don’t be afraid of trying things on your own. You can’t break anything :slight_smile:


#12

Ibex, what i’m confused here is when we declare

in .h file

and declare

in class extension in .m file or in the header file.

why would’t the program go haywire as they both has the same name of instance variable.
the property NSArray also creates an ivar with name _assets and we’re declaring a private/protected ivar with name _assets. Why are these two item distinct things?

Plus
the setter and getter method declared in the .m file

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

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

[/code]

are we overriding the setter and getter method of the property NSArray?


#13

[quote=“ibex10”][quote]So what are the purpose of providing one back[ing] storage for this particular situation?
Wouldn’t it be better if we let property do all the things for us, as you say it will automatically make one back[ing] storage for us.
[/quote]

[code]
@interface Company: NSObject
{
NSMutableArray *_assets;
}
@property (nonatomic, copy) NSArray *assets;

@end
[/code]The above code is intended to satisfy the following two requirements.

Requirement 1: Allow only the Company objects to modify the assets.
Requirement 2: Don’t allow any other object to modify the assets.

These two requirements can’t be satisfied by relying on the property alone.

[code]
@interface Company: NSObject

@property (nonatomic, copy) NSArray *assets;

@end[/code]Requirement 1: not satisfied.
Requirement 2: satisfied.

[code]
@interface Company: NSObject

@property (nonatomic, copy) NSMutableArray *assets;

@end[/code]Requirement 1: satisfied.
Requirement 2: not satisfied.[/quote]

Sorry ibex10, but there is a semantic redundancy in the following:

Requirement 1: Allow only the Company objects to modify the assets.
Requirement 2: Don’t allow any other object to modify the assets.

If you allow ONLY Company objects to modify the assets, then you don’t allow ANY OTHER object to modify them.

Hence, as long as Requirement 1 is satisfied, you never have to bother about Requirement 2.

And:

[code]
@interface Company: NSObject

@property (nonatomic, copy) NSMutableArray *assets;

@end[/code]

should be sufficient.

No?