Reputation: 129
I have a property list file "someFile.plist" and within the plist I have two rows "row1" and "row2" each with a string value that is either "Y" or "N" - If I want to check the "someFile.plist" file for "row2" to obtain the value of that row and read it into a string in objective c, how would I do that? I am coding for an iphone App using Xcode.
Upvotes: 1
Views: 7726
Reputation: 673
For Swift 3.0:
if let path = Bundle.main.path(forResource: "YourPlistFile", ofType: "plist"), let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject] {
let value = dict["KeyInYourPlistFile"] as! String
}
Upvotes: 0
Reputation: 3514
If you want to get the value of "row2" to String then it depends if you are having Dictionary type or Array type. In the case of Dictionary type:
NSString *path = [[NSBundle mainBundle] pathForResource:@"pListFileName" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString valueOfRow2 = [dict objectForKey:@"row2"]);
NSLog(@"The value of row2 is %@", valueOfRow2);
and in the case of Array:
NSString *path = [[NSBundle mainBundle] pathForResource:@"pListFileName" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile: path];
//the indexes of NSArray is counted from 0, not from 1
NSString valueOfRow2 = [array objectAtIndex:1];
NSLog(@"The value of row2 is %@", valueOfRow2);
You can use NSMutableDictionary and NSMutableArray respectively. It would be more easy to modify them.
Upvotes: 2
Reputation: 27073
Load the .plist
into a NSDictionary
like:
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
Loop through the NSDictionary
using something like:
for (id key in dictionary) {
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
Upvotes: 9