On lazy instantiation and convenience methods

前端 未结 4 992
醉话见心
醉话见心 2020-12-18 15:43

Assume you have a Singleton Constants class, instance of which you\'d like to use throughout your application.

In someClass, therefore we

4条回答
  •  抹茶落季
    2020-12-18 16:20

    Following @vinceburn I would use the following example for constants and a singleton for more complex structures.

    // Constants.h
    // Replace PSMyApp for something more useful. e.g. company/name initials followed by app/class
    
    // String example
    NSString * const PSMyAppString = @"constantString"; 
    
    // Logically related integers
    typedef enum {
       PSMyAppRelatedValuesOne = 0,
       PSMyAppRelatedValuesTwo,
       PSMyAppRelatedValuesThree
    } PSMyAppRelatedValues;
    
    // Float example
    const CGFloat PSMyAppFloat = 0.3f;
    
    // Integer that has no related values
    const NSInteger PSMyAppInteger = 2;
    

    I prefer this over #define as I get auto completion and compiler checking and it fits more naturally with the way Apple does thing in some of the UIKit classes.

提交回复
热议问题