Where's the best place to store constants in an iOS app?

后端 未结 5 1587
忘了有多久
忘了有多久 2020-12-22 23:56

I\'m developing an app just now that fetches resources from a JSON API.

all of the resources have the same base URL:

http://api.mysite.com/resources.         


        
5条回答
  •  清歌不尽
    2020-12-23 00:29

    Personally I prefer using actual const variables rather than defines.

    In a MyConstants.m file I have:

    NSString *const kXYMySiteBaseURL = @"http://api.mysite.com/";
    NSString *const kXYSomeOtherURL = @"http://www.google.com/";
    

    where XY is my initials or some other "unique" prefix to avoid collisions with other constants.

    Then I have a MyConstants.h file like this:

    extern NSString *const kXYMySitBaseURL;
    extern NSString *const kXYSomeOtherURL;
    

    Depending on how many files need to access these constants, I might include it in the precompiled header like ColdFusion suggests in his answer.

    This is how Apple defines their constants in most of the Core frameworks.

提交回复
热议问题