Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
Hi guys,

im new to objective-c (have experience with C and C++). This might be a really stupid question but i don't quiet get the use of Class methods VS object methods (+xxx or -xxx) methods in a class.

EXAMPLE:
i know that code below returns the current date, stores it in date and we can print it to the log.
NSDate *date = [NSDate date];

but how do i use -init. the apple docs say
Returns an NSDate object initialized to the current date and time.

- (id)init
Return Value
An NSDate object initialized to the current date and time.


and for +date it says
Creates and returns a new date set to the current date and time.

+ (id)date
Return Value
A new date object set to the current date and time.


so whats going on??? whats the difference? they both return an object dont they??? thanks for the help
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
+date is class factory method. -init is an initialiser.

You couldn't call init without calling alloc. For example, this code won't work:
Code:
Data *d = [Data init];

To use init you'd need to use:
Code:
Data *d = [[Data alloc] init]

+alloc is a class method. It returns an uninitialised memory block big enough to hold a Data object. It's equivalent to C++ operator new. -init then initialises the memory block, it's equivalent to a C++ constructor.

Behind the scenes, +date is calling +alloc and -init for you.

There are other implications of using +date verses using +alloc/-init. You'd really want to read up on Objective-C object creation and memory management.

Don't think you can code Objective-C like C++ with weird syntax. Your C and C++ knowledge will only give you a jumpstart. You still need to learn properly how Objective-C and Cocoa do things.


To go further to you're question. Simply a class method expects a class as the first expression on the inside of the [], whereas an object method expects an object (an instance of the class) as the first expression on the inside of the [].

More fundamentally, a object method operates at the level of a particular object. It access the instance variables, etc. of a particular object. A class method, in contrast, operates the level of the class itself. It doesn't operate on a particular object and so cannot access the instance variables of a particular object. It operates at the level common to all the instances of that class, if you like.

Sorry, this is a clumsy explanation. If you know what a static method is in C++, it's the same. Hopefully someone can explain this fundamental concept more clearly.
 
Last edited:

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
Just a little more detail:

Many of the classes that ship with Cocoa have what are called "conveniece methods", which are usually class methods. If you are using "Reference Counted" memory management, you really should understand how these methods differ from their, shall we say, inconvenient alternates.

The +date method produces the same result as using alloc/init, with one notable difference. With +date, you get an "autoreleased" object, which means you may not expect it to be valid after a while (basically, after your last method ends, returning to the main run loop).

Many of the convenience methods have alternatives that begin with "-init", like "-stringWithFormat:" vs. "-initWithFormat:". These do the same thing, but the init-type method, which must be paired with "+alloc" returns and object that will stick around until you "-release" it.

All this is important because all Cocoa objects live in heap memory space, so you need to be mindful of their lifespans in order to avoid leaks and dangling pointers. Garbage Collection can relieve you of some of the effort involved in memory management, but it is a very good idea to learn Reference Counted memory management because it can be more efficient and is currently the only option on iOS.
 

daproject85

macrumors member
Original poster
Apr 13, 2011
37
0
+date is class factory method. -init is an initialiser.

You couldn't call init without calling alloc. For example, this code won't work:
Code:
Data *d = [Data init];

To use init you'd need to use:
Code:
Data *d = [[Data alloc] init]

+alloc is a class method. It returns an uninitialised memory block big enough to hold a Data object. It's equivalent to C++ operator new. -init then initialises the memory block, it's equivalent to a C++ constructor.

Behind the scenes, +date is calling +alloc and -init for you.

There are other implications of using +date verses using +alloc/-init. You'd really want to read up on Objective-C object creation and memory management.

Don't think you can code Objective-C like C++ with weird syntax. Your C and C++ knowledge will only give you a jumpstart. You still need to learn properly how Objective-C and Cocoa do things.


To go further to you're question. Simply a class method expects a class as the first expression on the inside of the [], whereas an object method expects an object (an instance of the class) as the first expression on the inside of the [].

More fundamentally, a object method operates at the level of a particular object. It access the instance variables, etc. of a particular object. A class method, in contrast, operates the level of the class itself. It doesn't operate on a particular object and so cannot access the instance variables of a particular object. It operates at the level common to all the instances of that class, if you like.

Sorry, this is a clumsy explanation. If you know what a static method is in C++, it's the same. Hopefully someone can explain this fundamental concept more clearly.

amazing explanation thank you!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.