如何在一个数组中获取类别名称?
解决方法 Objective-C的// Read pList from bundle and get Root Dictionary out of itNSDictionary *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"]);}];
迅速
// Read pList from bundle and get Root Dictionary out of itvar 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 DictionaryarrayList.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代码
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 itvar 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"])") })}总结
以上是内存溢出为你收集整理的ios – 从plist中检索数据全部内容,希望文章能够帮你解决ios – 从plist中检索数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)