user179169
user179169

Reputation:

Allocating/Initialising objects

Was just wondering, which way is better to fully initialise an object:

NSString *myString = [[NSString alloc] init]; 

OR

NSString *myString = [NSString new]; 

thanks

Upvotes: 0

Views: 67

Answers (2)

Anshu Chimala
Anshu Chimala

Reputation: 2800

There is absolutely no difference at all between new and alloc+init; new is just a "convenience allocator." That said, one typically sees alloc+init used more than new, simply for the sake of consistency.

Upvotes: 0

Daniel A. White
Daniel A. White

Reputation: 190907

NSString *myString = [[NSString alloc] init];

This is the preferred way. The majority of code I have seen for iPhone does not use new. Don't for get to release it if you use alloc then init.

Upvotes: 3

Related Questions