While it is true that you can assign any value (not just objects) to a variable of type Any
and you can add any value to an array whose element type is Any
, it is not true that [Any]
and [SPTArtist]
are the same type.
You can see this from the following example:
var someArray = [Any]()
var someOtherArray = [SPTArtist]()
let artist = SPTArtist()
someArray.append(artist) // OK
someOtherArray.append(artist) // OK
someArray.append(1) // OK
someOtherArray.append(1) // Error
Your test is checking to see if the type of T
is literally [Any]
, not if it is "an array".
If you search you can find several variations on checking to see if a variable is an array.
This answer that extends Array
with an empty protocol seems like a reasonable approach.
You could add a type constraint so that it only applies to arrays of your type:
protocol ArrayType {}
extension Array: ArrayType where Element == SPTArtist {}
func isArray() -> Bool {
return T.self is ArrayType.Type // Returns true iff T is [SPTArtist]
}