如何在 swift 中读取数组和字典的 plist
Posted
技术标签:
【中文标题】如何在 swift 中读取数组和字典的 plist【英文标题】:How to read a plist of array and dictionary in swift 【发布时间】:2015-07-02 17:04:18 【问题描述】:我正在关注 Apple 示例项目 TVAnimationsGestures,但将其应用于我自己的项目之一。我需要将数据从 plist 加载到我的类中,但我似乎无法弄清楚如何快速做到这一点。这是 plist 尝试读取的图像。
示例项目使用此方法读取plist
- (NSArray *)plays
if (_plays == nil)
NSURL *url = [[NSBundle mainBundle] URLForResource:@"PlaysAndQuotations" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
_plays = [NSMutableArray arrayWithCapacity:[playDictionariesArray count]];
for (NSDictionary *playDictionary in playDictionariesArray)
APLPlay *play = [[APLPlay alloc] init];
play.name = playDictionary[@"playName"];
NSArray *quotationDictionaries = playDictionary[@"quotations"];
NSMutableArray *quotations = [NSMutableArray arrayWithCapacity:[quotationDictionaries count]];
for (NSDictionary *quotationDictionary in quotationDictionaries)
APLQuotation *quotation = [[APLQuotation alloc] init];
[quotation setValuesForKeysWithDictionary:quotationDictionary];
[quotations addObject:quotation];
play.quotations = quotations;
[_plays addObject:play];
return _plays;
我环顾四周并设法让大部分内容不显示任何错误,但我认为它没有正确完成,因为有很多演员表。这是我已经走了多远:
func loadFoodCategoryData() ->[Any]
// foodCategoryData is declared outside the function
if foodCategoryData == nil
var url = NSBundle.mainBundle().URLForResource("Items", withExtension: "plist")!
var categoryDictionaryArray = NSArray(contentsOfURL: url) as! [[String:AnyObject!]]
foodCategoryData = Array()
for foodDictionary:Dictionary in categoryDictionaryArray
var foodCat = FoodCategories()
foodCat.foodCategoryName = foodDictionary["categoryName"] as! String
var foodItemsDict = foodDictionary["items"]
var foodItemsArray = [AnyObject]()
for foodItems in foodDictionary
var foodItem:FoodItems = FoodItems()
foodItem.setValuesForKeysWithDictionary(foodItems as! Dictionary)
foodItemsArray.append(foodItem)
return foodCategoryData
谁有更好更有效的方法来将此 plist 加载到我的类中?
【问题讨论】:
你在函数之外声明foodCategoryData
吗?你声明它是什么?
是的,是函数外的变量。
【参考方案1】:
惰性初始化变量在 Swift 中具有特定的语法。相当于ObjC的代码是(体简化)
lazy var plays : [AnyObject] =
var foodCategoryData = [AnyObject]()
// do things
foodCategoryData.append("something")
return foodCategoryData
()
从 AnyObject 向下转换是一种痛苦但必要的
编辑
您的代码非常接近。试试这个(未经测试)
lazy var foodCategoryData : [AnyObject] =
let url = NSBundle.mainBundle().URLForResource("Items", withExtension: "plist")!
var categoryDictionaryArray = NSArray(contentsOfURL: url) as! [[String:AnyObject!]]
var foodItemsArray = [FoodItems]()
for foodDictionary in categoryDictionaryArray
var foodCat = FoodCategories()
foodCat.foodCategoryName = foodDictionary["categoryName"] as! String
var foodItemsDict = foodDictionary["items"] as! [String:AnyObject]
for foodItems in foodDictionary
var foodItem:FoodItems = FoodItems()
foodItem.setValuesForKeysWithDictionary(foodItems as! Dictionary)
foodItemsArray.append(foodItem)
return foodItemsArray
()
【讨论】:
我理解那部分。我不明白的是如何从我的 plist 中提取数据并将值设置到我的自定义类中。 谢谢,它在大多数情况下都有效。我在“foodItem.setValuesForKeysWithDictionary(foodItems as! Dictionary)”处收到警告,它说“从 (String,implicitlyUnwrappedOptional) 转换为不相关的类型 Dictionary以上是关于如何在 swift 中读取数组和字典的 plist的主要内容,如果未能解决你的问题,请参考以下文章