I'm trying to filter my array by some element. I want recordtype is contain "Income" and createdAt equal to a date but $0.createdAt! is an date type so I can't use .contain(""), what can I use?
recordFilter = record.filter { $0.recordtype!.contains("Income") /*|| $0.createdAt! = recordItem.createdAt!*/}
recordFilter output: (this result is not filter by createdAt yet)
[<Record: 0x600000099fa0> (entity: Record; id: 0xd0000000003c0000 <x-coredata://913F25A5-B2C9-4646-9091-5EFE7F906908/Record/p15> ; data: {
accountbook = "first book";
amount = "\U00a59.99";
assest = nil;
category = "\U6295\U8d44";
createdAt = "2017-11-16 16:00:00 +0000";
date = nil;
id = 15;
recordtype = "\U6536\U5165";
remark = "";
toAccBook = nil;
}), <Record: 0x600000099ff0> (entity: Record; id: 0xd000000000400000 <x-coredata://913F25A5-B2C9-4646-9091-5EFE7F906908/Record/p16> ; data: {
accountbook = "first book";
amount = "\U00a56.58";
assest = nil;
category = "\U5de5\U8d44";
createdAt = "2017-11-16 16:00:00 +0000";
date = nil;
id = 16;
recordtype = "\U6536\U5165";
remark = "";
toAccBook = nil;
})]
After that, I trying to get all the amount string in array and sum it. But I found that it's all separate string when I use this code, how can I group it in array and sum it or have any other way to do it?
let sum = recordFilter.map({Int($0.amount!.dropFirst())})
//[nil, nil]
for i in 0..<recordFilter.count{
if recordFilter[i].amount != nil{
let amountList = recordFilter[i].amount!.dropFirst()
print(amountList) // I found that it's separate string.
//9.99
//11.22
// ...
//let intArray = amountList.map { Int($0)!}
//let sum = amountList.reduce(0, +)
//print(sum)
}
}