2016-07-30 6 views
0

ich habe den plist:wie Werte auf Etiketten von plist angezeigt werden. Objective-C

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"  "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<array> 
    <dict> 
    <key>name</key> 
    <string>Daniel</string> 
    <key>Description</key> 
    <string>Bad boy</string> 
</dict> 
<dict> 
    <key>name</key> 
    <string>Mary</string> 
    <key>description</key> 
    <string>Good girl</string> 
</dict> 
</array> 
</plist> 

ich auf meinem Label Wert Namen und die Beschreibung des Namen abhängig angezeigt werden soll:

NSString *path = [[NSBundle mainBundle] pathForResource:@"PropList" ofType:@"plist"]; 
ComplexArray = [NSArray arrayWithContentsOfFile:path]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
for (int i = 0; i < [ComplexArray count]; i++) { 
      ThirdViewController *item = [ComplexArray objectAtIndex:i]; 

    if ([item.name isEqualToString: @"Mary"]) { 
     _nameLabel.text = [dict objectForKey:@"name"]; 
     _descriptionLabel.text = [dict objectForKey:@"description"]; 
    } 
} 

Und warum kann ich nicht tun, wie folgt: (i tat dies, wenn ich keine pliste, aber jetzt diese Rückkehr Fehler):

NSString *path = [[NSBundle mainBundle] pathForResource:@"PropList" ofType:@"plist"]; 
ComplexArray = [NSArray arrayWithContentsOfFile:path]; 
for (int i = 0; i < [ComplexArray count]; i++) { 
ThirdViewController *item = [ComplexArray objectAtIndex:i]; 
if ([item.name isEqualToString: @"Mary"]) { 
_nameLabel.text=item.name; 
_descriptionLabel.text=item.description; 
    } 
} 

Antwort

0

Wieder hatten Sie es ziemlich schon richtig.

NSString *path = [[NSBundle mainBundle] pathForResource:@"PropList" ofType:@"plist"]; 
ComplexArray = [NSArray arrayWithContentsOfFile:path]; 
for (NSDictionary *dict in ComplexArray) { 
    if ([dict[@"name"] isEqualToString:@"Mary"]) { 
     _nameLabel.text = dict[@"name"]; 
     _descriptionLabel.text = dict[@"description"]; 
     break; 
    } 
} 
+0

Droppy, danke! – Mario

0

Versuchen Sie diesen Code es Muss perfekt arbeiten!

 // Read plist from bundle and get Root Dictionary out of it 
    NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"PropList" ofType:@"plist"]]; 

    // Your dictionary contains an array of dictionary 
    // Now pull an Array out of it. 
    NSArray *array = [dictionary objectForKey:@"keyarray"]; 


    // Now a loop through Array to fetch single Item 

    for (NSDictionary *dict in array) { 



if ([[dict objectForKey:@"name"] isEqualToString:@"Mary"]) 
{ 
    // Do stuff... 

     NSString *name= [dict objectForKey:@"name"]; 
     NSString *description= [dict objectForKey:@"Description"]; 

} 

    } 

Sie müssen einen Schlüssel vor dem Array hinzufügen.

<key>keyarray</key> 
+0

danke, aber wie kann ich nur name und beschreibung je nach name (zum beispiel Mary) anzeigen? – Mario

+1

Dieser Code wird nicht funktionieren. Warum würdest du '[NSArray initWithContentsOfFile:]' aufrufen und ein Wörterbuch übergeben? Es sollte eine Zeichenfolge sein. Wie auch immer, Sie haben die '.plist' Datei bereits geladen?!? Sehr weit von "Muss perfekt arbeiten" ... – Droppy

+0

Ich habe einen Fehler: inkompatible Zeigertypen senden 'NSDictionary *' zu Parameter des Typs 'NSString * _Nonull' – Mario

0

Führen Sie folgende Schritte aus:

  1. die plist Daten in einem Wörterbuch Get

    NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"NameOfPlistFile" ofType:@"plist"]; 
    NSDictionary *bundleDictionary=[NSDictionary dictionaryWithContentsOfFile:plistPath]; 
    
  2. den Wert in Bezug auf Key

    Get
    NSString *nameString = [bundleDictionary valueForKey:kCustomURLScheme]; 
    

Hoffe, das hilft!

Verwandte Themen