Where various variable and method types should be placed in a header

前端 未结 2 1490
温柔的废话
温柔的废话 2020-12-20 06:33

I\'ve noticed that I get compilation errors if I place certain declarations in certain places in my header file. I\'ve put comments into the code as to where I think certain

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 07:19

    You are correct on all counts except one. Your last variables are not static variables, they are global variables. Static variables are simply variables that are declared with the static keyword and they mean something a little different than other languages. They aren't class variables, they are variables that are visible only to the file that they are declared in, and only then in the scope that it was declared (if you declare it inside a function other functions won't see it). However, as you would expect, they are only declared once regardless of how many instances you have. If you declare something outside an interface without the static keyword as you did, other classes will import them. However, this is not the ideal way to accomplish this (you might get redefinition errors if more than one class imports this header).

    Also, one caveat, is that properties don't need to have an explicit backing variable (the compiler will create one for you if you use the @synthesize keyword), but of course if you desire one there is nothing wrong with it.

    Finally, you should note that the only reason that your static methods class methods are not instance methods is because they start with a plus (+) character as opposed to a minus (-) character.

提交回复
热议问题