How to use -(id)init?!

Discussion in 'iOS Apps' started by lukelee, Jul 22, 2011.

  1. lukelee macrumors newbie

    Joined:
    Apr 20, 2011
    Location:
    Sydney
    #1
    I just want to initialize NumberOfSides to 5! and display it. just can't get it right!

    .h
    Code:
    @interface HelloPolyViewController : UIViewController {
    	IBOutlet UILabel *NumberOfSidesLabel;
    	IBOutlet UIButton *decrease;
    	IBOutlet UIButton *increase;
    	int NumberOfSides;
    
    }
    @property(nonatomic,retain)IBOutlet UILabel *NumberOfSidesLabel;
    @property int NumberOfSides;
    
    -(IBAction)decrease:(id)sender;
    -(IBAction)increase:(id)sender;
    - (id)init;
    
    
    .m
    Code:
    #import "HelloPolyViewController.h"
    @implementation HelloPolyViewController
    @synthesize NumberOfSidesLabel;
    @synthesize NumberOfSides;
    
    - (id)init {
    	if (self=[super init]) {
    		NumberOfSides = 5;
    	}
    }
    
    
    -(IBAction)decrease:(id)sender
    {
    	NumberOfSidesLabel.text = [NSString stringWithFormat:@"%d",NumberOfSides];
    	NSLog(@"123 %d",NumberOfSidesLabel);
    	
    }
    -(IBAction)increase:(id)sender {
    	//
    }
    
     
  2. Jeffrey Lim macrumors newbie

    Joined:
    Sep 9, 2003
    #2
    Use:

    NSLog(@"123 %d", NumberOfSides);

    or:

    NSLog(@"123 %@", NumberOfSidesLabel);
     
  3. orangetripwire macrumors newbie

    Joined:
    Feb 24, 2009
    #3
    Code:
    - (id)init {
    	if (self=[super init]) {
    		NumberOfSides = 5;
    	}
          [I]  return self;[/I]
    } 
    You need to return something in the method since you have - (id) init and not something like - (void) init. Note that the init method must return an - (id), so - (void) init is not a valid init method.

    Also like said above %d is for something like an int, %@ is for something like a string in the NSLog statement.
     

Share This Page