+/ - Functions

Discussion in 'iOS Programming' started by Nnavick, Apr 1, 2011.

  1. macrumors regular

    Joined:
    Oct 13, 2010
    #1
    what is the different between

    Code:
    +(void)blabla;
    and

    Code:
    -(void)blabla;
    what the - and + do?


    thanks!
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
  3. Sykte, Apr 2, 2011
    Last edited: Apr 2, 2011

    macrumors regular

    Joined:
    Aug 26, 2010
    #3
    - sends the message (method) to a particular instance of your class (object), whereas the + sends the message to the class itself.

    So an example of a class method (+) would be with NSString.

    Code:
    [NSString stringWithString:@"test"]; //this is a class method
    An example of an instance method (-) with NSString.

    Code:
    NSString *mystring = [[NSString alloc] initWithString:@"test"];  //creating my instance
    [mystring compare:@"test"]; //this is an instance method
    
     
  4. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #4
    If you're coming from another programming language like C++ you can kind of think of the (+) functions as static member functions. And they are often used as a pseudo factory methods. For example a car class

    Code:
    + (id)carWithEngine:(Engine *)engine;
    
    vs

    Code:
    - (id)initWithEngine:(Engine *)engine;
    
     

Share This Page