1

Why IB outlet collection do not have index of object method when it is an array? What I am doing wrong?

@IBOutlet var textFieldsCollection: [UITextField]!

self.textFieldsCollection.indexOfObject(textField)

End the error is this: Text.swift:30:18: '[UITextField]' does not have a member named 'indexOfObject'

Ramis
  • 13,985
  • 7
  • 81
  • 100
  • possible duplicate of [How do I do indexOfObject or a proper containsObject](http://stackoverflow.com/questions/24010700/how-do-i-do-indexofobject-or-a-proper-containsobject) - Swift arrays don't have a 'native' `indexOfObject` - you have to bridge to NSArray or search the array yourself – Paulw11 Oct 13 '14 at 09:38

1 Answers1

2

As swift array can contain non-class types indexOfObject could be too ambiguous. You can use find function too look up for index of object.

var textFieldsCollection: [UITextField]!
var myTextField = UITextField()

var index = find(textFieldsCollection, myTextField)
Kirsteins
  • 27,065
  • 8
  • 76
  • 78
  • Thanks for good answer! Maybe you know alternatives for objectAdIndex method? Again I can not found it. – Ramis Oct 13 '14 at 11:32
  • I guess this is the alternative. indexOfObject cannot work the same way as in NSArray, because all the objects in NSArray are class types and are compared by references. Swift arrays can contain structs and enums, that are value types and can only be compared by values. – Kirsteins Oct 13 '14 at 11:39