Search code examples
objective-cinterfaceidentifier

No visible @interface for 'Fraction' declares the selector 'add:'


I'm learning Objective-c programming and there are two errors I cannot solve. Could you tell me what's wrong?

#import "Fraction.h"
int main (int argc, char * argv[]) {
@autoreleasepool {
    Fraction *aFraction = [[Fraction alloc] init];
    Fraction *bFraction = [[Fraction alloc] init];
    [aFraction setTo: 1 over: 4];
    [bFraction setTo: 1 over: 2];
    [aFraction print];
    NSLog (@"+");
    [bFraction print];
    NSLog (@"=");
    [aFraction add: bFraction]; /*error 1: No visible @interface for 'Fraction' declares the    selector 'add:'*/
    [aFraction reduce];
    [aFraction print]; }
return 0; }


#import <Foundation/Foundation.h>
@interface Fraction : NSObject
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) add: (Fraction *) f; 
-(void) reduce; /*error 2: Expected identifier or '(' */
@end

Solution

    1. Grab TextWrangler from App Store

    2. Open the offending .h file

    3. Select Text -> Zap Gremlins...

    4. Check "Substitute with •"

    5. [ZAP!]

    ...

     -(double) convertToNum;
     ••••••••••••••••••••••••••••••••••••••-(void) add: (Fraction *) f; 
     -(void) reduce; /*error 2: Expected identifier or '(' */
    

    ...

    There's your problem right there!


    Phillip is most likely correct. And I bet it is a ctrl+return. Try this:

    Go to the end of the line that declares the reduce: method, then hit ctrl+a.

    The cursor will likely jump back to the beginning of the line that declares the add:. This happens if you happen to hit ctrl+return at the end of a line.

    To fix, go to the beginning of the line with reduce:, hit backspace, then hit return.


    Example:

    -(void) add: (Fraction *) f; 
    -(void) reduce; <*** cursor here, hit ctrl-a
    

    After pressing ctrl-a, the cursor will be at the beginning of the line declaring add:. That means you have a bad return character at the end of the add: line.

    Check the line before and after, too. Philip said he saw funky characters at the beginning of the add: line. Also, Xcode does have a "show hidden characters" feature. That might help but, in my experience, the bad newlines are invisible.