我有以下示例类:
Test.h:
@interface Test : UIButton { NSString *value; } - (id)initWithValue:(NSString *)newValue; @property(copy) NSString *value;
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; }
其中产生以下输出:
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 Concentration[18604:20b] on nil value has retain count of 0 2008-12-31 09:31:41.757 Concentration[18604:20b] after init value has retain count of 2147483647
我称之为:
Test *test = [[Test alloc] initWithValue:@"some text"];
值不应该有1的保留计数吗?我错过了什么?
谢谢你的帮助.
不要看保留计数.它们没有用,只会误导你 - 你不能确定没有其他东西保留一个物体,你从某个地方得到的物体不会被分享.
相反,专注于对象所有权并遵循Cocoa内存管理规则.这样,无论Cocoa在幕后为您做什么优化,您的内存管理都是正确的.(例如,-copy
仅-retain
针对不可变对象实现.)
此外,理解对象属性和对象中的实例变量之间的区别至关重要.在您的问题代码中,您要为实例变量赋值.那个实例变量就是:一个变量.分配给它将表现得像任何其他变量赋值.要使用该属性,必须使用点语法或括号语法来实际调用属性的setter方法:
self.value = newValue; // this is exactly equivalent to the next line [self setValue:newValue]; // this is exactly equivalent to the previous line
为点语法和括号语法生成的代码是相同的,并且都不会直接访问实例变量.
你传入一个文字字符串.编译器可能会在静态内存中分配它,并将保留计数设置为最大可能值.
尝试使用动态分配的字符串,看看会发生什么.
NSString* string = [[NSString alloc] initWithString: @"some text"]; Test* test = [[Test alloc] initWithValue: string];