iOS Application Configuration File

后端 未结 8 1497
南笙
南笙 2021-01-31 16:13

In a C# application I always used an app.config file to save some data for my application to load when needed (e.g Connection String).

Wha

8条回答
  •  花落未央
    2021-01-31 16:42

    If you're just storing a relatively simple set of configuration stuff, you can simply use NSDictionary's serialization methods (I don't know how to link to the methods directory in the class doc):

    NSString *settingsPath = [@"~/pathtoconfig.plist" stringByExpandingTildeInPath];
    
    NSMutableDictionary *settings = [NSMutableDictionary dictionary];
    if([[NSFileManager defaultManager] fileExistsAtPath:settingsPath]){
        settings = [NSMutableDictionary dictionaryWithContentsOfFile:settingsPath];
    }
    
    // writing settings
    [settings writeToFile:settingsPath atomically:YES];
    

    Since I don't know how familiar you are with objective-c, I'll also note:

    • In iOS, the home directory refers to the application's sandboxed directory.
    • I recommend reading a little bit up on reference counting. Although objective-c, as of iOS5, now automatically reference counts for you, there are some edge cases to be aware about (Here's a SO question on ARC)

提交回复
热议问题