XCode是否为所有空字符串创建一个对象? [英] Does XCode creates one object for all empty strings?

查看:57
本文介绍了XCode是否为所有空字符串创建一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到所有@"对象在每次执行时都会创建一个引用.

i've noticed that all @"" objects create one reference for all times it is executed.

NSString *s1=@"";
NSString *s2=@"";

在此样本中,s1等于s2.

In this sample s1 equals s2.

@"都会在所有情况下创建一个指针?

@"" will create one pointer in all cases, every time i use it?

在Objective-C中比较字符串时,我可以依靠此功能吗?

Can i rely on this feature in comparing strings in objective-c?

或者,如果我想确保我的字符串为空,我可以使用以下语句:

Or simply, can i use this statement, if i want to assure that my string is empty:

 if(s == @""){
 //do something
 }

推荐答案

是的,Objective C在编译器中进行了优化,将所有等效的字符串文字直接指向内存中的同一字符串,以避免分配不必要的资源.此功能是可靠的,但有可能并非总是如Objective C语言规范所记录的那样发生.

Yes Objective C has an optimization in the compiler that simply points all equivalent string literals to the same string in memory to avoid allocating unnecessary resources. This feature is reliable but there is a chance that this will not always happen as documented in the Objective C language specs.

这篇关于XCode是否为所有空字符串创建一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆