iOS - 从 plist 中提取数据无效
iOS - extracting data from a plist not working
这是例行练习。我在我当前的项目中已经做过很多次并且效果很好。我逐行复制了代码行,相同的初始化。我的 plist 数据进入字典,但在初始化时不会进入其各自的数组。我有一个名为 initArraysPlist
的方法
-(void)initArraysPlist{
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"trainerProfile" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];
trainerNames = [dict1 objectForKey:@"Names"];
trainerIcons = [dict1 objectForKey:@"Icons"];
trainerFactSheet= [dict1 objectForKey:@"Fact Sheet"];
trainerFocus = [dict1 objectForKey:@"Focus"];
trainerContactInfo= [dict1 objectForKey:@"Contact Info"];
}
我已经这样做了几次,目前它在我的代码中有效。所有的值都是正确的。我检查了很多次。当
请试试这个代码,它可能对你有帮助
// Read plist from bundle and get Root Dictionary out of it
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:@"catlist"]];
// Now a loop through Array to fetch single Item from catList which is Dictionary
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
// Fetch Single Item
// Here obj will return a dictionary
NSLog(@"Category name : %@",[obj valueForKey:@"category_name"]);
NSLog(@"Category id : %@",[obj valueForKey:@"cid"]);
}];
请阅读每一行的评论。
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"trainerProfile" ofType:@"plist"]; // **check if your plist is actually added in Bundle.If its there move to second line , if not then add plist in bundle.**
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];// **if plist is added in bundle , then check if you are getting value for dict1 . If no then you might be making some mistake in plist structure.**
如有可能,请post您的 plist 以获得更多说明。
这是例行练习。我在我当前的项目中已经做过很多次并且效果很好。我逐行复制了代码行,相同的初始化。我的 plist 数据进入字典,但在初始化时不会进入其各自的数组。我有一个名为 initArraysPlist
的方法-(void)initArraysPlist{
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"trainerProfile" ofType:@"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];
trainerNames = [dict1 objectForKey:@"Names"];
trainerIcons = [dict1 objectForKey:@"Icons"];
trainerFactSheet= [dict1 objectForKey:@"Fact Sheet"];
trainerFocus = [dict1 objectForKey:@"Focus"];
trainerContactInfo= [dict1 objectForKey:@"Contact Info"];
}
我已经这样做了几次,目前它在我的代码中有效。所有的值都是正确的。我检查了很多次。当
请试试这个代码,它可能对你有帮助
// Read plist from bundle and get Root Dictionary out of it
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
// Your dictionary contains an array of dictionary
// Now pull an Array out of it.
NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:@"catlist"]];
// Now a loop through Array to fetch single Item from catList which is Dictionary
[arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
// Fetch Single Item
// Here obj will return a dictionary
NSLog(@"Category name : %@",[obj valueForKey:@"category_name"]);
NSLog(@"Category id : %@",[obj valueForKey:@"cid"]);
}];
请阅读每一行的评论。
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"trainerProfile" ofType:@"plist"]; // **check if your plist is actually added in Bundle.If its there move to second line , if not then add plist in bundle.**
NSDictionary *dict1 = [[NSDictionary alloc] initWithContentsOfFile:path1];// **if plist is added in bundle , then check if you are getting value for dict1 . If no then you might be making some mistake in plist structure.**
如有可能,请post您的 plist 以获得更多说明。