Error in Obj-C : Expected identifier or '('

前端 未结 3 1091
迷失自我
迷失自我 2021-01-27 16:23

I\'m trying to make a simple app with a button a score counter and a timer but I\'m getting some errors

    #import 

@interface xyzViewCont         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-27 16:58

    You are mixing up interface and implementation. The interface contains the (globally visible) instance variables, properties and method declarations, i.e. the prototypes:

    @interface xyzViewController : UIViewController
    {
        IBOutlet UILabel *scoreLabel;
        IBOutlet UILabel *timerLabel;
        NSInteger count;
        NSInteger seconds;
        NSTimer *timer;
    }
    - (IBAction)buttonPressed;
    
    @end
    

    The method itself goes into the implementation:

    @implementation xyzViewController
    
    - (IBAction)buttonPressed
    {
        count++;
        scoreLabel.text = [NSString stringWithFormat:@"Score \n %i", count];
    }
    @end
    

    Remarks:

    • The convention is to start class names with a capital letter: XyzViewController.
    • Create properties for the outlets (if you don't have them already):

      @property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
      

      The compiler synthesizes the instance variable _scoreLabel automatically, so you don't need it in the interface. And then access the property via

      self.scoreLabel.text = ....;
      

提交回复
热议问题