Is it possible to have same name?


#1

in the BNREmployee.h
there are same instance varable named ‘_assets’. thus, there are 2 instance variable of _assets;
is it possible have same instance variable?


{
    NSMutableArray *_assets;
}

@property (nonatomic, copy) NSArray *assets;

int the BNREmployee.m
there are 2 setAssets(setter method) method, and 2 assets(getter method) of same name of NSArray and NSMutableArray?
is it possible? i don’t know why name is same?

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

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

#2

[quote]in the BNREmployee.h
there are same instance varable named ‘_assets’. thus, there are 2 instance variable of _assets;
is it possible have same instance variable?

[code]
{
NSMutableArray *_assets;
}

@property (nonatomic, copy) NSArray *assets;[/code]
[/quote]
There is only one instance variable (named _assets) here (It has the underscore character in front of it.) The other one, which almost sounds the same (assets) is a property.

[quote]int the BNREmployee.m
there are 2 setAssets(setter method) method, and 2 assets(getter method) of same name of NSArray and NSMutableArray?
is it possible? i don’t know why name is same?
[/quote]
This question is not clear. Please make it clear.


#3

thank for reply but i have a question, as below.

the book says that “The compiler created instance variables named _heightInMeters and _weightInKilos. however, you do not see these variables in your code because there are no longer any explicit accesssor implementations.”

{
    NSMutableArray *_assets;
}

@property (nonatomic, copy) NSArray *assets;

so, there is explicit “_assets” instance variable (NSMutableArray *_assets)
and, there is implicit “_assets” instance variable (@property (nonatomic, copy) NSArray *assets) exist.

so are there same name exist?


#4

I got you now.

[quote][code]{
NSMutableArray *_assets;
}

@property (nonatomic, copy) NSArray *assets;[/code][/quote]
Because you have explicitly declared an instance variable named _assets, the compiler will see that and won’t create another instance variable also named _assets for the assets property.

To convince yourself that this happening, build the following program:

//  main.m

#import <Foundation/Foundation.h>

@interface Foo : NSObject
{
    NSMutableArray *_store;
}

@property (nonatomic, copy) NSArray *store;
@property (nonatomic, copy) NSArray *otherStore;

@end

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        Foo *foo = [[Foo alloc] init];
        NSLog (@"%@", foo.store);
        foo.store = [NSMutableArray array];
        NSLog (@"%@", foo.store);

    }
    return 0;
}

@implementation Foo
@end

And look at its symbol table with the nm command:

> nm Property
0000000100000db0 t -[Foo .cxx_destruct]
0000000100000d40 t -[Foo otherStore]
0000000100000d70 t -[Foo setOtherStore:]
0000000100000d00 t -[Foo setStore:]
0000000100000cd0 t -[Foo store]
                 U _NSLog
0000000100001230 S _OBJC_CLASS_$_Foo
                 U _OBJC_CLASS_$_NSMutableArray
                 U _OBJC_CLASS_$_NSObject
00000001000012a8 s _OBJC_IVAR_$_Foo._otherStore
00000001000012a0 S _OBJC_IVAR_$_Foo._store
0000000100001258 S _OBJC_METACLASS_$_Foo
                 U _OBJC_METACLASS_$_NSObject
                 U ___CFConstantStringClassReference
0000000100000000 T __mh_execute_header
                 U __objc_empty_cache
                 U __objc_empty_vtable
0000000100000b80 T _main
                 U _objc_autoreleasePoolPop
                 U _objc_autoreleasePoolPush
                 U _objc_getProperty
                 U _objc_msgSend
                 U _objc_msgSend_fixup
                 U _objc_release
                 U _objc_retainAutoreleasedReturnValue
                 U _objc_setProperty_nonatomic_copy
                 U _objc_storeStrong
                 U dyld_stub_binder

As you can see, there is only one instance variable named _store, with the internal name OBJC_IVAR$_Foo._store.


#5

thank for help.