0

When I try to use this method the compiler shows the next error: "Type 'UIView' does not conform to protocol 'IntegerLiteralConvertible'"

if find(_views, 1) {

}
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71
  • If `_views` is an array of `UIView` then passing `1` as the second argument makes no sense. – Martin R Aug 19 '14 at 19:10
  • I thought the second parameter was the index of the array to check. But I see now that it isn't. Thanks anyway. – Víctor Albertos Aug 19 '14 at 19:19
  • What are you trying to find? – Martin R Aug 19 '14 at 19:27
  • If the element at specified index is not null. I'm currently using the extension suggested here: http://stackoverflow.com/questions/24102024/how-to-check-if-an-element-is-in-an-array But I thought the "find()" method would be better. But obviously i don't know how it works. – Víctor Albertos Aug 19 '14 at 19:31

1 Answers1

1

That method signature is:

find(domain: C, value: C.Generator.Element) -> C.Index?

Where C is a typed array, C.Generator.Element is the type of the elements in that array, and C.Index? is an optional that will contain the index the element is found at, if found at all.

So the error you are getting is because it looking at the instances in your array UIView and trying to compare them to 1 which is an IntegerLiteral. And UIView is not IntegerLiteralConvertible because it would make no sense to convert a view to an integer.

So find will return the index where some instances can be found in an array of those instances.

var strings: [String] = ["A", "B", "C"]
find(strings, "C")! // 2

But you don't seem to want the index. if find(views, 1) seems to indicate to me that you want to check if index 1 exists in the array. If this is really what you want, you can do this very simply by checking the count.

if _views.count > 1 {
  println("index 1 exists in this array")
}
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337