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"类别。但是现在我想再次过滤filtersArray的金额。因此,只有两个(Home Arrays)的数量在一个额外的数组中。
答案 0 :(得分:1)
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 { $0["category"] as? String == "Home" }
let amounts = filteredArray.flatMap { $0["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 { $0.category == "Home" }
let amounts = filteredArray.map { $0.amount }
print(amounts)
使用struct
有许多优点。它对每个领域都是安全的。你可以使它Codable
,这样可以更容易地保存和读取文件。