2016-05-06 1 views
0

Il s'agit d'un exercice de routine. Je l'ai fait un certain nombre de fois dans mon projet actuel et cela a bien fonctionné. J'ai copié la ligne de code pour la ligne, les mêmes initialisations. Mes données de plist vont dans un dictionnaire mais ensuite elles ne vont pas dans leurs tableaux respectifs dans leurs initialisations. J'ai une méthode appelée initArraysPlistiOS - extraction des données d'un plist ne fonctionne pas

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

}

Ive fait cela plusieurs fois et il travaille actuellement dans mon code. toutes les valeurs sont correctes. Je l'ai vérifié plusieurs fois. quand

Répondre

0

S'il vous plaît essayer ce code, il peut être utile de vous

// 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

S'il vous plaît lire les commentaires pour la chaque ligne.

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

Pour plus de précisions s'il vous plaît poster votre plist si possible.