扩展字典数组 <String, Any> Swift 3
extend an Array of Dictionary<String, Any> Swift 3
var dicts = [["key1": "value1", "key2": "value2"]]
dicts.values(of: "key1") // prints - value1
我正在做一个项目,我想在其中存储字典数组,然后在字典数组包含特定值的情况下从那里获取数据。
你的问题很含糊。但我的理解是,你想过滤字典数组,使其只包含具有特定值的字典,这可以通过这种方式完成:
let filteredDicts = dicts.filter({ [=10=].values.contains("value2") })
Swift 3.0
你可以这样试试
var dicts:[[String:Any]] = []
var check:Bool = false
dicts = [["search_date": "17/03/17", "search_title": ""],["search_date": "17/02/19", "search_title": "parth"],["search_date": "20/02/19", "search_title": "roy"]]
for item in dicts {
if let title = item["search_title"] as? String {
if title == "parth" {
check = true
break
}else {
check = false
}
}
else {
check = false
}
}
print(check)
我们可以使用模型来解决问题
class Person: NSObject, NSCoding {
let name: String
let age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
required init(coder decoder: NSCoder) {
self.name = decoder.decodeObject(forKey: "name") as? String ?? ""
self.age = decoder.decodeInteger(forKey: "age")
}
func encode(with coder: NSCoder) {
coder.encode(name, forKey: "name")
coder.encode(age, forKey: "age")
}
}
Class
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// setting a value for a key
let newPerson = Person(name: "Joe", age: 10)
var people = [Person]()
people.append(newPerson)
let encodedData = NSKeyedArchiver.archivedData(withRootObject: people)
UserDefaults.standard.set(encodedData, forKey: "people")
// retrieving a value for a key
if let data = UserDefaults.standard.data(forKey: "people"),
let myPeopleList = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Person] {
myPeopleList.forEach({print( [=11=].name, [=11=].age)}) // Joe 10
} else {
print("There is an issue")
}
}
}
感谢 Leo Dabus
[Link] ()
var dicts = [["key1": "value1", "key2": "value2"]]
dicts.values(of: "key1") // prints - value1
我正在做一个项目,我想在其中存储字典数组,然后在字典数组包含特定值的情况下从那里获取数据。
你的问题很含糊。但我的理解是,你想过滤字典数组,使其只包含具有特定值的字典,这可以通过这种方式完成:
let filteredDicts = dicts.filter({ [=10=].values.contains("value2") })
Swift 3.0
你可以这样试试
var dicts:[[String:Any]] = []
var check:Bool = false
dicts = [["search_date": "17/03/17", "search_title": ""],["search_date": "17/02/19", "search_title": "parth"],["search_date": "20/02/19", "search_title": "roy"]]
for item in dicts {
if let title = item["search_title"] as? String {
if title == "parth" {
check = true
break
}else {
check = false
}
}
else {
check = false
}
}
print(check)
我们可以使用模型来解决问题
class Person: NSObject, NSCoding {
let name: String
let age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
required init(coder decoder: NSCoder) {
self.name = decoder.decodeObject(forKey: "name") as? String ?? ""
self.age = decoder.decodeInteger(forKey: "age")
}
func encode(with coder: NSCoder) {
coder.encode(name, forKey: "name")
coder.encode(age, forKey: "age")
}
}
Class
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// setting a value for a key
let newPerson = Person(name: "Joe", age: 10)
var people = [Person]()
people.append(newPerson)
let encodedData = NSKeyedArchiver.archivedData(withRootObject: people)
UserDefaults.standard.set(encodedData, forKey: "people")
// retrieving a value for a key
if let data = UserDefaults.standard.data(forKey: "people"),
let myPeopleList = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Person] {
myPeopleList.forEach({print( [=11=].name, [=11=].age)}) // Joe 10
} else {
print("There is an issue")
}
}
}
感谢 Leo Dabus
[Link] (