swift - How to tell if a variable is an array -
i have swift function accepts , want able take array of strings, array of ints, mixed array, or array of arrays, etc. can take string or int, etc, not in array.
so have this:
private func parse(parameter: any) { if parameter int { // int } else if (parameter float) || (parameter double) { // double } else if parameter string { // string } else if parameter bool { // bool } else if allow array = parameter as? [any] { // should grab arrays } else { assert(false, "unsupported type") // [string] ends here } }
but if phone call parse(["strings"])
, assert raised. how can grab types of arrays?
edit - there confusion i'm trying accomplish. need homecoming string based on type, int -> "" , string -> "", array create recursive calls homecoming "..."
this post marked duplicate, other question javascript, not swift.
i found way that, utilize nsarray
casting.
private func parse(x: any) { if allow o = x as? [any] { println("[any]") } if allow o = x as? [anyobject] { println("[anyobject]") } if allow o = x as? nsarray { println("nsarray") } } allow a: [any] = ["bar"] allow b: [anyobject] = ["bar"] allow c = ["foo", 3.14] parse(a) // ==> [any] parse(b) // ==> [anyobject], , nsarray parse(c) // ==> nsarray
it array containing values of any
internally represented in nsarray
. (but should able cast c
[any]
...? i'm suspecting it's bug.)
arrays swift
No comments:
Post a Comment