Constructors and methods

Discussion in 'iOS Programming' started by MickeyT, May 9, 2010.

  1. MickeyT macrumors member

    Joined:
    Apr 26, 2010
    Location:
    Newcastle, United Kingdom
    #1
    Hello all

    After having read a C# book I just wanted to confirm whether I'm seeing what I think I'm seeing in Objective C.

    When you have something like the following, is this one of the constructor methods available to the object being called:

    Code:
    hopRate=[[NSString alloc] initWithFormat:@"%1.2f hops", 1/(2-animationSpeed.value)];
    Is the "initWithFormat' one of the available constructors? In C#, I think the constructor has the same name but several combinations of parameters (called overloading??????)

    Therefore, if you just wrote "init" then would that perform the most basic setup?

    Finally, on methods in general, is the syntax for invoking one of an object's methods basically the following:

    [objectname methodname : parametervalue parametername : parametervalue];

    I have not yet bought a dedicated Objective C book (I will soon), and I'm used to the "objectname.methodname(parameter, parameter, parameter)" notation.

    Many thanks for any help.
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    The term constructor isn't used in Objective-C. You instantiate (allocate) and initialize. As you'll see in a moment, this could be combined or not. The act of instantiating is the act of allocating memory for a new instance of a class, otherwise known as an object. Any instance variables will be initialized to nil for objects, and zero for numbers.

    The most basic way you'll see a new object being created is like the following; (Note, is this example, that for a string you'd likely want to use NSMutableString instead of NSString.)
    Code:
    ClassName * myObject;
    myObject = [[ClassName alloc] init].
    Here are three equivalent ways of returning a new initialized NSString as per your question sample;
    Code:
    NSString * str1 = [[NSString alloc] initWithFormat:@"%1.2f hops", 1/(2-animationSpeed.value)];
    
    NSString * str2 = [NSString alloc];
    str2 = [str2 initWithFormat:@"%1.2f hops", 1/(2-animationSpeed.value)];
    
    NSString * str3 = [NSString stringWithFormat: @"%1.2f hops", 1/(2-animationSpeed.value)];
    The first two are identical, except for the fact that I've broken out the initialize instance method from the alloc class method onto a separate line. For the last sample I've used an class method that effectively combines the class and instance method I've used in the first two samples. If you look up the NSString class, you'll see there are several init instance methods and similar class methods.

    Another difference is since I'm not using automated garbage collection, the first two samples return retained objects that I'm responsible for releasing, while the last one returns an autoreleased object that I would have to retain if I want to keep it around beyond the life of the method I'm in. Garbage collection is a whole other subject.

    There isn't overloading in Objective-C. You use different and unique method names.

    For methods where you are passing a variable, the name is typically made up of a label and a colon. Convention seems to be that for a given label, the colon immediately follows it with no space in between. Experiment. By convention your sample would be properly formatted as;
    Code:
    [objectname methodname: parametervalue parametername: parametervalue];
    The label and colon are considered part of the method name.

    Arguments being past must be declared. The method name "::" is valid and does require a definition of the arguments being passed, such as;
    Code:
    - (void) : (int) anInteger : (id) anObject;
    Naturally, if you wanted another method that pasts the same number arguments and types in that order, you'd have to add a label to distinguish the new method from this one. Also, you cannot have a method with the same name but different arguments. The compiler will tell you that you have declared the same method twice, regardless of argument type.

    Convention is that you use descriptive labels so that your method names are effectively self documenting. So, my "::" example above is not recommended.

    There are some nice intro articles over at Cocoadevcentral.

    I hope I managed to word this well. I'm sure a good book will clarify this topic.
     

Share This Page