Tolgay Toklar Tolgay Toklar - 1 year ago 150
Swift Question

indexOf using with arrays in Swift

I have an array:

var swiftBlogs:[Message] = [Message]()

And I want to find index number of some object. I tried following code:

let mm=Message()

But I am getting compiler error:

Cannot convert value of type 'Message' to expected argument type '@noescape (Message) throws -> Bool'

How can I resolve this?

Answer Source
class Message {
    var id: Int
    init(id: Int) { = id

var arr: [Message] = []
arr.append(Message(id: 1))
arr.append(Message(id: 2))
arr.append(Message(id: 3))
print(arr.indexOf { $ == 2 }) // Optional(1)

check the declaration !!

.indexOf(predicate: (Message) throws -> Bool)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download