Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS iOS error

I know, I know. I've Google and searched here but can't figure out what's going on.

Anyway, i'm getting this error when using the following code, i'm sure it's something small. I know it's to do with memory management and down to how i'm handling my object in the main method.

Here's my main code:

Person *p1 = [[Person alloc] init];
[p1 initWithFirstname:@"John" lastname:@"Doe" andAge:23];

outputText.text = [p1 getDetails]; // App crashes inside getDetails

Then in the person class here's the two relevant methods:

-(Person *)initWithFirstname:(NSString *)_firstname lastname:(NSString *)_lastname andAge:
(int)_age {
self = [super init];

if(self) {
self.firstname = _firstname;
self.lastname = _lastname;
self.age = _age;

}
return self;
}


-(NSString *)getDetails {
 return [NSString stringWithFormat:@"%@ %@, %@", firstname, lastname, age];
}
like image 412
jim Avatar asked Dec 05 '22 22:12

jim


1 Answers

Judging by your init method, age is an int which means the format specifier is wrong. Try this:

-(NSString *)getDetails 
{
    return [NSString stringWithFormat:@"%@ %@, %d", firstname, lastname, age];
    //                                         ^^ format specifier for an int
}
like image 68
JeremyP Avatar answered Dec 24 '22 13:12

JeremyP