Search Array of Dictionaries for Value in Swift

后端 未结 6 508
你的背包
你的背包 2020-12-16 01:50

I\'m new to Swift and taking a class to learn iOS programming. I find myself stumped on how to search in an array of dictionaries for a string value and dump the string valu

6条回答
  •  感动是毒
    2020-12-16 02:15

      Use the following code to search from NSArray of dictionaries whose keys are ID and Name.
    
          var txtVal:NSString
          let path = NSBundle.mainBundle().pathForResource(plistName, ofType: "plist")
          var list = NSArray(contentsOfFile: path!) as [[String:String]]
    
          var namePredicate = NSPredicate(format: "ID like %@",  String(forId));
          let filteredArray = list.filter { namePredicate!.evaluateWithObject($0) };
          if filteredArray.count != 0
          {
              let value = filteredArray[0] as NSDictionary
              txtVal = value.objectForKey("Name") as String
          }
    

提交回复
热议问题