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

前端 未结 3 1089
迷失自我
迷失自我 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:51

    Define your interface in the .h iff they are to be public, then create your implementation in the .m. You can't combine them in the .h

    0 讨论(0)
  • 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 = ....;
      
    0 讨论(0)
  • 2021-01-27 17:05

    You want semicolons inside the function like so:

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

    This is the correct syntax to use.

    0 讨论(0)
提交回复
热议问题