Xcode 8 added class properties to Objective-C. This is similar to type properties in Swift but not as full-featured. Think of a class property as a property on the class itself rather than a property on an instance of the class. Objective-C class properties are not automatically synthesized, so you will need to explicitly define only a getter if the property is readonly, and both a getter and a setter if the property is readwrite. For the setter, you will need to use some sort of storage, so it could be things like static storage, or NSUserDefaults storage, etc.

Using multiple, small Objective-C classes with nothing but readonly class properties can be a nice way to add logical grouping to your constants. For example, below in the .h file, we create a class called AnalyticsEvent for grouping our string constants for analytics events. Note that all the properties are declared with class and readonly attributes:


@interface AnalyticsEvent : NSObject

@property (nonatomic, class, readonly) NSString *tap;
@property (nonatomic, class, readonly) NSString *swipe;
@property (nonatomic, class, readonly) NSString *pinch;



In the .m file, we just need to create class method getters for each property:

@implementation AnalyticsEvent

+ (NSString *)tap { return @"tap_event_name"; }
+ (NSString *)swipe { return @"swipe_event_name"; }
+ (NSString *)pinch { return @"pinch_event_name"; }


Note that each getter is implemented on one-line. This is just a stylistic choice that should really only be used for very simple single-token expressions. Using this with more complicated expressions will make your code difficult to visually scan. However, in this case it keeps things nice and lightweight.

Then to use the properties, just access them with dot syntax directly on the class itself:

[myAnalytics trackEvent:AnalyticsEvent.tap];
[myAnalytics trackEvent:AnalyticsEvent.swipe];
[myAnalytics trackEvent:AnalyticsEvent.pinch];

Also note the Objective-C class properties can be accessed the same way in Swift:


It's up to the developer if each of these classes should be in it's own file. However, for most use cases there could simply be one "Constants" file with multiple, small grouped constants classes included in it.