xcode instance help!

Discussion in 'Mac Apps and Mac App Store' started by Istrfly, Sep 14, 2004.

  1. Istrfly macrumors newbie

    Joined:
    Sep 14, 2004
    #1
    Hello, I am new to this forum, but I cannot find any help. I was wondering if anyone knew why the currency converter tutorial comes up with this error when trying to run it.

    2004-09-14 20:36:36.054 CurrencyConverter[1158] *** -[ConverterController convertAmount:atRate:]: selector not recognized
    2004-09-14 20:36:36.057 CurrencyConverter[1158] *** -[ConverterController convertAmount:atRate:]: selector not recognized

    Then nothing happens. I have checked again and again and my code is exactly like the tutorials. However... if I put a + instead of a - in front of the method, it works great. Can anyone please explain this?
    Thanks!
     
  2. SilentPanda Moderator emeritus

    SilentPanda

    Joined:
    Oct 8, 2002
    Location:
    The Bamboo Forest
    #2
    I'm pulling this from the "I did a few tutorials" memory so it might be wrong but I do know how to code (just not strong in Obj-C).

    The - in Obj-C makes the function private to the class whereas the + makes it public to all. Most likely your function is in the wrong class. For example:

    Class A has methods:
    +myPublicMethod
    -myPrivateMethod

    Class B could have the following

    A = new A;
    A.myPublicmethod;

    However it could not have:

    A.myPrivateMethod;

    Unless myPrivateMethod was in class B instead.

    That would be my guess as to your problem.
     
  3. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #3
    No, the - represents an instance method and the + represents a static method.

    I found a (working) copy of the tutorial that I did when I was learning Objective C, and it turns out that convertAmount:atRate: is a method of the Converter class, but the error you are getting suggests that the message is being sent to the ConverterController class. Check what objects you are sending the convertAmount:atRate: message to.

    For reference, I've put a copy of the code from my copy below (hopefully it's the same version).

    ConverterController.h:

    Code:
    /* ConverterController */
    
    #import <Cocoa/Cocoa.h>
    
    @interface ConverterController : NSObject
    {
        IBOutlet id converter;
        IBOutlet NSTextField *dollarField;
        IBOutlet NSTextField *rateField;
        IBOutlet NSTextField *totalField;
    }
    - (IBAction)convert:(id)sender;
    @end
    ConverterController.m:
    Code:
    #import "ConverterController.h"
    #import "Converter.h"
    
    @implementation ConverterController
    
    - (void)awakeFromNib {
    	[[rateField window] makeKeyAndOrderFront:self];
    	[rateField selectText:self];
    }
    
    - (IBAction)convert:(id)sender
    {
    	float rate, amt, total;
    	amt = [dollarField floatValue];
    	rate = [rateField floatValue];
    		
    	total = [converter convertAmount:amt atRate:rate];
    	
    	[totalField setFloatValue:total];
    	[rateField selectText:self];
    }
    
    @end
    Converter.h:
    Code:
    /* Converter */
    
    #import <Cocoa/Cocoa.h>
    
    @interface Converter : NSObject
    {
    }
    - (float)convertAmount:(float)amt atRate:(float)rate;
    
    @end
    Converter.m:
    Code:
    #import "Converter.h"
    
    @implementation Converter
    - (float)convertAmount:(float)amt atRate:(float)rate {
    	return (amt * rate);
    } 
    @end
     
  4. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #4
    Are you sure that the connections you drew in Interface Builder are correct?

    The code could look fine but the hidden information would be pointing to/from the incorrect object in the pane.
     

Share This Page