views:

128

answers:

3

I've got a problem (in Objective-C/iPhone dev) since more than one week, so I'd be really grateful if someone can help me out.

When I instantiate an object from a class that I've written, it returns me nil, but when I launch the debug mode I actually see in the init method that the attributes of self are correctly initialized and it seems like it doesn't execute the return self instruction.

EDIT:

Thanks for your answers Here is the init code

-(id)initWithDate:(NSString *)aDate
             type:(NSString *)aType
           amount:(NSString *)anAmount
         currency:(NSString *)aCurrency
     merchantName:(NSString *)aMerchant
           status:(NSString *)aStatus
{
    if (!(self = [super init])) return nil;
    self->date=aDate;
    self->type=aType;
    self->amount=anAmount;
    self->currency=aCurrency;
    self->merchantName=aMerchant;
    self->status=aStatus;
    return self;
}
+1  A: 

I'd put your code inside the following

    if (self = [super init]) {
        // Custom initialization
    }
    return self;

rather than the if (!(self... return nil you have used. But that's just a habit.

I would also avoid the C++ style '->' assignments and instead use self.currency=aCurrency; (or [self setCurrency:aCurrency]; which is closer to the c++ calls I guess) assuming these are declared as @property or have getters and setters.

I'm sure one of those will get you going!

Andiih
`self.currency` only works if `currency` has been declared as an `@property` (or if he has a `setCurrency` method).
Dave DeLong
Actually, using `if (!(self = [super init])) return nil;` gets rid of the whole-method-long `if` instruction which I personally despise. I use this method too :)
Adam Woś
I thought the c++ -> was a method caller and required the same thing. My mistake.
Andiih
Now I've thought about it, I agree Adam.
Andiih
Also, Wil Shipley's take on initializer form is worth reading: http://www.wilshipley.com/blog/2005/07/self-stupid-init.html
Brad Larson
+1  A: 

Don't use self->instanceVariable.

Just use instanceVariable.

mouviciel
A: 

You should write

self.date = aDate;
// etc.... 

if date etc. is declared as a @property in the @interface, or if it's just a class instance variable, use

date = aDate;
// etc...

Also, if the strings are not declared as @property with retain or copy modifiers, you'll need to manually retain them thus:

date = [aDate retain];
Adam Woś
Thanks. But I don't think this is the actual problem, even though every attribut is a property. Actually, I just wrote it with -> to avoid the properties mechanisme to trigger, for performance reasons. May be i'm completely wrong since I'm in Objective-C only for one month... :)
Archanimus
"to avoid the properties mechanisme to trigger, for performance reasons" - trying to optimize something prematurely is one of the **worst** possible errors people do to increase performance... Using properties (with their `retain`/`copy`/`assign` types) lets it easier to manage memory correcly in Objective-C - I strongly suggest you use it, and optimise performance only after you've established where your real bottlenecks are.
Adam Woś
just use date=aDate to avoid triggering the setters. However, you will start to leak if you are not very careful.
Andiih
Thank you for the advise !
Archanimus