tengo la siguiente clase de ejemplo:Objetivo C NSString * propiedad de retener rareza recuento
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;
}
que produce el siguiente resultado:
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
I am call ing lo como:
Test *test = [[Test alloc] initWithValue:@"some text"];
¿No debería el valor tener un conteo de retención de 1? ¿Qué me estoy perdiendo?
Gracias por su ayuda.
Esto realmente no aborda los problemas fundamentales con el código del póster original: no está siguiendo las reglas de administración de memoria, y está asignando directamente a una variable de instancia en lugar de pasar por la propiedad. –