I would like to extend Array to create a contact list.
One of the new methods would looks like:
extension Array {
func contactWithChatId<Contact>(chat_id: String) -> Contact? {
for contact in self {
if contact.chat_id == chat_id {
return contact
}
}
return nil
}
}
However, I didn't found any way to express this in Swift. Is it possible to do it, and how to ?
Solution:
extension Array {
func contactWithChatId<T>(chat_id: String) -> Contact? {
if self.isEmpty {
return nil
}
precondition(self.first is Contact , "Should be a Contact list")
for contact in self {
let contact = contact as! Contact
if contact.chat_id == chat_id {
return contact
}
}
return nil
}
}