Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 14, 2011, 01:50 AM   #1
daproject85
macrumors member
 
Join Date: Apr 2011
objective C help

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
daproject85 is offline   0 Reply With Quote
Old Apr 14, 2011, 02:56 AM   #2
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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 by jiminaus; Apr 14, 2011 at 03:10 AM. Reason: Added clumsy object vs class method explaination
jiminaus is offline   0 Reply With Quote
Old Apr 14, 2011, 03:59 AM   #3
Hansr
macrumors 6502a
 
Join Date: Apr 2007
Quote:
Originally Posted by jiminaus View Post
If you know what a static method is in C++, it's the same.
All that was needed
Hansr is offline   0 Reply With Quote
Old Apr 14, 2011, 05:54 PM   #4
findhornriver
macrumors newbie
 
Join Date: Oct 2006
Location: Maine
Great explanation! Thanks
findhornriver is offline   0 Reply With Quote
Old Apr 14, 2011, 07:07 PM   #5
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
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.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Apr 16, 2011, 09:25 PM   #6
daproject85
Thread Starter
macrumors member
 
Join Date: Apr 2011
Quote:
Originally Posted by jiminaus View Post
+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!!
daproject85 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
I Need Help Learning Objective-c!! Woodshark69 Mac Programming 11 Dec 14, 2012 07:21 AM
Objective C Accessors ag2web Mac Programming 9 Nov 18, 2012 02:53 PM
Problems with Objective-C++? frank100 Mac Programming 5 Aug 8, 2012 04:49 PM
Testing objective-c Evre iPhone/iPad Programming 21 Jul 12, 2012 07:22 AM
Objective C Syntax nashyo iPhone/iPad Programming 6 Jul 4, 2012 11:56 AM

Forum Jump

All times are GMT -5. The time now is 10:58 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC