0

I am newbie to Objective-C. I have a 'XYZPerson' Class with attributes {firstName, lastName, dateOfBirth} and I want when I write "XYZPerson *person=[[XYZPerson alloc] init]" in main, it should call my overridden 'init' method which should in-turn call my designated initializer and initializes my object with the defined values.

My Code snippets. http://pastebin.com/FfxNDDhf

#import <Foundation/Foundation.h>
#import "XYZShoutingPerson.h"

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        XYZPerson *person=[[XYZPerson alloc] init];
        if(person) {
            [person sayHello];
        }
        else {
            NSLog(@"Person Object is NULL");
        }
    }
    return 0;
}

-(id)init
{
    self=[super init];
    return [self initWithFirstName:@"Ankit" LastName:@"Sehra" DOB:01/01/2000];
}

-(id)initWithFirstName:(NSString *)aFirstName LastName:(NSString *)aLastName DOB:(NSDate *)aDateOfBirth
{
    _firstName=aFirstName;
    _lastName=aLastName;
    _dateOfBirth=aDateOfBirth;
}

    -(void)sayHello;
{
    NSLog(@"%@ %@ %@",self.firstName,self.lastName,self.dateOfBirth);
}

Write now the output of the program is "Person Object is NULL", I want it to print the firstName, lastName and DOB.

2
  • Which files is that code in? What is in XYZPerson .h / .m?
    – Wain
    Jul 21, 2013 at 18:20
  • Hi ... The declarations are in .h files and implementations are in .m files. The code I pasted is in XYZPerson.m files. I think the problem was i did not put a 'return self' in the end of my designated initializer. Jul 21, 2013 at 19:02

2 Answers 2

1

If a class has several init methods, one of them is the "designated initializer". This is the one that calls [super initXXX]. In your example, initWithFirstName:LastName:DOB: would be the designated initializer and should look like this:

-(id)initWithFirstName:(NSString *)aFirstName LastName:(NSString *)aLastName DOB:(NSString *)aDateOfBirth
{
    self = [super init];
    if (self) {
        _firstName=aFirstName;
        _lastName=aLastName;
        _dateOfBirth=aDateOfBirth;
    }
    return self;
}

(I have changed the type of the dateOfBirth property and the DOB argument to NSString and will explain that later.)

All other init methods just call the designated initializer (and not [super initXXX]), in your example init:

-(id)init
{
    return [self initWithFirstName:@"Ankit" LastName:@"Sehra" DOB:@"01/01/2000"];
}

Note that (among several other errors), "01/01/2000" is not a NSDate, so

return [self initWithFirstName:@"Ankit" LastName:@"Sehra" DOB:01/01/2000];

does not make any sense. Therefore, to get a first working example for you, I have changed the argument type to NSString.

2
  • Hi, Thank you for the explanation. I got it working now, with my default 'init' calling my designated initializer. How should I pass the NSDate value ? Jul 21, 2013 at 19:07
  • 1
    @user2171983: There are many possibilities to create an NSDate value. For example, [NSDate date] returns the current date/time. To convert a string to NSDate, use NSDateFormatter (look up the documentation or google for NSDateFormatter, there are lots of Q&A about that on stackoverflow).
    – Martin R
    Jul 21, 2013 at 19:12
0

I'd bet the compiler is giving you plenty of warnings on that code. You should fix them. If it isn't, your compiler isn't tuned correctly.

First, your designated initializer doesn't return anything.

Secondly, there is no reason to override init and call your designated initializer. Simply do:

foo = [[Person alloc] initWithFirstName:@"bob" lastName:@"dobbs"];
4
  • Hi ... Yes you were right, I put a 'return self' at the end of my designated initializer and now I am able to get an initialized object in my main. However I am still not able to pass the date value. I am passing it like this 'return [self initWithFirstName:@"Ankit" LastName:@"Sehra" DOB:01/01/1988];' Jul 21, 2013 at 19:03
  • 01/01/1988 is a mathematical expression. It is not a date as far as the compiler is concerned.
    – bbum
    Jul 21, 2013 at 21:31
  • I thought that you should always override the superclass's designated initializer (presumably init) and in that call the current class's designated initializer.
    – nevan king
    Jul 24, 2013 at 11:05
  • @nevanking Not true; consider UIView. initWithFrame: is the designated initializer and it calls [super init]. (It likely also overrides init to call initWithFrame: with CGRectZero). There are many, many, many example of a subclass providing a more specific DI that calls a generic super's DI.
    – bbum
    Jul 24, 2013 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.