new to iPhone programming

Discussion in 'iOS Programming' started by chaosal, Jan 26, 2010.

  1. chaosal macrumors newbie

    Joined:
    Jan 26, 2010
    #1
    hey guys been looking through a lot of info on the web but can't understand what i'm doing wrong:

    my .h file:

    Code:
    @interface myClass : NSObject {
    
    	NSString* colour;
    }
    
    - (NSString*) colour;
    - (void) setColour:(NSString*)input;
    
    @end
    my .m file:

    Code:
    @implementation myClass
    
    - (NSString*) colour {
        return colour;
    }
    
    - (void) setColour: (NSString*)input {
        colour = input;
    }    
    
    - (void) dealloc
    {
        [colour release];
        [super dealloc];
    }
    
    @end
    using myClass:

    Code:
    myClass *test = [[myClass alloc] init];
    [myClass setNumber:10];
    the error i get:

    010-01-26 20:59:36.540 myApp[6428:207] *** -[NSCFString setNumber:]: unrecognized selector sent to instance 0x405c
    2010-01-26 20:59:36.542 myApp[6428:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString setNumber:]: unrecognized selector sent to instance 0x405c'

    any help or just pointing me in the right direction would be greatly appreciated :) thanks
     
  2. icewing macrumors 6502

    Joined:
    Jun 19, 2008
    Location:
    St Louis
    #2
    You're calling a method called setNumber, but all I see in your class is a method called setColour.
     
  3. chaosal thread starter macrumors newbie

    Joined:
    Jan 26, 2010
    #3
    oops sorry been testing loads and posted the wrong bit, this is what i'm actually trying to do:

    Code:
    myClass *test = [[myClass alloc] init];
    NSString *testString = @"blue";
    [test setColour:testString];	
    
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    And you're still getting
    from this new code?

    EDIT: P.S. It's also common convention to name classes starting with a capital letter so as to not confuse them with (commonly) lower-case-started instance variables. I.E. MyClass would be the conventional approach.
     
  5. chaosal thread starter macrumors newbie

    Joined:
    Jan 26, 2010
    #5
    ok thanks will remember that and yeah im still gettin the same error:

    *** -[NSCFString setColour:]: unrecognized selector sent to instance 0x405c

    really don't know why it's failing, should be such a simple thing :S
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I'm not getting any errors when running that code. Your code must be different than what you included. It's complaining that you are trying to call the instance method setColour: on an object of NSString type. Are you seeing any warnings when building?
     
  7. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #7
    I also think it's a good idea to realize that you might not be following the best practice with your setter. In your setColour: method, you have:

    Code:
    colour = input;
    but then in your dealloc, you have:

    Code:
    [colour release]
    Do you see where you might have a problem with this?
     
  8. skunkworker macrumors regular

    Joined:
    Sep 9, 2007
    #8
    I would recommend using a @property
    some thing like this

    Code:
     @property ( nonatomic,retain) NSString * color; 
    then just call

    Code:
    [self setColor: myColor];
    and it will properly retain and release it. But don't make a setColor method. ( it makes one for you)
     

Share This Page