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];
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With