Rob Segal
Rob Segal

Reputation: 7625

Best way to store and reference a list of 3rd party API keys and constants in code?

I've got a list of constants for numbers api's that I'd like to collect in one location in code and am wondering about the best ways to do this. Here's what I have so far..

A static array with all the constants...

static NSDictionary* kApiConstants =
    [NSDictionary dictionaryWithObjectsAndKeys:
     @"crittercism-app-id", @"myAppId",
     @"crittercism-key",    @"myAppKey",
     @"crittercism-secret", @"mySecretKey",

     @"content-server-url-dev", @"http://my-dev-url/",
     @"content-server-url-stg", @"http://my-staging-url",
     @"content-server-url-pro", @"http://my-production-url",
     nil];

I then have a macro for quick retrieval of items in the array...

#define MYAPIKEY(x) [kApiConstants objectForKey:x]

I like this setup. It makes code cleaner to read overall and makes merging easier for my purposes between branches in our git repo. One feature I would love to have at build/compile time is if a string is not in the dictionary then a build and/or compiler error would get flagged to indicate this.

I'm sure others have run into this situation before with so many 3rd party libraries, sdk's and what have you in a project it's hard to keep track of them all. For those willing to share what systems have you come up with to help with this?

In my case this is for an iOS project but the situation applies for any kind of project really.

Upvotes: 0

Views: 156

Answers (1)

Max
Max

Reputation: 587

What advantage do you see in using a dictionary over just making them constants on their own? Your wish:

One feature I would love to have at build/compile time is if a string is not in the dictionary then a build and/or compiler error would get flagged to indicate this.

would be solved by just making them constants in their own right.

Upvotes: 2

Related Questions