|Apr 14, 2011, 01: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, 02: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 03:10 AM. Reason: Added clumsy object vs class method explaination
|Apr 14, 2011, 07: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.
You got to be a spirit. You can't be no ghost.
|Apr 16, 2011, 09:25 PM||#6|
|Thread Tools||Search this Thread|
|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|
All times are GMT -5. The time now is 09:33 PM.