简体   繁体   English

NSString和Objective-C内存管理ARC

[英]NSString and Objective-C Memory Management ARC

I'm a little confused when coding an Objective-C project. 在编写Objective-C项目时,我有些困惑。 The ARC is on. ARC已打开。 Here is a sample code: 这是一个示例代码:

NSString *foo = [[NSString alloc] initWithUTF8String:"This is a C string."];

// Use foo here...

foo = @"This is an ObjC string."

Here are my questions: 这是我的问题:

  1. Do I need to explicitly terminate C string with '\\0' in initWithUTF8String: method, or it is okay to omit NULL terminator? 我是否需要在initWithUTF8String:方法中以'\\ 0'显式终止C字符串,还是可以省略NULL终止符?

  2. Is there any memory leakage when I reuse foo as a pointer and assign new Objective-C string to it? 当我将foo用作指针并为其分配新的Objective-C字符串时,是否存在任何内存泄漏? Why? 为什么?

  3. If I change NSString to other class, like NSObject or my own class, is there any difference for question 2? 如果我将NSString更改为其他类,例如NSObject或自己的类,问题2有什么区别吗? (Initialize an object and then reassign other value directly to it.) (初始化一个对象,然后直接将其他值重新分配给它。)

Thank you! 谢谢!

  1. You must have the null terminator. 您必须具有空终止符。 From the documentation: "bytes - A NULL-terminated C array of bytes in UTF-8 encoding. This value must not be NULL." 在文档中:“ bytes-以UTF-8编码的NULL终止的C字节数组。此值不能为NULL。”
  2. No. The compiler will insert implicit release of the previous value and retain the new one since you declared foo with (implicit) strong semantics. 不会。编译器将插入前一个值的隐式释放,并保留新值,因为您使用(隐式)强语义声明了foo。 From the documentation: "__strong is the default. An object remains “alive” as long as there is a strong pointer to it." 在文档中:“ __ strong是默认值。只要有很强的指针指向对象,该对象就会保持”活动”状态。”
  3. In general, no. 一般来说,没有。
  1. An explicit \\0 is not required because in C (and hence Objective C), quoted string literals are null-terminated implicitly by the compiler. 不需要显式\\ 0,因为在C中(因此在目标C中),引用的字符串文字被编译器隐式终止为空。 Here's a similar question. 这是一个类似的问题。

Do string literals that end with a null-terminator contain an extra null-terminator? 以null终止符结尾的字符串文字是否包含额外的null终止符?

  1. No memory leakage. 没有内存泄漏。 The ARC-configured compiler will generate code to release the first string that was being referenced before assigning the new string. 配置了ARC的编译器将生成代码,以在分配新字符串之前释放被引用的第一个字符串。

  2. No change. 没变。 You may get a compile-time warning if the types aren't compatible. 如果类型不兼容,则可能会收到编译时警告。

For the first question, what Apple's official documentation tells me is: 对于第一个问题,Apple的官方文档告诉我的是:

Returns a string created by copying the data from a given C array of UTF8-encoded bytes. 返回一个字符串,该字符串是通过从给定的UTF8编码字节的C数组复制数据而创建的。

  • (id)stringWithUTF8String:(const char *)bytes Parameters bytes A NULL-terminated C array of bytes in UTF8 encoding. (id)stringWithUTF8String:(const char *)bytes参数bytes一个以NULL终止的C字节数组,采用UTF8编码。

But since string literal is NULL terminated by default (as @TomSwift points out), it's okay to omit it. 但是由于字符串文字默认情况下为NULL终止(如@TomSwift指出的那样),可以省略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM