Tolgay Toklar Tolgay Toklar - 5 months ago 12
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()
swiftBlogs.indexOf(mm)


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
class Message {
    var id: Int
    init(id: Int) {
        self.id = id
    }
}

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

check the declaration !!

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