嗯,我有一个UITextField。
它内部有一个属性UITextField.text。
可以这样做吗?
// Assume we have UITextField * tf somewhere..
// now set its text..
tf.text = [ [ NSString alloc ] initWithUTF8String:"Init'd with utf8" ] ;我的问题是记忆。对UITextField的text属性的旧值发生了什么。
你不必这样做:
// 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 ] ;我仍然在想记忆mgmt,就像我在普通C中一样。这可能是这里的缺陷。
发布于 2009-11-02 17:11:57
UITextField将负责释放它所拥有的任何旧值。您只关心您的代码,并且您正确地认为必须释放这个分配的NSString。
您也可以使用自动释放来避免额外的声明。
tf.text = [ [ [ NSString alloc ] initWithUTF8String:"Init'd with utf8" ] autorelease];发布于 2009-11-02 19:46:26
tf.text是个策划人。查看头文件,定义了该属性:
@property(nonatomic,copy) NSString *text;因此,我认为你应该这样设置它,让系统处理它自己的副本:
mytext = [ [ NSString alloc ] initWithUTF8String:"Init'd with utf8" ] ;
tf.text = mytext;
[mytext release];https://stackoverflow.com/questions/1662475
复制相似问题