Objective-C environment config loader with validation
Objective-C
#import <Foundation/Foundation.h>
static NSDictionary *LoadConfig(NSError **error) {
NSDictionary *env = NSProcessInfo.processInfo.environment;
NSString *portText = env[@"PORT"] ?: @"8080";
NSString *mode = env[@"APP_ENV"] ?: @"development";
NSArray *modes = @[@"development", @"staging", @"production"];
NSInteger port = portText.integerValue;
NSString *problem = nil;
if (port < 1 || port > 65535) problem = [NSString stringWithFormat:@"PORT must be 1-65535, got %@", portText];
else if (![modes containsObject:mode]) problem = [NSString stringWithFormat:@"APP_ENV must be one of %@", modes];
if (problem) {
if (error) *error = [NSError errorWithDomain:@"ConfigError" code:1 userInfo:@{NSLocalizedDescriptionKey: problem}];
return nil;
}
return @{@"port": @(port), @"mode": mode, @"debug": @([env[@"DEBUG"] boolValue])};
}