Parameters for objective-c class?

Discussion in 'Mac Programming' started by Macman1993, Oct 22, 2010.

  1. macrumors 6502


    (Sorry guys if I sound stupid I am totally new to objective-c coming from a background in java)

    Can someone please explain (or point to a document) how make a class that requires parameters? I understand how to pass parameters to methods but I got somewhat lost with passing them to classes. I am trying to make a very simple class called person. It needs two basic parameters (a name and an age) but I have no idea how to do it. In Java it would be like

    Creating the object with the two parameters (name and age)
    personX = new Person ("bob", 18);
    and the constructor for the person class would be like
    public Person (string name, int age) {
    ...code for the class
    but I just don't understand how to do it in objective-c. Could someone please explain the syntax or point me to a document. I know I probably sound really dumb but the author of my book chose to not to explain the syntax (or I haven't been able to find it). Any answer that explains passing methods to a class would be a big help, it doesn't need to be an objective-c equivalent to my above code I just really need to know how to pass some parameters to my classes.
  2. macrumors 603


  3. macrumors 68040


    Very briefly:
    You are used to a constructor that, when called, will allocate space for a new object implicitly, then begin a method that can accept arguments to initialize the object's state. The new keyword is used in combination with the class name and any arguments to a constructor.

    In Objective-C, things are similar, but are broken into an alloc and an init stage. Passing the alloc message to a class object will return a pointer to space for the new object, which will be uninitialized. The first message that is passed to this new object is an init message. Traditionally initializers start with init, then describe any parameters i.e. initWithString:. The initializer can accept arguments which can be used to setup your object's state. You also call the parent class's initializer in this method. Sometimes an initializer will return a pointer to a different object (a singleton for a specific init value, say) than was returned from alloc, so the normal pattern is:
    myAwesomeObject *awesomeSix = [[myAwesomeObject alloc] initWithInt: 6];
    This really isn't that different, but the alloc and init are more explicit than new and a constructor.

    This doesn't cover factory methods, obviously, but that's for another day.

    For your example, you'd have an init method called initWithName:andAge:. In this method you'd call init on the superclass, then set your ivars using the passed in values. Assuming properties with synthesized getters/setters:
    - (id) initWithName: (NSString *) newName andAge: (int) newAge {
      if(self = [super init]) {
        [self setName:newName];
        [self setAge:newAge];
      return self;
    You'd call this:
    mmPerson *myBuddyMax = [[mmPerson alloc initWithName:@"Max Sblongrass" andAge:22]];


    Edit: ended up not-so-brief. You asked about requiring parameters, not just allowing them. This is a bit tough because NSObject responds to init with no parameters. You could override init and throw an exception if it's used, but I'd say the best way to go is to only document the init method with the parameters.
  4. macrumors 68030


    mmPerson *myBuddyMax = [[mmPerson alloc] initWithName:@"Max Sblongrass" andAge:22];
    Secondly Objective-C uses a two stage initializer, the reason for this is to separate allocation from initialization with +allocWithZone: and what people call "the designated initializer"

    Most people will not override -init to throw an exception, they would override it to call their own designated initializer with default values (but I guess if you REALLY want to require them...)

    In the above example:
    - (id)init {
    return [self initWithName: @"unnamed" andAge: 0];
  5. macrumors 68040


    Absolutely right. I was trying to devise a way to *require* the use of a paramterized init method, like you could do by only having one constructor with parameters in Java and couldn't come up with one.

Share This Page