0

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)

    }
}
2
  • your question is not clear that much. give some extra code and proper description so other user can understand easily. @Wan jern Commented Nov 17, 2017 at 4:50
  • ok, I edited the question @VishalPatel Commented Nov 17, 2017 at 4:57

1 Answer 1

1

Well, If you want to fulfil two cases (recordtype == Income && createdAt == Your Date) so why you are not filtering using the &&?

recordFilter = record.filter { return $0.recordtype!.contains("Income") && $0.createdAt! == recordItem.createdAt!}

Please give a try and let me know if I am missing something?

If you want to sum here you go:

recordFilter.map{($0["amount"] as! Float)}.reduce(0, +)
Sign up to request clarification or add additional context in comments.

8 Comments

Oh thanks! How can I get all the amount after that? because i want to sum it. let sum = recordFilter.map({Int($0.amount!.dropFirst())}).reduce(0,+) something like this but I got error @Sohil R. Memon
I can't covert the string array to int array
@WanJern Amount is String, Double or Float ?
Opps, I changed it to Float already. how to sum it ? I got this error when I use reduce(0, +). Cannot invoke 'reduce' with an argument list of type '(Int, _)' @Sohil R.Memon
Hi, I found that double is limited in hundred number. how to expand it, is it possible? @Sohil R.Memon
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.