Update 2017
Xcode 8 introduced class properties to Objective-C, from the release notes:
Objective-C now supports class properties, which interoperate with Swift type properties. They are declared as @property (class) NSString *someStringProperty;
, and are never synthesised.
This means our sample interface below can become:
@interface PlayerMenuController : NSObject
@property (class) int idGen;
@end
However you must still implement the methods yourself, as shown below, as class properties are never synthesised. Note that this also means if you specify property attributes, such as copy
, that your methods must implement the semantics.
Original Answer
It looks like you are trying to implement a class property, but there is not such thing in Objective-C - a property is a pair of instance methods.
However, you can fake it...
While the @property
declaration is not available to you, if you declare class methods which follow the right naming convention then your compiler may (tested on Xcode 4.6.1, "may" as I cannot offhand point to this being supported, but it's simple to test and will compile time error if not) allow you to use dot notation, i.e. it looks like a class property even if it lacks an @property
.
A sample interface:
@interface PlayerMenuController : NSObject
// a class "property"
+ (int) idGen;
+ (void) setIdGen:(int)value;
@end
The implementation:
@implementation PlayerMenuController
static int idGen = 0;
+ (int) idGen { return idGen; }
+ (void) setIdGen:(int)value { idGen = value; }
@end
And test it:
NSLog(@"initial value: %d", PlayerMenuController.idGen);
PlayerMenuController.idGen = 42;
NSLog(@"updated value: %d", PlayerMenuController.idGen);
producing:
initial value: 0
updated value: 42
So we have a "class property" - it looks, walks and quacks like a property ;-)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…