2016-05-06 42 views
0

這是一個例行練習。我在當前的項目中做過很多次,並且它工作得很好。我複製了代碼行,相同的初始化。我的plist數據進入一個字典,但不會在其初始化中進入其各自的數組。我有一個名爲initArraysPlistiOS - 從plist中提取數據不起作用

-(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"]; 

方法}

伊夫這個做了幾次,它目前的工作在我的代碼。所有的值都是正確的。我已經檢查過很多次了。當

回答

0

請試試這個代碼,可能對你有幫助

// 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"]); 
}]; 
0

請閱讀每一行的意見。

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.** 

如需更多說明,請張貼您的plist。