Pregunta

Bueno, tengo una UITextField.

Dentro de ella es una UITextField.text propiedad.

¿Está bien hacer:

// Assume we have UITextField * tf somewhere..
// now set its text..
tf.text = [ [ NSString alloc ] initWithUTF8String:"Init'd with utf8" ] ;

Mi problema con esto es la memoria. ¿Qué sucede para el antiguo valor de la propiedad text del UITextField.

¿No tiene que hacer:

// maintain reference to old NSString
NSString * oldTfText = tf.text ;

// set the value to the new value you want
tf.text = [ [ NSString alloc ] initWithUTF8String:"Init'd with utf8" ] ;

// release the old NSString now..
[ oldTfText release ] ;

Todavía estoy pensando en MGMT memoria como yo en C. normal, lo cual podría ser la falla aquí.

¿Fue útil?

Solución

UITextField será responsable de la liberación de cualquier valor antiguo que tiene. Su preocupación es sólo con su código y usted es correcto que este NSString alloc'ed debe ser liberado.

También puede utilizar autorelease para evitar la declaración adicional.

tf.text = [ [ [ NSString alloc ] initWithUTF8String:"Init'd with utf8" ] autorelease];

Otros consejos

tf.text es un setter. En cuanto a los archivos de cabecera, la propiedad se define:

@property(nonatomic,copy) NSString *text;

Por lo tanto, yo creo que se debe establecer de esta manera, y dejar que el sistema se encargue de su propia copia:

mytext = [ [ NSString alloc ] initWithUTF8String:"Init'd with utf8" ] ;
tf.text = mytext;
[mytext release];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top