I am reading the book Programming in Objective-C 2.0 by Stephan G.Kochan 3rd edition I tried this code sample
When creating an instance I am getting an error
Then I changed the code to
The sample code which was mentioned in the book didn't work but the
tweaked code works perfectly why ?
I am using XCODE 4.1 !
PHP:
//
// main.m
// fractions
//
// Created by XFactor on 11/13/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
//interface
@interface Fraction : NSObject {
int numerator;
int denumerator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDeNumerator: (int) d;
@end
//implementation
@implementation Fraction
-(void) print{
NSLog(@"%i/%i", numerator,denumerator);
}
-(void) setNumerator:(int)n{
numerator = n;
}
-(void) setDeNumerator:(int)d {
denumerator = d;
}
@end
// main program
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *myFraction;
[B]
//Create instance of a fraction
myFraction[ = [Fraction alloc];
myFraction = [Fraction init];
[/B]
//setFraction to 1/3
[myFraction setNumerator:1];
[myFraction setDeNumerator:3];
// Display the fractions using print method
NSLog(@"The value of my fraction is:");
[myFraction print];
[myFraction release];
[pool drain];
return 0;
}
When creating an instance I am getting an error
PHP:
2011-11-13 21:44:24.492 fractions[639:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[Fraction<0x100001218> init]: cannot init a class object.'
*** First throw call stack:
Then I changed the code to
PHP:
myFraction = [[Fraction alloc] init];
The sample code which was mentioned in the book didn't work but the
tweaked code works perfectly why ?
I am using XCODE 4.1 !
Last edited: