objective-c – 使用[NSString stringWithString:@“some string”]与@“some string”的优点和缺点

objective-c – 使用[NSString stringWithString:@“some string”]与@“some string”的优点和缺点,第1张

概述我想比较以下简单的作业: ...@property(nonatomic,retain) UITextField *textField;...self.textField.text = @"some string";self.textField.text = [NSString stringWithString:@"some string"];self.textField.text = 我想比较以下简单的作业:

...@property(nonatomic,retain) UITextFIEld *textFIEld;...self.textFIEld.text = @"some string";self.textFIEld.text = [Nsstring stringWithString:@"some string"];self.textFIEld.text = [Nsstring stringWithFormat:@"some string"];

其中textFIEld是UITextFIEld,text属性是Nsstring.当然所有这些都有效.我知道使用参数时最后两个的区别.但是让我们说我们只对这种用法感兴趣.

问题:

>为了做这种任务,我为什么不总是使用第一个?
>比较后两个,这两个的编译和/或运行时有什么不同吗?为什么我应该使用stringWithString:如果不是这样的话?

解决方法

For doing this kind of assignment,why shouldn’t I always use the first one?

在描述的情况下,您应该始终使用第一个.第二种和第三种情况可能会复制常量字符串,但UITextFIEld的text属性被指定为复制提供的字符串.制作常量字符串的副本是没有意义的,因为UITextFIEld的-setText:可以复制该副本.

Comparing the last two,is there any difference for the compile-
and/or runtime of these two? And why should I use stringWithString: at
all if not?

我的理解是-stringWithFormat:将始终创建一个新字符串,而-stringWithString:可能不会(可能不会)为常量字符串. hypercrypt上面的结果在这方面非常有说服力;如果你想探索更多,你可以尝试使用可变字符串进行相同的测试.

总结

以上是内存溢出为你收集整理的objective-c – 使用[NSString stringWithString:@“some string”]与@“some string”的优点和缺点全部内容,希望文章能够帮你解决objective-c – 使用[NSString stringWithString:@“some string”]与@“some string”的优点和缺点所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1033071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存