输出应该是什么?

时间:2013-04-12 14:17:26

标签: objective-c automatic-ref-counting weak-references

输出应为

strString = değiştim
wkString  = NULL   

但事实并非如此。为什么?

#import <Foundation/Foundation.h>

@interface learnARC : NSObject {
    NSString *strString, __weak *wkString;
}

@property (strong) NSString *strString;
@property (weak) NSString *wkString;

-(void) yaz;

@end

#import "learnARC.h"

@implementation learnARC

@synthesize wkString, strString;

-(void) yaz {
    NSString *anaString = @"anaString";
    strString = anaString;
    wkString = anaString;
    NSLog(@"\nstrString = %@\nwkString  = %@",strString,wkString);

    anaString = @"değiştim";
    NSLog(@"\nstrString = %@\nwkString  = %@",strString,wkString);
}

@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        learnARC *lrnarc = [[learnARC alloc]init];
        [lrnarc yaz];
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

  

为什么?

因为您正在捕捉问题而不是您的班级名称......

严重的是,弱引用应为NULL。您已为其指定了指针(指向字符串@"anaString"的指针)。由于字符串文字具有静态存储持续时间,因此在程序的生命周期内它们永远不会被释放。 (我认为你可能会将变量与属性混淆?)