开发者

Why my Objective-C object doesn't want to be instantiated?

开发者 https://www.devze.com 2022-12-16 01:47 出处:网络
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.

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;
}


Don't use self->instanceVariable.

Just use instanceVariable.


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!


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号