Is a literal NSString autoreleased or does it need to be released?

后端 未结 3 1243
执念已碎
执念已碎 2020-12-14 02:13

When creating a string using the following notation:

NSString *foo = @\"Bar\";

Does one need to release foo? Or is foo

3条回答
  •  天命终不由人
    2020-12-14 03:12

    As mentioned in the docs

    http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html

    You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

    Since you're not using alloc, copy, etc. you don't need to worry about releasing the object.

提交回复
热议问题