Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan

Old Stuff => Chapter 3 - Classes, Objects, and Methods => : pro11 December 26, 2010, 12:56:56 AM



: Program 3.2 SIGABRT error message
: pro11 December 26, 2010, 12:56:56 AM
I'm getting this error message: GDB: program received signal: "SIGABRT."
Ive reviewed my code several times and cant find whats wrong with it.
I would really appreciate if you guys can point out my mistake.
Here's my code:
: (Objective-C)
//Program to work with fractions - class version
#import <Foundation/Foundation.h>

//---- @interface section ----

@interface Fraction: NSObject
{
int numerator;
int denominator;
}

- (void) print;
- (void) setNumerator: (int) n;
- (void) setDenominator: (int) d;

@end

// ---- @implementatioin section ----

@implementation Fraction
- (void) print
{
NSLog (@"%i/%i", numerator, denominator);
}

- (void) setNumerator: (int) n
{
numerator = n;
}

- (void) setDenominator: (int) d
{
denominator = d;
}

@end

// ---- program section ----

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction;

//Create an instance of a fraction

myFraction = [Fraction alloc];
myFraction = [Fraction init];

//Set fraction to 1/3

[myFraction setNumerator: 1];
[myFraction setDenominator: 3];

//Display the fraction using the print method

NSLog (@"The value of myFraction is:");
[myFraction print];
[myFraction release];

[pool drain];
return 0;
}



: Re: Program 3.2 SIGABRT error message
: skochan December 26, 2010, 01:19:29 PM
The init method is an instance method, not a class method.  So you need to send that message to an instance from the class, which in this case is myFraction.

Cheers,

Steve


: Re: Program 3.2 SIGABRT error message
: pro11 December 27, 2010, 11:11:37 PM
That works, Im having a hard time understanding why but Im sure it will make sense later.

Thanks a lot Steve.


: Re: Program 3.2 SIGABRT error message
: pro11 December 28, 2010, 12:51:14 AM
Steve I have a question in regards to this chapter 3.2 program and in specific to this line.

: (Objective-C)
myFraction = [Fraction alloc]; 
myFraction = [myFraction init];

After reading the program section I would like to ask what is the difference or advantage, if theres any, to combine those two messages in the same line? Or is it just for coding style differences?

: (Objective-C)
myFraction = [[Fraction alloc] init];

Thanks!



: Re: Program 3.2 SIGABRT error message
: Jake December 28, 2010, 11:44:23 PM
Steve I have a question in regards to this chapter 3.2 program and in specific to this line.

: (Objective-C)
myFraction = [Fraction alloc]; 
myFraction = [myFraction init];

After reading the program section I would like to ask what is the difference or advantage, if theres any, to combine those two messages in the same line? Or is it just for coding style differences?

: (Objective-C)
myFraction = [[Fraction alloc] init];

Thanks!



It's does indeed give the same desire affect. However, it's often merge into one.
For example:

: (Objective-C)
@property int test1, test2;

and

: (Objective-C)
@property int test1;
@property int test2;

are exactly the same and give the same affect. However, isn't the second code a bit more to type? And so often, the alloc and init message are often merge into one.


: Re: Program 3.2 SIGABRT error message
: pro11 December 29, 2010, 10:34:20 PM
Thank you very much Jake!


: Re: Program 3.2 SIGABRT error message
: skochan December 30, 2010, 09:40:48 AM
Yeah, it's just easier to type.  The condensed version might be a bit more efficient as well, although that would likely be negligible.   Some might also argue that the compact version is easier to read (once you get used to it), as you can see the same object that being allocated is then being initialized.

Cheers,

Steve


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.