如何在iOS中添加和获取.plist中的值

sek*_*har 22 objective-c plist ios4 ios

我正在实现基于Web服务的应用程序.在那里我需要在.plist中添加一个字符串作为属性,我需要在代码中随时获取.plist中的值.

jv4*_*v42 36

这是一个代码示例:

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];
Run Code Online (Sandbox Code Playgroud)


Nik*_*esh 18

NSBundle* mainBundle = [NSBundle mainBundle]; 
Run Code Online (Sandbox Code Playgroud)

 

// Reads the value of the custom key I added to the Info.plist
NSString *value = [mainBundle objectForInfoDictionaryKey:@"key"];

//Log the value
NSLog(@"Value = %@", value);
Run Code Online (Sandbox Code Playgroud)
// Get the value for the "Bundle version" from the Info.plist
[mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"];

// Get the bundle identifier
[mainBundle bundleIdentifier];
Run Code Online (Sandbox Code Playgroud)


VSN*_*VSN 6

NSURL *url = [[NSBundle mainBundle] URLForResource:@"YOURPLIST" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];

NSLog(@"Here is the Dict %@",playDictionariesArray);
Run Code Online (Sandbox Code Playgroud)

或者你也可以使用以下

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sample.plist"];
Run Code Online (Sandbox Code Playgroud)