Ich habe ein Plist und in diesem ein Array und dann Satz von Wörterbuchelementen? Wie kann ich Daten von der PLIST zu meinem Array abrufen?Daten von plist abrufen
Wie kann ich Kategorienamen in einem Array?
Ich habe ein Plist und in diesem ein Array und dann Satz von Wörterbuchelementen? Wie kann ich Daten von der PLIST zu meinem Array abrufen?Daten von plist abrufen
Wie kann ich Kategorienamen in einem Array?
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-Code
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"])")
})
}
// Um Kategoriename direkt abzurufen, benutze [[arrayList objectAtIndex: 0] valueForKey: @ "category_name"] –
Ich habe einen Code für Swift 2.0 hinzugefügt. Bitte stört mich nicht –
Kein Problem, hier sind wir anderen zu helfen und Problem zu lösen, indem Sie unser Wissen teilen. thx für Ihre Bemühungen –
Get Wörterbuch
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"]);
}
ITs nicht funktioniert @bhargavi – Naveen
Ich weiß es spät, aber nur auf das Update schauen. es wird klappen. –
OK. Wie kann ich die Farbe der Navigationsleiste im Storyboard ändern? eigentlich habe ich in den navigation controller eingebettet und ich möchte seine farbe auf RGB umstellen (20,60,72) weißt du das? ich habe es versucht: self.navigationController.navigationBar.tintColor = [UIColor colorWithRed: 26 grün: 62 blau: 72 alpha : 0]; es funktioniert nicht – Naveen
hinzufügen .plist-Datei in Targets => Kopieren Bundle Ressource Mit der obigen Antwort
Warum müssen Sie ein Array von category_name machen? Das sind gut strukturierte Daten. Wenn Sie darauf problemlos zugreifen möchten, erstellen Sie eine Modellklasse für die Kategorie mit den Eigenschaften categoryName und categoryID. Das wäre einfacher. – Anupdas