roberto tomás
roberto tomás

Reputation: 4687

Cannot invoke 'isEqualToString' with an argument list of type '(String)' (from closure)

This problem stems from having two different conditions for comparing a struct.

Typically, I'd like the struct to be unique if its id is unique. But that is not part of the problem here.. here, I need to build a unique set from them where uniqueness comes from multiple conditions. I'm building an array of condition closures and then testing each in a custom TimeSeries class, that's the plan (although a way to override a set's conditional would let me use a simple just just fine). But I'm running into an issue with the closures themselves.

{(_ entry: TimeSeriesEntry, _ set: [TimeSeriesEntry]) -> Bool in
    return !set.contains({ item in item.id.isEqualToString(entry.id) })
}

I'm not clear why that reports Cannot invoke 'isEqualToString' with an argument list of type '(String)' TimeSeriesEntry.id is defined as String not (String):

struct TimeSeriesEntry: Codable, Equatable {
    let id: String
    let uid: String
    let date: Date
    let apps: [String:Double]
    let locations: [String:Bool]
}

Upvotes: 0

Views: 41

Answers (2)

vadian
vadian

Reputation: 285082

isEqualToString belongs to NSString. In native Swift it's much easier

{(entry, set) -> Bool in // the underscores and types are unused in Swift 3+
    return !set.contains { $0.id == entry.id }
}

This could be more efficient

!set.map{$0.id}.contains(entry.id)

Upvotes: 2

Robert Dresler
Robert Dresler

Reputation: 11150

You can just check if entry's id is equal to id property of some element from set

return !set.contains { $0.id == entry.id }

Upvotes: 1

Related Questions