I want to check whether a variable is array type (maybe [Int]
, [UIView]
or [AnyObject]
), not just a specific type like [Int]

- 2,119
- 2
- 27
- 36
-
Compare http://stackoverflow.com/questions/27132761/how-to-detect-that-parameter-is-a-tuple-of-two-arbitrary-types for a similar question about tuples. I assume that the answer can be modified for arrays. – But it would be interesting to know *why* you need this information and how you would use it. – Martin R Dec 05 '14 at 05:29
-
1It's really hard to imagine a case where you would ever have to make this kind of a check manually. Are you trying to work with an instance of `AnyObject`? If so, just do `variable is NSArray`. – mattt Dec 05 '14 at 08:34
5 Answers
I don't know if reflect
is any more or less stable than _stdlib_getDemangledTypeName
, but it's another option:
if reflect(myVariable).disposition == MirrorDisposition.IndexContainer {
// myVariable is an array
} else {
// myVariable is something else
}

- 91,912
- 16
- 138
- 175
Here's a possible test (v
is a variable):
let ok = _stdlib_getDemangledTypeName(v) == "Swift.Array"
If ok
is true
, v
is an array of some sort.

- 515,959
- 87
- 875
- 1,141
-
Just note that `_stdlib_getDemangledTypeName` (and the non-demangled version `_stdlib_getTypeName`) are part of the private Swift API, not the public one. This means, for example, that Apple is not likely to approve a submitted app using these calls. – Mark Reed Dec 05 '14 at 05:27
-
1@MarkReed Those are just words. All I know is it has worked correctly on every variable I've thrown at it. If you know a better answer, give it as an answer. – matt Dec 05 '14 at 05:29
-
I'm not saying it doesn't work. But you may not be able to rely on it continuing to work in future releases... – Mark Reed Dec 05 '14 at 05:32
-
@MarkReed You can't rely on _anything_ about Swift to continue to work in future releases. The whole language is in flux. Now is when the question is being asked. Now is when I'm answering. – matt Dec 05 '14 at 05:34
Following code works if you import Foundation
func isArray(value: AnyObject) -> Bool {
return value is [AnyObject]
}
However if you don't import Foundation
it gives error as "[AnyObject] does not conform to protocol AnyObject".
The reason for this swift compiler behaves different when you import Foundation
. Only classes are conforms to AnyObject
. Any
protocol is for everything (class, struct, enum etc). So without importing Foundation
the Array
type is struct type and it does not conform AnyObject
protocol in swift. When you import Foundation
swift compiler implicitly converts Array
to NSArray
and it becomes class and of course conforms AnyObject
. That is why when you import Foundation
above code works otherwise doesn't.

- 15,254
- 10
- 48
- 57
You can use NSObject method "isKindOfClass()" to check if the variable store an instance of the class you want to compare it:
let isArray: Bool = myVariable.isKindOfClass(NSArray)
// OR
if myVariable.isKindOfClass(NSArray) {
// whatever
}
or:
let isArray: Bool = myVariable is NSArray
// OR
if myVariable is NSArray {
// whatever
}
"isKindOfClass()" works only with classes that are subclasses of NSObject
"is" works with any class in Swift

- 66
- 5
-
-
yourArray is it kind of class NSArray?, if "true" than yes the variable is array. I can't explain more. Its human-readable code – mrfoxix Jun 30 '15 at 12:42
-
1You should update the answer not as comment. Code only answer is not good answer.It goes to 'low quality post' review task where people can vote to delete the answer.I found your answer there and suggested to improve your answer instead of vote to delete. – Shaiful Islam Jun 30 '15 at 12:45
-
This is a similar situation to checking if an Any.Type
is an optional.
As of Swift 2 the language lacks covariance and contravariance. Also checks against typeless Arrays
(or any typeless generic) are still unsupported.
As with the Any.Type
-is-Optional
case, Array
, can be extended to implement a protocol that can be checked akin a typeless array:
protocol ArrayProtocol{}
extension Array: ArrayProtocol {}
["one", "two"] is ArrayProtocol // true
[12, true, "nay"] is ArrayProtocol // true
"not an array" is ArrayProtocol // false

- 1
- 1

- 10,385
- 4
- 44
- 57