Difference between [alloc [init]] and [new]

Is there a difference between these two syntax: [alloc [init]] and [new]. Apple uses [new] when they allocate the memory pool, so why do all developers use the convention [alloc [init]] instead of the short form [new] ?

I tried using [new] and it seems to do exactly the same thing as [alloc [init]]

Any clarification would be appreciated.

Thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Conveniance allocators

Depends on the Class you are invoking this. Most probably it's a conveniance allocator you're speaking of, which calls [[XY alloc] init] internally.

You are correct, if there are conveniance allocators available, you should use them. But sometimes there are none, and sometimes you don't want autoreleased objects (which the conveniance allocator returns).

new vs alloc-init

The new method actually comes from NeXT days. Back then, there was no two phase initialization, it was just a single new method.

They soon realized that a two phase approach could have advantages, and introduced alloc. new was kinda deprecated, but kept in for backwards compatibility. It is exactly the same as alloc-init.

Use 'new' if it suits. One shortcoming is that it only works with the basic 'init' initializer, and will not work with other initializers (eg initWithString:).

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org

Thanks

Thank you for the clarification - much appreciated.

new does not return an autoreleased object

Contrary to what is said by one of the comment, the method new does not return an autoreleased object (contrary to convenience methods such as 'array' for NSArray or 'string' for NSString,...).

new is equivalent to alloc, but does not necessarily rely on alloc: see this thread on Cocoabuilder.

alloc init

That is an interesting thread. I could surmise from the thread that "new" is "like" alloc but may NOT behave like alloc in all situations. Coming from DOS/OS2/Windows development, I have observed that there are a number of scenario that are not very clear in MAC development and this is just one of the case.

I have now spent three months ONLY looking at MAC development and I am really surprised to note that the concept of properties, garbage collections, enumerations etc have only now been implemented. These features have been present in other languages and libraries such as JAVA and C# quite a long time back. Also the documentation seems to be spars. It seems as if Apple does not consider developers a critical component in the life cycle unlike Microsoft who consider the developers as critical components to the success of the platform.

Mac Development

I don't think I agree that Mac Development is in some way 'worse off' than on Windows. I think you are looking in the wrong place.

Yes, Obj-C has just got GC and properties, and, yes, Java and C# have had them for a long while. But that's why Java is almost dead as a desktop API. The performance of Java in the beginning was so poor, that it never recovered in the Desktop space. That's an example of adopting a technology too early.

Computers are now at a point where GC is not a critical performance bottleneck, and it is right that Apple should now offer that option. But 10 years ago, it was right not to offer it. Mac OS X would never have gotten off the ground if all Mac OS X 10.0 apps had GC. (It was slow enough as it was ;-) )

As for properties: they are just frosting. Nice little time saver, but nothing more than that.

I also disagree that Apple's docs are poor. They were certainly poor in the beginning, but now you can read up on any aspect of Mac OS X development from Xcodes documentation browser, and there are heaps of samples too. (There is no real discussion about 'new': it is documented, and is equivalent to alloc-init.)

When windows developers move to the Mac, they are inclined to be disappointed about what Windows development has that Mac Development doesn't, but they miss what the Mac has that Windows doesn't, and in my mind that is more important than GC or properties: dynamic typing. This is so useful in many ways (eg Cocoa bindings, Key-Value Observing, Interface Builder, bridging to scripting languages); you can certainly approximate these things in statically typed languages, but graceful it ain't.

My advice is to stop reading about Mac Development, and dive in. Actually develop a little Cocoa app in Xcode with IB. You'll be in love in no time, and forget that you ever said nasty things about Mac development ;-)

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org

MAC Development

Thanks Drew - I agree with some points and I disagree with some points.

1. I looked at CoreData as example - it is an excellent implementation but incomplete. If you are creating a data persistence store API, that API has to be able to connect to any back end - eg in .NET one uses ADO.NET which allows you to bind to any backend thus there is a level of abstraction where the developer focuses on creating the business logic rather than the intricacies of connectivity etc. Secondly ADO.NET surfaces up the appropriate methods related to database application development which is 80% of desktop application requirement such as forward cursor, back cursor etc. If CoreData framework is not supposed to be the Framework for such work than which framework does one use ?

2. I agree with you that possibly the time was not right as the power of desktop had not matured to handle the JVM. May be now is the right time. But for garbage collection is not necessarily relegated to just desktop app.

3. Properties - agreed is it candy, but less code and clearer code to read ie less clutter. nothing serious here.

4. As for documentation, I read the "Cocoa Fundamental" that book is exceeding well written as for the other I am not too sure. I am still looking at those docs. Although I do not see a proper sample of each and every class. This is a must when you are on the exploratory ground something I think that VS.NET has done well.

5. The observer pattern is a definite plus and that is how messages should move around!. Binding already is present on .NET. Obviously there will always be pros and cons in either development environment and one has to accept things as they come along.

6. I am actually writing some code - heavy stuff... will push it out in due course and NO I did not say nasty things.. I just expressed my thoughts loudly. I love the MAC platform - I started my career writing dbaseII applications and VISCALC templates on an APPLE IIe! and moved on to writing devices drivers in DOS and then to OS/2 and of course with no recourse to Windows.

from the Apple's mouth

There was a very long thread on this same subject (alloc/init vs. new) on the cocoa-dev mailing list this week (search for "[Foo new] vs [[Foo alloc] init]"). Unfortunately the documentation is not crystal clear on this, but Bill Bumgarner (an Apple Engineer) confirmed that new is implemented as allocWithZone/alloc followed by init back at least to the beginning of OS X. So the answer is, use whichever you prefer. The current vogue in Cocoa programming is to use alloc/init because it makes the intended behavior explicit.

Mac Development

I'm not really in a position to comment on things like bindings in .NET. I can easily believe that KVC is possible, but I imagine that KVO would be difficult in a statically typed language. Here is an article that discusses these issues in relation to Java, which is similar to C#:

http://waffle.wootest.net/2007/02/16/the-planks-in-the-bridge/

What I can comment on is CoreData. I think you are misinterpreting what it is intended for. It is not intended to compete with ADO.NET or any other enterprise database abstraction layer. For that, you would want Enterprise Objects framework.

CoreData is intended for desktop applications. It replaces old fashioned reading and writing documents in files. The database that is used is just an efficient way to implement this reading and writing to file. In this regard, I think CoreData works very well. I believe there is something similar on Windows, but I don't know what it is called. Probably just some library in .NET.

As for Apple's docs, you should find that if you use the Xcode documentation browser, and search for a class, that there is sample data embedded in the class docs and in the overview articles. It can take a while to get used to how to best use the browser, but there is a lot in there.

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org

EOF

Thanks for your feedback much appreciated.

I will look into the other stuff you mentioned. Ob the subject of Enterprise Objects Framework - I though Apple has dropped that product and now has the web object framework which is a pure JAVA implementation. If it is still available and supported can you point me to the right place. I would love to look at that because my interest lies in creating database applications on desktop and at Enterprise Level.

Thanks

JD

Re: EOF

Yes, Apple has stopped shipping an Objective-C version of EOF, but there is a Java version built into Web Objects. I know a few people that develop database backed web apps with WO, and they love it. Definitely give it a look.

Drew

---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org

And the Answer

And the Answer Is...[drumroll]

This is an old post but the Internet is ageless so I thought I should toss my two cents in... or more accurately Apple's two cents. This is from the developer docs:

new
Allocates a new instance of the receiving class, sends it an init message, and returns the initialized object.

+ (id)new

Return Value
A new instance of the receiver.

Discussion
This method is a combination of alloc and init. Like alloc, it initializes the isa instance variable of the new object so it points to the class data structure. It then invokes the init method to complete the initialization process.

Unlike alloc, new is sometimes re-implemented in subclasses to invoke a class-specific initialization method. If the init... method includes arguments, they’re typically reflected in a new... method as well.

-

So, new is EXACTLY like [[object alloc] init] because it literally is just a convenience method that does it for you. In addition if there is an initWithBlahBlahBlah there will be a newWithBlahBlahBlah.

I felt this was important to note because the link that was provided above made no comment but just highlighed one argumentative guy's incorrect assumptions and unsupported assertions with, funny enough, an Apple engineer.

It's my conclusion than that you should always use new unless you specifically need to use alloc/init. Otherwise you look like someone that likes to do things the hard way when there's no reason to. Like you're trying to impress all the hot coder ladies with your l33t 5k1LLz.

So when SHOULD you use alloc and init? Well if you are making a group of classes where you have a dummy front-end class and a bunch of other classes behind the scenes that actually handle the different types; like the NSString and NSArray class clusters do.

Hope that helps and remember, the Internet hates you and will lie to you so always check the docs.