NSString * s = @“string”之间的区别是什么?和NSString * s = [[NSString alloc] initWithString:@" string"]? [英] What's the difference between NSString *s = @"string" and NSString *s = [[NSString alloc] initWithString:@"string"]?

查看:201
本文介绍了NSString * s = @“string”之间的区别是什么?和NSString * s = [[NSString alloc] initWithString:@" string"]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为问题很清楚,但仍然 - 有什么区别:

I think the question is clear enough but still - what's the difference between:

NSString *string = @"Hello world!";

NSString *string = [[NSString alloc] initWithString:@"Hello world!"];

让我知道

Let me know if this already answers it.

推荐答案

NSString *string = [[NSString alloc] initWithString:@"Hello world!"];

每个可可作为可可命名约定使用一个名称以alloc开头的方法创建一个对象, ,new,copy或mutableCopy。这意味着你拥有上面的字符串,因此你负责释放该对象。

Per cocoa as cocoa naming convention, You create an object using a method whose name begins with "alloc", "new", "copy", or "mutableCopy". That means you own the string above, hence you're responsible to release the object.

NSString *string = @"Hello World";

上面的行是一个字符串文字/常量,你不分配它或释放它。您不拥有此对象。

The line above is a string literal / constant, you don't alloc it or release it. You don't own this object.

这篇关于NSString * s = @“string”之间的区别是什么?和NSString * s = [[NSString alloc] initWithString:@" string"]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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