Tôi có lớp sau Ví dụ:Mục tiêu sở hữu C NSString * giữ lại đếm kỳ quặc
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;
}
nào xuất ra như sau:
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
Tôi gọi ing it like:
Test *test = [[Test alloc] initWithValue:@"some text"];
Giá trị không được tính là 1? Tôi đang thiếu gì?
Cảm ơn sự giúp đỡ của bạn.
Điều này không thực sự giải quyết các vấn đề cơ bản với mã của poster ban đầu: Anh ta không tuân theo các quy tắc quản lý bộ nhớ, và anh ta gán trực tiếp cho một biến cá thể thay vì đi qua thuộc tính. –