2013-03-23 14 views
7

Ho un plist e al suo interno un array e quindi un set di elementi del dizionario? Come posso recuperare i dati dal plist al mio array?Recupero dati da plist

plist

Come posso ottenere nomi delle categorie in un array?

+1

Perché è necessario creare un array di category_name? Questo è un dato ben strutturato. Se vuoi accedervi facilmente, prova a creare una classe modello per la categoria con proprietà categoryName e categoryID. Questo sarebbe più facile. – Anupdas

risposta

29

Objective-C

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

Swift

// Read plist from bundle and get Root Dictionary out of it 
var dictRoot: [NSObject : AnyObject] = [NSObject : AnyObject].dictionaryWithContentsOfFile(NSBundle.mainBundle().pathForResource("data", ofType: "plist")) 
// Your dictionary contains an array of dictionary 
// Now pull an Array out of it. 
var arrayList: [AnyObject] = [AnyObject].arrayWithArray((dictRoot["catlist"] as! String)) 
// Now a loop through Array to fetch single Item from catList which is Dictionary 
arrayList.enumerateObjectsUsingBlock({(obj: AnyObject, index: UInt, stop: Bool) -> Void in 
    // Fetch Single Item 
    // Here obj will return a dictionary 
    NSLog("Category name : %@", obj["category_name"]) 
    NSLog("Category id : %@", obj["cid"]) 
}) 

Swift 2.0 Codice

var myDict: NSDictionary? 
    if let path = NSBundle.mainBundle().pathForResource("data", ofType: "plist") { 
     myDict = NSDictionary(contentsOfFile: path) 
    } 
    let arrayList:Array = myDict?.valueForKey("catlist") as! Array<NSDictionary> 
    print(arrayList) 

    // Enumerating through the list 
    for item in arrayList { 
     print(item) 

    } 

Swift 3,0

// Read plist from bundle and get Root Dictionary out of it 
var dictRoot: NSDictionary? 
if let path = Bundle.main.path(forResource: "data", ofType: "plist") { 
    dictRoot = NSDictionary(contentsOfFile: path) 
} 

if let dict = dictRoot 
{ 
    // Your dictionary contains an array of dictionary 
    // Now pull an Array out of it. 
    var arrayList:[NSDictionary] = dictRoot?["catlist"] as! Array 
    // Now a loop through Array to fetch single Item from catList which is Dictionary 
    arrayList.forEach({ (dict) in 
     print("Category Name \(dict["category_name"]!)") 
     print("Category Id \(dict["cid"])") 
    }) 
} 
+0

// Per andare direttamente a nome_categoria, utilizzare [[arrayList objectAtIndex: 0] valueForKey: @ "nome_categoria"] –

+0

Ho aggiunto un codice per Swift 2.0. Per favore non mi dispiaccia, –

+0

Nessun problema, eccoci qui per aiutare gli altri e risolvere il problema condividendo le nostre conoscenze. grazie per i tuoi sforzi –

4
  1. Ottenere il vostro percorso del file da pacco o da qualsiasi directory
  2. Prendi l'array dal dizionario recuperato da plist dizionario
  3. vengono memorizzati in ordine di

    NSString *plistFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"test.plist"]; 
    
    NSDictionary *list = [NSDictionary dictionaryWithContentsOfFile:plistFilePath]; 
    NSLog(@"%@",list); 
    NSArray  *data = [list objectForKey:@"catlist"]; 
    for(int i=0; i< [data count]; i++) 
    { 
        NSMutableDictionary *details=[data objectAtIndex:i]; 
        NSLog(@"%@",[details objectForKey:@"category_name"]); 
        NSLog(@"%@",[details objectForKey:@"cid"]); 
    
    } 
    
+0

ITs non funziona @bhargavi – Naveen

+0

So che è tardi ma basta guardare l'aggiornamento. Funzionerà. –

+0

OK. come posso cambiare il colore della barra di navigazione nello storyboard ?? in realtà sono incorporato nel controller di navigazione e voglio cambiare il suo colore in RGB (20,60,72) lo sai ?? ho provato che: self.navigationController.navigationBar.tintColor = [UIColor colorWithRed: 26 green: 62 blue: 72 alpha : 0]; non funziona – Naveen

-1

aggiungi file .plist in Obiettivi => Copia risorsa bundle Con quanto sopra in risposta