0

I created an array of objects:

var fullMonthlyList = [SimulationMonthly]()

The class here:

class SimulationMonthly {
    var monthlyMonthDuration: NSNumber = 0
    var monthlyYearDuration: NSNumber = 0
    var monthlyFullAmount: NSNumber = 0
    var monthlyAmount: Int = 0

    init(monthlyMonthDuration: NSNumber, monthlyYearDuration: NSNumber, monthlyFullAmount: NSNumber, monthlyAmount: Int){
        self.monthlyMonthDuration = monthlyMonthDuration
        self.monthlyYearDuration = monthlyYearDuration
        self.monthlyFullAmount = monthlyFullAmount
        self.monthlyAmount = monthlyAmount
    }
}

I just did append to populate it, now I want to find for example if they're an existing value, for example monthlyAmount equals to "194" by search in the array, how can I do ? I have tried filter and contains but I get errors.

What I've tried:

if self.fullMonthlyList.filter({ $0.monthlyAmount == self.monthlyAmount.intValue }) { ... }

Error:

Cannot invoke 'filter' with an argument list of type '((SimulationMonthly) throws -> Bool)'

2
  • 1
    "I have tried filter and contains but I get errors." Can you show us what you did and which error did you get? Commented Dec 13, 2016 at 16:12
  • @Eendje for if self.fullMonthlyList.filter({$0.monthlyAmount == self.monthlyAmount.intValue}){ I get: Cannot invoke 'filter' with an argument list of type '((SimulationMonthly) throws -> Bool)' Commented Dec 14, 2016 at 8:21

2 Answers 2

3

You can do:

if let sim = fullMonthlyList.first(where: { $0.monthlyAmount == 194 }) {
    // Do something with sim or print that the object exists...
}

This will give you the first element in your array where monthlyAmount equals 194.

If you want all elements with that condition, you can use filter:

let result = fullMonthlyList.filter { $0.monthlyAmount == 194 }

If you don't need the object at all but you just want to know if one exists, then contains would be enough:

let result = fullMonthlyList.contains(where: { $0.monthlyAmount == 194 })
Sign up to request clarification or add additional context in comments.

1 Comment

I recommend you use trailing closure syntax, though I can see the value of having where: for demonstrative purposes
1

Here's a simple playground example of filtering objects based on matching a property. You should be able to expand it to your situation.

class Item {
    var value: Int

    init(_ val: Int) {
        value = val
    }
}

var items = [Item]()
for setting in 0..<5 {
    items.append(Item(setting))
}

func select(_ with: Int) -> [Item] {
    return items.filter { $0.value == with }
}

let found = select(3)

3 Comments

I'd recommend against writing a trivial function like select() in your example. It doesn't abstract much, but still adds an unnecessary layer of abstraction.
Rollback. (If you're going to edit an answer to match your coding style, at least change it to something that compiles.)
Whoops, that should've been (0..<5).map(Item.init)

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.