I want to use function contains
on Array of type AnyObject
import UIKit
var resultArray: Array<AnyObject> = Array()
resultArray.append(50)
resultArray.append(false)
resultArray.append("Test string")
let found = contains(resultArray, 50)
I get the error:
Type 'AnyObject -> L' does not conform to protocol 'IntegerLiteralConvertible'
The contains() method returns: true - if the array contains the specified element. false - if the array doesn't contain the specified element.
Here, [Int]() specifies that the empty array can only store integer data elements. Note: In Swift, we can create arrays of any data type like Int , String , etc.
You use AnyObject when you need the flexibility of an untyped object or when you use bridged Objective-C methods and properties that return an untyped result. AnyObject can be used as the concrete type for an instance of any class, class type, or class-only protocol.
I agree with the comments and other answer; AnyObject
is not good practice, but if you really want to use AnyObject
, you can treat your array of AnyObjects as an NSArray
object and then use the function containsObject()
:
if (resultArray as NSArray).containsObject(AnyObjectOfAnyType) {
// Do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With