For an IBOutlet UITextField
, does it matter as far as memory management or other reasons how you clear the text value?
对于IBOutlet UITextField,在内存管理或其他原因如何清除文本值方面是否重要?
textFieldX.text = nil
or
要么
textFieldX.text = @"";
In objective-c it is acceptable to message a nil
object and @""
is a static NSString *
. I'm not sure if every @""
points to the same object in memory or if it allocates a bunch of 1 byte null terminated strings.
在objective-c中,可以发送一个nil对象,而@“”是一个静态的NSString *。我不确定每个@“”是否指向内存中的同一个对象,或者它是否分配了一堆1字节的空终止字符串。
Not a big deal, just thought I'd ask the community. Thanks.
没什么大不了的,只是想我会问社区。谢谢。
5 个解决方案
#1
32
Personally I would think less of the memory usage here and more on code-maintainability.
就个人而言,我会更少考虑这里的内存使用情况,更多地考虑代码可维护性。
To me, It makes sense that a label always has a string. In the future someone might try to append a labels value, save it in a database, wrap it in xml, etc. An empty NSString in this case makes much more sense to me that a 0x0.
对我来说,标签总是有一个字符串是有道理的。在将来,有人可能会尝试附加标签值,将其保存在数据库中,将其包装在xml中等等。在这种情况下,空NSString对我来说更有意义,即0x0。
#2
8
I usually do textFieldX.text = @"";
just for clarity. This helps me remember that the value should be a string, and that I can pass it all the standard string methods.
我通常做textFieldX.text = @“”;为了清楚起见。这有助于我记住该值应该是一个字符串,并且我可以将所有标准字符串方法传递给它。
#3
3
I recommend you to use nil, because I discovered that when you use @"", the placeholder is not aligned in the center(if it must be aligned). Look, how I changed my code to get placeholder aligned properly.
我建议你使用nil,因为我发现当你使用@“”时,占位符没有在中心对齐(如果它必须对齐)。看,我是如何更改代码以使占位符正确对齐的。
wordTextField.textAlignment = UITextAlignmentCenter;
//wordTextField.text = @"";
wordTextField.text = nil;
translationTextField.textAlignment = UITextAlignmentCenter;
//translationTextField.text = @"";
translationTextField.text = nil;
#4
2
Strings always copy in Objective-C, so the second option is most likely creating another string, and then pointing to it. In that way, I think you're right.
字符串总是在Objective-C中复制,因此第二个选项很可能是创建另一个字符串,然后指向它。这样,我认为你是对的。
To play devil's advocate, I would assume that the compiler optimizes option B to do something like option A anyway. Personally, I would always do option B because it's more readable as far as the end operation you want to accomplish.
为了扮演魔鬼的拥护者,我认为编译器会优化选项B以执行类似选项A的操作。就个人而言,我总是会做选项B,因为就你想要完成的最终操作而言,它更具可读性。
UPDATE: I didn't find a way to accomplish your goal differently, but you may be interested in this tidbit (from Apple UITextField Docs):
更新:我没有找到以不同方式实现目标的方法,但您可能对这个花絮感兴趣(来自Apple UITextField Docs):
clearButtonMode
clearButtonMode
Controls when the standard clear button appears in the text field.
控制标准清除按钮何时出现在文本字段中。
@property(nonatomic) UITextFieldViewMode clearButtonMode
@property(非原子)UITextFieldViewMode clearButtonMode
Discussion
讨论
The standard clear button is displayed at the right side of the text field as a way for the user to remove text quickly. This button appears automatically based on the value set for this property.
标准清除按钮显示在文本字段的右侧,以便用户快速删除文本。此按钮基于为此属性设置的值自动显示。
The default value for this property is UITextFieldViewModeNever.
此属性的默认值为UITextFieldViewModeNever。
I think this would allow you to setup functionality for the user to clear the text field.
我认为这将允许您为用户设置清除文本字段的功能。
#5
0
I find that using textField.text = nil;
has an undesirable effect on my project. I am building a calculator.
我发现使用textField.text = nil;对我的项目有不良影响。我正在建一个计算器。
When using the above code, after I press the clear button, pressing a digit does not come up on screen.
使用上述代码时,按下清除按钮后,屏幕上不会显示数字。
textField.text = @"";
works good for me.
对我有益。
#1
32
Personally I would think less of the memory usage here and more on code-maintainability.
就个人而言,我会更少考虑这里的内存使用情况,更多地考虑代码可维护性。
To me, It makes sense that a label always has a string. In the future someone might try to append a labels value, save it in a database, wrap it in xml, etc. An empty NSString in this case makes much more sense to me that a 0x0.
对我来说,标签总是有一个字符串是有道理的。在将来,有人可能会尝试附加标签值,将其保存在数据库中,将其包装在xml中等等。在这种情况下,空NSString对我来说更有意义,即0x0。
#2
8
I usually do textFieldX.text = @"";
just for clarity. This helps me remember that the value should be a string, and that I can pass it all the standard string methods.
我通常做textFieldX.text = @“”;为了清楚起见。这有助于我记住该值应该是一个字符串,并且我可以将所有标准字符串方法传递给它。
#3
3
I recommend you to use nil, because I discovered that when you use @"", the placeholder is not aligned in the center(if it must be aligned). Look, how I changed my code to get placeholder aligned properly.
我建议你使用nil,因为我发现当你使用@“”时,占位符没有在中心对齐(如果它必须对齐)。看,我是如何更改代码以使占位符正确对齐的。
wordTextField.textAlignment = UITextAlignmentCenter;
//wordTextField.text = @"";
wordTextField.text = nil;
translationTextField.textAlignment = UITextAlignmentCenter;
//translationTextField.text = @"";
translationTextField.text = nil;
#4
2
Strings always copy in Objective-C, so the second option is most likely creating another string, and then pointing to it. In that way, I think you're right.
字符串总是在Objective-C中复制,因此第二个选项很可能是创建另一个字符串,然后指向它。这样,我认为你是对的。
To play devil's advocate, I would assume that the compiler optimizes option B to do something like option A anyway. Personally, I would always do option B because it's more readable as far as the end operation you want to accomplish.
为了扮演魔鬼的拥护者,我认为编译器会优化选项B以执行类似选项A的操作。就个人而言,我总是会做选项B,因为就你想要完成的最终操作而言,它更具可读性。
UPDATE: I didn't find a way to accomplish your goal differently, but you may be interested in this tidbit (from Apple UITextField Docs):
更新:我没有找到以不同方式实现目标的方法,但您可能对这个花絮感兴趣(来自Apple UITextField Docs):
clearButtonMode
clearButtonMode
Controls when the standard clear button appears in the text field.
控制标准清除按钮何时出现在文本字段中。
@property(nonatomic) UITextFieldViewMode clearButtonMode
@property(非原子)UITextFieldViewMode clearButtonMode
Discussion
讨论
The standard clear button is displayed at the right side of the text field as a way for the user to remove text quickly. This button appears automatically based on the value set for this property.
标准清除按钮显示在文本字段的右侧,以便用户快速删除文本。此按钮基于为此属性设置的值自动显示。
The default value for this property is UITextFieldViewModeNever.
此属性的默认值为UITextFieldViewModeNever。
I think this would allow you to setup functionality for the user to clear the text field.
我认为这将允许您为用户设置清除文本字段的功能。
#5
0
I find that using textField.text = nil;
has an undesirable effect on my project. I am building a calculator.
我发现使用textField.text = nil;对我的项目有不良影响。我正在建一个计算器。
When using the above code, after I press the clear button, pressing a digit does not come up on screen.
使用上述代码时,按下清除按钮后,屏幕上不会显示数字。
textField.text = @"";
works good for me.
对我有益。