0

I'm defining an Objective-C class:

@interface MyRequest : NSObject

@property (strong, nonatomic, readonly) NSDecimalNumber *myNumber;

@property (strong, nonatomic, readonly) CommConfig *commConfig;

@property (nonatomic, assign, readonly) BOOL debug;

How do I make commConfig a static variable? When I use the 'class' keyword, the compiler gives me the following warning:

Class property 'commConfig' requires method 'commConfig' to be defined - use @dynamic or provide a method implementation in this class implementation

And the constructor doesn't recognize this line anymore:

_commConfig = commConfig
jscs
  • 63,694
  • 13
  • 151
  • 195
user1695758
  • 173
  • 1
  • 3
  • 14

1 Answers1

1

If not implemented by the programmer instance properties are automatically implemented by the compiler - an instance variable allocated and getter and/or setter methods written. Class properties are never automatically implemented, you therefore need to declare the static backing variable and define the getter. In your @implementation add:

static CommConfig *_commConfig;

+ (CommConfig *) commConfig { return _commConfig; }

You can call the backing anything you wish, e.g. to follow a naming convention for global/static variables.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Hopefully it's obvious, but might be worth noting, that you have to implement _all_ attributes of the property. I.e., if you want it to be `weak`, you must use a `__weak` backing var; if you want it to be atomic, you must lock it yourself... – jscs Nov 17 '17 at 20:46