Cannot declare variable inside @interface or @protocol

前端 未结 4 1636
时光说笑
时光说笑 2021-01-31 18:19

I have an iOS app built since the beginning with an error in it. Since the source was began constructed from the template, its appdelegate.h looks like:

@interfa         


        
4条回答
  •  醉酒成梦
    2021-01-31 19:04

    Are you trying to define them as public members on a class? Classes in Objective-C are rather different than in other languages you might be familiar with. Outside of the curly braces you can only define methods. If you want to make a publicly-accessible member, define them as properties:

    @interface myAppDelegate : NSObject  {
        UIWindow *window;
        myViewController *viewController;
        BOOL _myBool;
        NSString *_myString;
    }
    
    @property BOOL       myBool;     // intended to be globally accessible
    @property NSString   *myString;  // intended to be globally accessible
    
    @end
    

    Then in your @implementation do something like:

    @implementation myAppDelegate
    @synthesize myBool = _myBool;
    @synthesize myString = _myString;
    

    Then you can access them as myObject.myBool and so on.

    If you are just trying to make them into static ("global") data for all instances of the class, then as other posters have said, you want to move the definition into your .m file (and ideally declare them static so they won't cause link issues).

提交回复
热议问题