指针与引用的坑

2019-12-25  本文已影响0人  风___________
+ (void)load{
    [self test];
    NSLog(@"====分界线====");
    [[[ViewController alloc] init] test];
}

+ (void)test{
    NSString *s = @"aaa";
    NSLog(@"%@,%p",s,s);
    [self change:&s];
    NSLog(@"%@,%p",s,s);
}
+ (void)change:(NSString **)s{
    NSLog(@"%@,%p",*s,*s);
    *s = @"bbb";
    NSLog(@"%@,%p",*s,*s);
}

- (void)test{
    NSMutableString *s = @"aaa".mutableCopy;
    NSLog(@"%@,%p",s,s);
    [self change:s];
    NSLog(@"%@,%p",s,s);
}
- (void)change:(NSMutableString *)s{
    NSLog(@"%@,%p",s,s);
    [s appendString:@"bbb"];
    NSLog(@"%@,%p",s,s);
}
ViewController.m:30 aaa,0x104707548
ViewController.m:35 aaa,0x104707548
ViewController.m:37 bbb,0x104707588
ViewController.m:32 bbb,0x104707588
ViewController.m:24 ====分界线====
ViewController.m:42 aaa,0x2834cca80
ViewController.m:47 aaa,0x2834cca80
ViewController.m:49 aaabbb,0x2834cca80
ViewController.m:44 aaabbb,0x2834cca80
上一篇下一篇

猜你喜欢

热点阅读