i'm trying to learn programming, and the language i chose to start is objective c! i'm currently studying from : Programing in Objective-C Fourth edition, and am a bit stuck at the classes chapter.
we have the following code :
#import <Foundation/Foundation.h>
@interface Fraction: NSObject;
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
@end
@implementation Fraction
{
int numerator;
int denominator;
}
-(void) print
{
NSLog(@"%i/%i" , numerator, denominator);
}
-(void) setNumerator:(int)n
{
numerator = n;
}
-(void) setDenominator:(int)d
{
denominator = d;
}
@end
int main (int argc, char * argv[])
{
@autoreleasepool {
Fraction *myFraction;
myFraction = [Fraction alloc];
myFraction = [Fraction init];
[myFraction setDenominator:1];
[myFraction setNumerator:3];
NSLog(@"The value of myFraction is :");
[myFraction print];
}
return 0;
}
I copied the code from the book, in hopes to help me understand it better, but for some reason when i try to run i get the following message :
2014-04-25 22:23:28.374 cocoTerminal[1751:303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[Fraction<0x1000011d8> init]: cannot init a class object.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8f5a825c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff86403e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8f5ab490 +[NSObject(NSObject) dealloc] + 0
3 cocoTerminal 0x0000000100000dde main + 110
4 libdyld.dylib 0x00007fff8d43a5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
If someone would be so kind to explain what and why is causing the error, to a complete noob i will be forever grateful !