Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Why do you need to "alloc" for initWithCString and but doesn't for stringWithFormat ?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="xstep" data-source="post: 922845" data-attributes="member: 11647"><p>In the first one, you are using an NSString template that will allocate the space needed to return an NSString type for what you are creating.</p><p></p><p>In the second one, you are converting a C string which has different memory requirements, so you have to first allocated memory for an NSString type.</p><p></p><p>Note also that initWithCString is deprecated. See the <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:" target="_blank">NSString Class reference</a>.</p><p></p><p>Lastly, the crasher occurs because you are trying to use a class method with an object instance. When you see the documentation mentioned above, you'll notice class methods have a plus (+) sign in front of them while an instance method has a minus (-) sign in front of them. A class method can only be used with the class name while a instance method works with instance variables you have have allocated.</p></blockquote><p></p>
[QUOTE="xstep, post: 922845, member: 11647"] In the first one, you are using an NSString template that will allocate the space needed to return an NSString type for what you are creating. In the second one, you are converting a C string which has different memory requirements, so you have to first allocated memory for an NSString type. Note also that initWithCString is deprecated. See the [URL="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:"]NSString Class reference[/URL]. Lastly, the crasher occurs because you are trying to use a class method with an object instance. When you see the documentation mentioned above, you'll notice class methods have a plus (+) sign in front of them while an instance method has a minus (-) sign in front of them. A class method can only be used with the class name while a instance method works with instance variables you have have allocated. [/QUOTE]
Verification
Name this item. 🍎
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Why do you need to "alloc" for initWithCString and but doesn't for stringWithFormat ?
Top