复制对象(三)属性的copy特性(续)

NSString对象是不可变字符串对象,但是NSString类型的指针可以指向NSMutableString类型的对象(NSMutableString类是NSString的子类),因此NSString *str指针指向的对象可能是变化的。测试代码如下:

StringObject类:

#import <Foundation/Foundation.h>

@interface StringObject : NSObject

@property (strong, nonatomic) NSString *strong_string;

@property (weak, nonatomic)   NSString *weak_string;

@property (retain,nonatomic)  NSString *retain_string;

@property (copy, nonatomic)   NSString *cpy_string;

- (void)createStrings;

@end

#import "StringObject.h"

@implementation StringObject

- (void)createStrings {
    
    NSMutableString *mutable_string = [NSMutableString stringWithString:@"string"];
    NSLog(@"mutable string");
    NSLog(@"指向的对象的地址为:%p", mutable_string);
    NSLog(@"指针的地址为:     %p", &mutable_string);
    NSLog(@"---------------------------");
    
    self.strong_string = mutable_string;
    NSLog(@"strong string");
    NSLog(@"指向的对象的地址为:%p", _strong_string);
    NSLog(@"指针的地址为:     %p", &_strong_string);
    NSLog(@"---------------------------");
    
    self.weak_string   = mutable_string;
    NSLog(@"weak string");
    NSLog(@"指向的对象的地址为:%p", _weak_string);
    NSLog(@"指针的地址为:     %p", &_weak_string);
    NSLog(@"---------------------------");
    
    self.retain_string = mutable_string;
    NSLog(@"retain string");
    NSLog(@"指向的对象的地址为:%p", _retain_string);
    NSLog(@"指针的地址为:     %p", &_retain_string);
    NSLog(@"---------------------------");
    
    self.cpy_string    = mutable_string;
    NSLog(@"copy string");
    NSLog(@"指向的对象的地址为:%p", _cpy_string);
    NSLog(@"指针的地址为:     %p", &_cpy_string);
    NSLog(@"---------------------------");
    
    [mutable_string appendString:@" append"];
    NSLog(@"mutable string = %@", mutable_string);
    NSLog(@"strong string  = %@", self.strong_string);
    NSLog(@"weak string    = %@", self.weak_string);
    NSLog(@"retain string  = %@", self.retain_string);
    NSLog(@"copy string    = %@", self.cpy_string);
    
}

@end

main.m文件:

#import <Foundation/Foundation.h>
#import "StringObject.h"

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

    @autoreleasepool {
        
        StringObject *so = [[StringObject alloc] init];
        [so createStrings];
        
    }
    return 0;
}

输出结果如下:

2014-02-01 21:03:59.468 copy[2702:303] mutable string
2014-02-01 21:03:59.469 copy[2702:303] 指向的对象的地址为:0x1002044e0
2014-02-01 21:03:59.469 copy[2702:303] 指针的地址为:     0x7fff5fbff8b8
2014-02-01 21:03:59.470 copy[2702:303] ---------------------------
2014-02-01 21:03:59.470 copy[2702:303] strong string
2014-02-01 21:03:59.470 copy[2702:303] 指向的对象的地址为:0x1002044e0
2014-02-01 21:03:59.470 copy[2702:303] 指针的地址为:     0x100201e58
2014-02-01 21:03:59.470 copy[2702:303] ---------------------------
2014-02-01 21:03:59.471 copy[2702:303] weak string
2014-02-01 21:03:59.471 copy[2702:303] 指向的对象的地址为:0x1002044e0
2014-02-01 21:03:59.471 copy[2702:303] 指针的地址为:     0x100201e60
2014-02-01 21:03:59.471 copy[2702:303] ---------------------------
2014-02-01 21:03:59.472 copy[2702:303] retain string
2014-02-01 21:03:59.472 copy[2702:303] 指向的对象的地址为:0x1002044e0
2014-02-01 21:03:59.472 copy[2702:303] 指针的地址为:     0x100201e68
2014-02-01 21:03:59.472 copy[2702:303] ---------------------------
2014-02-01 21:03:59.473 copy[2702:303] copy string
2014-02-01 21:03:59.494 copy[2702:303] 指向的对象的地址为:0x7fff7e709ad0
2014-02-01 21:03:59.495 copy[2702:303] 指针的地址为:     0x100201e70
2014-02-01 21:03:59.495 copy[2702:303] ---------------------------
2014-02-01 21:03:59.495 copy[2702:303] mutable string = string append
2014-02-01 21:03:59.496 copy[2702:303] strong string  = string append
2014-02-01 21:03:59.496 copy[2702:303] weak string    = string append
2014-02-01 21:03:59.496 copy[2702:303] retain string  = string append
2014-02-01 21:03:59.496 copy[2702:303] copy string    = string

由输出结果可知,设NSMutableString *mutable_string指针指向可变字符串对象STRING,那么在赋值后strong, weak, retain属性的NSString指针指向是同一个对象STRING,所以如果mutable_string对STRING作出修改,将会影响到strong, weak, retain类型的属性。相对地,由于copy特性的指针在赋值时进行了深复制,所以copy_string指向的对象不受影响。


如果希望NSString属性跟随外部的影响动态变化,就用strong,weak或retain等特性,如果希望保护NSString的值不变,就用copy特性。


NSArray对象同理:

ArrayObject类:

#import <Foundation/Foundation.h>

@interface ArrayObject : NSObject

@property (strong, nonatomic) NSArray *strong_array;

@property (weak, nonatomic)   NSArray *weak_array;

@property (retain, nonatomic) NSArray *retain_array;

@property (copy, nonatomic)   NSArray *cpy_array;

- (void)createArrayObjects;

@end

#import "ArrayObject.h"

@implementation ArrayObject

- (void)createArrayObjects {
    
    NSMutableArray *mutable_array = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil];
    NSLog(@"mutable array");
    NSLog(@"指向的对象的地址为:%p", mutable_array);
    NSLog(@"指针的地址为:     %p", &mutable_array);
    NSLog(@"---------------------------");
    
    self.strong_array = mutable_array;
    NSLog(@"strong array");
    NSLog(@"指向的对象的地址为:%p", _strong_array);
    NSLog(@"指针的地址为:     %p", &_strong_array);
    NSLog(@"---------------------------");
    
    self.weak_array   = mutable_array;
    NSLog(@"weak array");
    NSLog(@"指向的对象的地址为:%p", _weak_array);
    NSLog(@"指针的地址为:     %p", &_weak_array);
    NSLog(@"---------------------------");
    
    self.retain_array = mutable_array;
    NSLog(@"retain array");
    NSLog(@"指向的对象的地址为:%p", _retain_array);
    NSLog(@"指针的地址为:     %p", &_retain_array);
    NSLog(@"---------------------------");
    
    self.cpy_array    = mutable_array;
    NSLog(@"copy array");
    NSLog(@"指向的对象的地址为:%p", _cpy_array);
    NSLog(@"指针的地址为:     %p", &_cpy_array);
    NSLog(@"---------------------------");
    
    mutable_array[0] = @"new 1";
    NSLog(@"mutable array = %@", mutable_array);
    NSLog(@"strong array  = %@", self.strong_array);
    NSLog(@"weak array    = %@", self.weak_array);
    NSLog(@"retain array  = %@", self.retain_array);
    NSLog(@"copy array    = %@", self.cpy_array);
    
}

@end

main.m文件:

#import <Foundation/Foundation.h>
#import "StringObject.h"
#import "ArrayObject.h"

#define TEST 0

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

    @autoreleasepool {
#if TEST == 1
        StringObject *so = [[StringObject alloc] init];
        [so createStrings];
#else
        ArrayObject *ao = [[ArrayObject alloc] init];
        [ao createArrayObjects];
#endif
        
    }
    return 0;
}

输出如下:

2014-02-01 21:23:35.100 copy[2839:303] mutable array
2014-02-01 21:23:35.101 copy[2839:303] 指向的对象的地址为:0x10010b4e0
2014-02-01 21:23:35.101 copy[2839:303] 指针的地址为:     0x7fff5fbff8b8
2014-02-01 21:23:35.101 copy[2839:303] ---------------------------
2014-02-01 21:23:35.101 copy[2839:303] strong array
2014-02-01 21:23:35.102 copy[2839:303] 指向的对象的地址为:0x10010b4e0
2014-02-01 21:23:35.102 copy[2839:303] 指针的地址为:     0x1001097a8
2014-02-01 21:23:35.102 copy[2839:303] ---------------------------
2014-02-01 21:23:35.102 copy[2839:303] weak array
2014-02-01 21:23:35.103 copy[2839:303] 指向的对象的地址为:0x10010b4e0
2014-02-01 21:23:35.103 copy[2839:303] 指针的地址为:     0x1001097b0
2014-02-01 21:23:35.103 copy[2839:303] ---------------------------
2014-02-01 21:23:35.103 copy[2839:303] retain array
2014-02-01 21:23:35.104 copy[2839:303] 指向的对象的地址为:0x10010b4e0
2014-02-01 21:23:35.104 copy[2839:303] 指针的地址为:     0x1001097b8
2014-02-01 21:23:35.104 copy[2839:303] ---------------------------
2014-02-01 21:23:35.104 copy[2839:303] copy array
2014-02-01 21:23:35.104 copy[2839:303] 指向的对象的地址为:0x1002002a0
2014-02-01 21:23:35.105 copy[2839:303] 指针的地址为:     0x1001097c0
2014-02-01 21:23:35.105 copy[2839:303] ---------------------------
2014-02-01 21:23:35.105 copy[2839:303] mutable array = (
    "new 1",
    2,
    3
)
2014-02-01 21:23:35.105 copy[2839:303] strong array  = (
    "new 1",
    2,
    3
)
2014-02-01 21:23:35.106 copy[2839:303] weak array    = (
    "new 1",
    2,
    3
)
2014-02-01 21:23:35.106 copy[2839:303] retain array  = (
    "new 1",
    2,
    3
)
2014-02-01 21:23:35.106 copy[2839:303] copy array    = (
    1,
    2,
    3
)





复制对象(三)属性的copy特性(续)

上一篇:Python实现支付宝当面付之——扫码支付


下一篇:Java-day2