私はplistを持っていて、その中に配列と辞書要素のセットがありますか?plistから配列にデータを取得するにはどうすればよいですか?
1つの配列でカテゴリ名を取得するにはどうすればよいですか?
私はplistを持っていて、その中に配列と辞書要素のセットがありますか?plistから配列にデータを取得するにはどうすればよいですか?
1つの配列でカテゴリ名を取得するにはどうすればよいですか?
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"]);
}];
迅速
// 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"])
})
Swift2.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 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"])")
})
}
辞書を配列に格納する
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"]);
}
PropertyListDecoderを使用して、plistファイルをオブジェクトに直接デコードできます。詳細については、この回答を参照してくださいhttps://stackoverflow.com/a/60389142/5662893
Targetsに.plistファイルを追加してください=>バンドルリソースをコピー してください