开发者

String declarations and assignments: 3 methods

For non-retained string declarations, are these thr开发者_运维问答ee lines the same?

NSString *list2 = self.map;

NSString *list2 = [NSString stringWithFormat:@"%@", self.map];

NSString *list2 = [NSString stringWithString:self.map];

They all create an autoreleased string object, right? Is there a preferred method among these, or are there any differences in the memory usage or behavior of "list2" depending on these methods?

For some reason, I find the manipulation of strings in objective-C the most confusing transition from other languages.


The simple fact, You don't own the object in the above three cases, So you could use either, This is more related to choice of developer then performance.

Go through the Memory Management Programming Guide


They all create an autoreleased string object, right?

No, the first one merely assigns the pointer returned by string.map to list2. The second and third ones theoretically create new NSStrings that you don't own and assign them to list2. However, if string.map returns an immutable string, the third one will probably give you the same pointer (possibly retained and autoreleased).

In all cases you do not own the (new) string. That's actually all you need to know. They may be autoreleased, but it is not relevant to you using them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜