0

输出应该是

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;
}
4

1 回答 1

2

为什么?

因为你在强调你的问题而不是你的班级名称......

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

于 2013-04-12T14:23:26.290 回答