我过滤了字典的 NSMutableArray 然后我想再次过滤一个键
I filtered an NSMutableArray of Dictionaries and then I want to filter for a Key again
var str = "Hello, playground"
var dataArray : NSMutableArray = []
var sum = 0.00
func addNewIncomeRecord(_ id: String, date: String, description: String, amount: Double, currency: String, category: String, notes: String) {
let dict = ["id" : id, "date" : date, "description" : description, "amount" : amount, "currency" : currency, "category" : category, "notes" : notes] as [AnyHashable : Any]
dataArray.add(dict)
}
addNewIncomeRecord("1", date: "11-25", description: "Money", amount: 20.00, currency: "EUR", category: "Home", notes: "More Money")
addNewIncomeRecord("2", date: "11-25", description: "Rent", amount: 50.00, currency: "EUR", category: "Home", notes: "Rent Money")
addNewIncomeRecord("3", date: "11-25", description: "Hair", amount: 10.00, currency: "EUR", category: "Medical" ,notes: "HairCut")
let homeValue = NSPredicate(format: "category == %@", "Home")
let filteredArray = (dataArray as NSMutableArray).filtered(using: homeValue)
print(filteredArray)
我过滤了 "Home" 类别的所有数组。但现在我想再次过滤 filteredArray 的数量。这样只有两个(Home Arrays)的数量在一个额外的Array中。
- 不要在 Swift 中使用
NSArray
或 NSMutableArray
。使用 Swift 本机数组(和字典)。
- 一旦你这样做了,就没有必要
NSPredicate
。使用 filter
方法。
- 不要使用字典来保存数据。使用所需字段创建
struct
。
首先,在不创建 struct
的情况下,这里有一个使用原生数组的简单解决方案:
var dataArray = [[String:Any]]()
var sum = 0.00
func addNewIncomeRecord(_ id: String, date: String, description: String, amount: Double, currency: String, category: String, notes: String) {
let dict: [String:Any] = ["id" : id, "date" : date, "description" : description, "amount" : amount, "currency" : currency, "category" : category, "notes" : notes]
dataArray.append(dict)
}
addNewIncomeRecord("1", date: "11-25", description: "Money", amount: 20.00, currency: "EUR", category: "Home", notes: "More Money")
addNewIncomeRecord("2", date: "11-25", description: "Rent", amount: 50.00, currency: "EUR", category: "Home", notes: "Rent Money")
addNewIncomeRecord("3", date: "11-25", description: "Hair", amount: 10.00, currency: "EUR", category: "Medical" ,notes: "HairCut")
let filteredArray = dataArray.filter { [=10=]["category"] as? String == "Home" }
let amounts = filteredArray.flatMap { [=10=]["amount"] as? Double }
print(amounts)
这是使用 struct
.
的解决方案
struct Income {
let id: String
let date: String
let description: String
let amount: Double
let currency: String
let category: String
let notes: String
}
var dataArray = [Income]()
var sum = 0.00
func addNewIncomeRecord(_ id: String, date: String, description: String, amount: Double, currency: String, category: String, notes: String) {
let income = Income(id: id, date: date, description: description, amount: amount, currency: currency, category: category, notes: notes)
dataArray.append(income)
}
addNewIncomeRecord("1", date: "11-25", description: "Money", amount: 20.00, currency: "EUR", category: "Home", notes: "More Money")
addNewIncomeRecord("2", date: "11-25", description: "Rent", amount: 50.00, currency: "EUR", category: "Home", notes: "Rent Money")
addNewIncomeRecord("3", date: "11-25", description: "Hair", amount: 10.00, currency: "EUR", category: "Medical" ,notes: "HairCut")
let filteredArray = dataArray.filter { [=11=].category == "Home" }
let amounts = filteredArray.map { [=11=].amount }
print(amounts)
使用 struct
有很多优点。它对每个字段都是类型安全的。您可以将其设置为 Codable
,这样可以更轻松地保存和读取文件。
var str = "Hello, playground"
var dataArray : NSMutableArray = []
var sum = 0.00
func addNewIncomeRecord(_ id: String, date: String, description: String, amount: Double, currency: String, category: String, notes: String) {
let dict = ["id" : id, "date" : date, "description" : description, "amount" : amount, "currency" : currency, "category" : category, "notes" : notes] as [AnyHashable : Any]
dataArray.add(dict)
}
addNewIncomeRecord("1", date: "11-25", description: "Money", amount: 20.00, currency: "EUR", category: "Home", notes: "More Money")
addNewIncomeRecord("2", date: "11-25", description: "Rent", amount: 50.00, currency: "EUR", category: "Home", notes: "Rent Money")
addNewIncomeRecord("3", date: "11-25", description: "Hair", amount: 10.00, currency: "EUR", category: "Medical" ,notes: "HairCut")
let homeValue = NSPredicate(format: "category == %@", "Home")
let filteredArray = (dataArray as NSMutableArray).filtered(using: homeValue)
print(filteredArray)
我过滤了 "Home" 类别的所有数组。但现在我想再次过滤 filteredArray 的数量。这样只有两个(Home Arrays)的数量在一个额外的Array中。
- 不要在 Swift 中使用
NSArray
或NSMutableArray
。使用 Swift 本机数组(和字典)。 - 一旦你这样做了,就没有必要
NSPredicate
。使用filter
方法。 - 不要使用字典来保存数据。使用所需字段创建
struct
。
首先,在不创建 struct
的情况下,这里有一个使用原生数组的简单解决方案:
var dataArray = [[String:Any]]()
var sum = 0.00
func addNewIncomeRecord(_ id: String, date: String, description: String, amount: Double, currency: String, category: String, notes: String) {
let dict: [String:Any] = ["id" : id, "date" : date, "description" : description, "amount" : amount, "currency" : currency, "category" : category, "notes" : notes]
dataArray.append(dict)
}
addNewIncomeRecord("1", date: "11-25", description: "Money", amount: 20.00, currency: "EUR", category: "Home", notes: "More Money")
addNewIncomeRecord("2", date: "11-25", description: "Rent", amount: 50.00, currency: "EUR", category: "Home", notes: "Rent Money")
addNewIncomeRecord("3", date: "11-25", description: "Hair", amount: 10.00, currency: "EUR", category: "Medical" ,notes: "HairCut")
let filteredArray = dataArray.filter { [=10=]["category"] as? String == "Home" }
let amounts = filteredArray.flatMap { [=10=]["amount"] as? Double }
print(amounts)
这是使用 struct
.
struct Income {
let id: String
let date: String
let description: String
let amount: Double
let currency: String
let category: String
let notes: String
}
var dataArray = [Income]()
var sum = 0.00
func addNewIncomeRecord(_ id: String, date: String, description: String, amount: Double, currency: String, category: String, notes: String) {
let income = Income(id: id, date: date, description: description, amount: amount, currency: currency, category: category, notes: notes)
dataArray.append(income)
}
addNewIncomeRecord("1", date: "11-25", description: "Money", amount: 20.00, currency: "EUR", category: "Home", notes: "More Money")
addNewIncomeRecord("2", date: "11-25", description: "Rent", amount: 50.00, currency: "EUR", category: "Home", notes: "Rent Money")
addNewIncomeRecord("3", date: "11-25", description: "Hair", amount: 10.00, currency: "EUR", category: "Medical" ,notes: "HairCut")
let filteredArray = dataArray.filter { [=11=].category == "Home" }
let amounts = filteredArray.map { [=11=].amount }
print(amounts)
使用 struct
有很多优点。它对每个字段都是类型安全的。您可以将其设置为 Codable
,这样可以更轻松地保存和读取文件。