xcode 从 plist 中检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15583275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve data from plist
提问by Naveen
I have a plist and inside that an array and then set of dictionary elements? How can I retrieve data from the plist to my array?
我有一个 plist,里面有一个数组,然后是一组字典元素?如何将数据从 plist 检索到我的数组?
How can I get category names in one array?
如何在一个数组中获取类别名称?
回答by Dipen Panchasara
Objective-C
目标-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
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
斯威夫特 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"])")
})
}
回答by βhargav?
- Get your file path from bundle or from any directory
- Get the array from dictionary retrieved from plist
Get dictionary stored in array
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"]); }
- 从包或任何目录中获取文件路径
- 从 plist 检索的字典中获取数组
获取存储在数组中的字典
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"]); }
回答by Imran
PropertyListDecodercan be used to decode plist file directly to object. For details see this answer https://stackoverflow.com/a/60389142/5662893
PropertyListDecoder可用于将 plist 文件直接解码为对象。有关详细信息,请参阅此答案https://stackoverflow.com/a/60389142/5662893
回答by tushar
do add .plist file in Targets => Copy bundle resourceWith the above in reply
在Targets 中添加 .plist 文件=> 复制捆绑资源上面的回复