Tolgay Toklar Tolgay Toklar - 3 months ago 5x
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?

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)