|Apr 14, 2011, 02:50 AM||#1|
objective C help
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.
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.
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.
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
|Apr 14, 2011, 03:56 AM||#2|
+date is class factory method. -init is an initialiser.
You couldn't call init without calling alloc. For example, this code won't work:
Data *d = [Data init];
Data *d = [[Data alloc] init]
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 04:10 AM. Reason: Added clumsy object vs class method explaination
|Apr 14, 2011, 08:07 PM||#5|
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.
Mr. Paul, sir, I thought you should be advised, there seems to be a zombie tribble clinging to your head, for it is scarfing your brain
|Apr 16, 2011, 10:25 PM||#6|
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Trying to convert object to NSMutableArray||johnmerlino||iPhone/iPad Programming||4||Nov 11, 2011 04:08 AM|
|CoreDataBooks Help!||ahan.tm||iPhone/iPad Programming||2||Oct 10, 2011 04:07 PM|
|help with excel for mac 2008||damiang72||Mac Applications and Mac App Store||0||Nov 14, 2010 07:19 AM|
|Objective C Help||Furtim||Mac Programming||7||Oct 8, 2009 05:31 AM|
|Core Data migration, missing source managed object model. Help!||GeeYouEye||Mac Programming||0||Jul 9, 2009 01:09 PM|
All times are GMT -5. The time now is 01:06 PM.