鉴于此代码:
// Initialize string
NSString *name = @"Franzi";
Run Code Online (Sandbox Code Playgroud)
@""宏创建一个带有给定文本的NSString(这里名称为Franzi)和一个RETAIN COUNT OF 1?
所以@""给出了一个必须被释放的NSString?我对这个对象负责吗?第二个代码示例然后让我感到困惑,即使我这样使用它:
NSSting *message;
message = [NSString stringWithFormat:@"Hello @%!",name];
//message = [NSString stringWithFormat:@"Hello Girl!"];
Run Code Online (Sandbox Code Playgroud)
因此消息在下一个运行循环中释放,k.但是作为stringWithFormat的参数给出的NSString是什么?
类对象NSString是否释放了作为争论的NSString @"Hello%@"/ @"Hello Girl"?或@"" - Konstruktor只回馈自动释放的NSStrings?
我有以下示例类:
Test.h:
@interface Test : UIButton {
NSString *value;
}
- (id)initWithValue:(NSString *)newValue;
@property(copy) NSString *value;
Run Code Online (Sandbox Code Playgroud)
Test.m:
@implementation Test
@synthesize value;
- (id)initWithValue:(NSString *)newValue {
[super init];
NSLog(@"before nil value has retain count of %d", [value retainCount]);
value = nil;
NSLog(@"on nil value has retain count of %d", [value retainCount]);
value = newValue;
NSLog(@"after init value has retain count of %d", [value retainCount]);
return self;
}
Run Code Online (Sandbox Code Playgroud)
其中产生以下输出:
2008-12-31 09:31:41.755 Concentration[18604:20b] before nil value has retain count of 0
2008-12-31 09:31:41.756 …Run Code Online (Sandbox Code Playgroud)