iPhone开发 - NSString创建 [英] iPhone Dev - NSString Creation

查看:91
本文介绍了iPhone开发 - NSString创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑与NSStrings。喜欢什么时候应该做

  NSString * aString = @Hello 

应该是:

  NSString * aString = [[NSString alloc] initWithString:@Hello]; 

但是当你为NSString属性赋值时,它的不同呢?

解决方案

div>

一般来说,你应该做的第一,但他们大多是功能相同。您可以像正常的NSString字符串对象一样对待常量NSStrings,例如:

  [@Hellolength] 

将返回5.您可以将它们分配给属性,一切正常。你可能注意到的一件事是,使用常量NSStrings,你不必担心retain / release。这是因为它们实际上映射到应用程序只读数据部分,并且没有分配内存。保留并释放对他们的呼叫仍然有效,他们只是成为了noops。


I'm really confused with NSStrings. Like when should I do

NSString *aString = @"Hello";

of should it be:

NSString *aString = [[NSString alloc] initWithString:@"Hello"];

But then its different when you're assigning a value to an NSString property isn't it? Can someone clear this up for me?

Thanks!!

解决方案

In general you should do the first, but they are mostly functionally the same. You can treat constant NSStrings just like normal NSString string objects, for instance:

[@"Hello" length]

will return 5. You can assign them to properties, everything just works. The one thing you might notice is that with the constant NSStrings you don't have to worry about retain/release. That is because they are actually mapped into the applications readonly data section, and don't have allocated memory. Retain and release calls against them still work, they just become noops.

这篇关于iPhone开发 - NSString创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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