2

There are two ways of initializing objects in Objective-C, i.e.:

1) AVCaptureSession *captureSession = [[AVCaptureSession alloc] init];

2) AVCaptureSession *session = [AVCaptureSession new];

Seems like they are doing the same job. What is the difference between them? Or "new" it is just a novelty of iOS5?

Would be grateful for the answer,

Artem

1 Answer 1

3

new is a shortcut way of doing an alloc/init that should work on any object, but it's mainly a hangover from Smalltalk and new calls never take arguments so you don't see it used much at all any more.

So the two should be identical and it's a novelty but not of iOS 5.

EDIT: further to this, new is defined on NSObject as something that calls alloc and then init and is given as having been available since Mac OS X 10.0 (which is the beginning of time as far as Apple's documentation is concerned).

Sign up to request clarification or add additional context in comments.

1 Comment

Note that many objects have designated initializers other than -init, so it's not appropriate to call -new. A prime example would be creating any type of NSView subclass (including all Cocoa controls) in code. The designated initializer is -initWithFrame: and takes a rectangle as an argument. Initializing a view with -init will give you unpredictable (and usually undesirable) results so you need the alloc/initWithFrame: pair. Lesson: Don't use -new just anywhere. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.