从 plist 中检索多行

Retrieving multiple rows from plist

我的 plist 中有一些代码数据。我正在尝试使用 Xcode 阅读它。基本上我想检索满足给定条件的多行

我只能使用以下代码检索 1 行:但我无法检索超过 1 行。例如我有以下行

住宿 col1 col2 col3 1. 一个。 b. C 1.d. e. f

print(getDateForDate(date: "1"))

func getSwiftArrayFromPlist(name: String) -> (Array<Dictionary<String,String>>)
        {
        let path = Bundle.main.path(forResource: name, ofType: "plist")
        var arr : NSArray?
        arr = NSArray(contentsOfFile: path!)
        return(arr as? Array<Dictionary<String,String>>)!
    }
    func getDateForDate(date: String) -> (Array<[String:String]>)
    {
        let array = getSwiftArrayFromPlist(name: "file")
        let namePredicate = NSPredicate(format: "Lodging = %@", date)
        return [array.filter{namePredicate.evaluate(with: [=10=])}[0]]
    }

上面的代码能够检索到第 1 行,但不能检索到第 2 行。我想提取所有符合条件的行。不止一个

首先,不鼓励使用 NSArrayNSDictionary API 阅读 plist。使用 PropertyListSerialization 或更好地将 属性 列表反序列化为具有 Codable 协议的结构。

func getSwiftArrayFromPlist(name: String) -> [[String:String]]
{
    let url = Bundle.main.url(forResource: name, withExtension: "plist")!
    let data = try! Data(contentsOf: url)
    return try! PropertyListSerialization.propertyList(from: data, format: nil) as! [[String:String]]
}

您的谓词过滤数组 return 第一个对象 [0],将行更改为

return array.filter{namePredicate.evaluate(with: [=11=])}

中的NSPredicate是多余的,Swift中的可以直接过滤。 return 类型周围的括号也是多余的

func getDateForDate(date: String) -> [[String:String]]
{
    let array = getSwiftArrayFromPlist(name: "file")
    return array.filter{ [=12=]["Lodging"]! == date }
}

谢谢。那有帮助。下面是代码:

return array.filter{namePredicate.evaluate(with: [=10=])}

我返回了一组字典,如下所示。

[["a":"1","b":"2","c":3],["a":"3","b":"4","c":5]]

由此我需要检索在键 "a" 下定义的任何数据。我该怎么做?

谢谢!