Warning Error with init override

Discussion in 'iOS Programming' started by JLB-UK, May 25, 2010.

  1. JLB-UK macrumors regular

    Joined:
    Oct 21, 2009
    Location:
    UK
    #1
    Hi,

    I get the following warning when calling the initWithNumberOfSides from the init method, which is in a init override in a custom class I have created.

    Code:
     'PolygonShape' may not respond to '-initWithNumberOfSides:minimumNumberOfSides:maximumNumberOfSides:'
    
    The code is as follows

    Code:
    - (id)init { 
    	if (self = [super init]) {
    		[self initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:10];
    	}
    	return self;
    }
    
    
    - (id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max; { 
    	if (self = [super init]) {
    		[self setMinimumNumberOfSides:min];
    		[self setMaximumNumberOfSides:max];
    		[self setNumberOfSides:sides];
    	}
    	return self;
    }
    
    The warning error appears however, the code still runs and completes correctly. Could anyone provide any advice on resolving the warning message?

    Thanks for the help
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Did you declare -initWithNumberOfSides:minimumNumberOfSides:maximumNumberOfSides: in the .h file?
     
  3. JLB-UK thread starter macrumors regular

    Joined:
    Oct 21, 2009
    Location:
    UK
    #3
    Thank you! Thought I had, but misspelt it! Problem solved! Thanks for your help
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    Location:
    Brobdingnag
    #4
    Your init override will end up calling super init twice. This is inadvised.
     
  5. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    chown's comment is correct.

    You should read up on the concept of the designated initializer in the Objective-C 2.0 programming language guide.

    You should probably fix it like this

    Code:
    - (id)init { 
    	if ((self = [self initWithNumberOfSides:4 minimumNumberOfSides:3 maximumNumberOfSides:10])) {
    	}
    	return self;
    }
    
     

Share This Page