Amazon.com Widgets setto: and over:
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 25, 2014, 10:06:08 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Programming in Objective-C, 4th edition
| |-+ Chapter 18
| | |-+ setto: and over:
Pages: [1] Go Down
Print
Author Topic: setto: and over: (Read 3276 times)
trissc
Newbie
*
Posts: 2






on: January 17, 2013, 05:37:54 AM

can seem to get it to run on exercise 18.3.
my fraction.h is the following:
#import <Foundation/Foundation.h> // The Fraction class
@interface Fraction: NSObject <NSCopying>
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) setTo: (int) n over: (int) d;
-(int) numerator;
-(int) denominator;
-(double) convertToNum;
@end
and fraction.m
#import "Fraction.h"
@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;
}
-(int) numerator
{
    return numerator;
}
-(int) denominator
{
    return denominator;
}
-(double) convertToNum
{
    if (denominator != 0)
        return (double) numerator / denominator;
    else
        return NAN;
}
-(void) setTo: (int) n over: (int) d
{
    numerator = n;
    denominator = d;
}
-(id) copyWithZone: (NSZone *) zone {
    Fraction *newFract = [[Fraction allocWithZone: zone] init];
    [newFract setTo: numerator over: denominator];
    return newFract;
}
@end

main.m

#import "fraction.h"
int main (int argc, char * argv[]) {
    @autoreleasepool {
        Fraction *f1 = [[Fraction alloc] init];
        Fraction *f2;
        [f1 setTo: 2 over: 5];
        f2 = [f1 copy];
        [f2 setTo: 1 over: 3];
        [f1 print];
        [f2 print];
    }
    return 0;
}
 i get 4 errors
two says on: [f1 setTo: 2 over: 5]; and [f2 setTo: 1 over: 3]; No visible @interface for 'Fraction' declares the selector 'setTo:over
and the other two says on: [f1 print]; and [f2 print]; No visible @interface for 'Fraction' declares the selector print

Help? thanks
Logged
afterDark
Global Moderator
Full Member
*****
Posts: 131






Reply #1 on: January 17, 2013, 02:38:44 PM

hi!

I copied and pasted your code in a new project to test it out... and I get no warnings at all! Although I found it a bit strange that you called your class files fraction.h instead of Fraction.h, where your implementation file is #importing Fraction.h, the main.m is #importing fraction.h. But all that works somehow, I learned today that capitalization is not important in .h files apparantly.

So the funny thing is your code works perfectly, without warnings or errors, here. I am on XCode 4.5.1
Logged

I am just an amateur with Objective-C, don't let the moderator label fool you. Working my way through the book slowly.
trissc
Newbie
*
Posts: 2






Reply #2 on: January 18, 2013, 04:54:45 AM

hi!

I copied and pasted your code in a new project to test it out... and I get no warnings at all! Although I found it a bit strange that you called your class files fraction.h instead of Fraction.h, where your implementation file is #importing Fraction.h, the main.m is #importing fraction.h. But all that works somehow, I learned today that capitalization is not important in .h files apparantly.

So the funny thing is your code works perfectly, without warnings or errors, here. I am on XCode 4.5.1

hmm u were right. i created a new one and copied and pasted what i post up and everything is fine...weird :/ and my ver is 4.5.2
Thanks for ur reply btw Cheesy
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.