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

Prasoon

macrumors newbie
Original poster
Jul 28, 2012
1
0
Can you please explain the code below?

Code:
-(id)initWithName:(NSString *)newName atFrequency:(double)newFrequency;
 
Last edited by a moderator:

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
It is a declaration of an Objective-C object instance method with 2 parameters. Presumably it initializes a previously allocated object.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Can you please explain the code below?

Code:
-(id)initWithName:(NSString *)newName atFrequency:(double)newFrequency;

mfram explained it well, but I thought I'd break it down by component.
-
This indicates an instance or object method. This means you call this method on an instance of this class. The other option is a + which you call on the class itself. This is used for utility methods, object factories, etc.
(id)
This indicates the return type, which is id in this case. id is the most generic object pointer. It can point to any sort of object.
initWithName:
This is part of the method name. Objective-C methods have "segmented" names with arguments interspersed so each argument has a label, making it easier to know what the argument does, rather than just its type.
(NSString *)
This indicates the type of the first argument. NSString is the Cocoa string implementation. The * indicates that the type is a pointer to an NSString. You will see this for every object type, as Objective-C dynamically allocates all objects on the heap, so you never have a "local" object used without a pointer.
newName
This is the name of the first argument, and what will be used in the method to refer to it.
atFrequency:(double)newFrequency
This is the second segment of the method name, the type of the second argument (double is a double precision floating point value. It is a primitive type.), and the name of the second argument.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.