8

I want to check whether the generic class type is an Array:

func test<T>() -> Wrapper<T> {
  let isArray = T.self is Array<Any>
  ... 
}

But it warns

Cast from 'T.type' to unrelated type 'Array' always fails

How can I solve this problem?

added: I've uploaded my codes to Gist. https://gist.github.com/nallwhy/6dca541a2d1d468e0be03c97add384de

What I want to do is to parse json response according to it's an array of model or just one model.

mayTree
  • 727
  • 2
  • 9
  • 22
  • Related: [How do I check if an object is a collection? (Swift)](http://stackoverflow.com/q/41236021/2976878) – Hamish Jan 07 '17 at 12:07

3 Answers3

4

As commentator @Holex says, you can use Any. Combine it with Mirror and you could, for example, do something like this:

func isItACollection(_ any: Any) -> [String : Any.Type]? {
    let m = Mirror(reflecting: any)
    switch m.displayStyle {
    case .some(.collection):
        print("Collection, \(m.children.count) elements \(m.subjectType)")
        var types: [String: Any.Type] = [:]
        for (_, t) in m.children {
            types["\(type(of: t))"] = type(of: t)
        }
        return types
    default: // Others are .Struct, .Class, .Enum
        print("Not a collection")
        return nil
    }
}

func test(_ a: Any) -> String {
    switch isItACollection(a) {
    case .some(let X):
        return "The argument is an array of \(X)"
    default:
        return "The argument is not an array"
    }
}

test([1, 2, 3]) // The argument is an array of ["Int": Swift.Int]
test([1, 2, "3"]) // The argument is an array of ["Int": Swift.Int, "String": Swift.String]
test(["1", "2", "3"]) // The argument is an array of ["String": Swift.String]
test(Set<String>()) // The argument is not an array
test([1: 2, 3: 4]) // The argument is not an array
test((1, 2, 3)) // The argument is not an array
test(3) // The argument is not an array
test("3") // The argument is not an array
test(NSObject()) // The argument is not an array
test(NSArray(array:[1, 2, 3])) // The argument is an array of ["_SwiftTypePreservingNSNumber": _SwiftTypePreservingNSNumber]
Grimxn
  • 22,115
  • 10
  • 72
  • 85
3

Try out define an array marker protocol. Refer to this answer.

protocol AnyTypeOfArray {}
extension Array: AnyTypeOfArray {}
extension NSArray: AnyTypeOfArray {}

func isArray<T>(type: T.Type) -> Bool {
    return T.self is AnyTypeOfArray.Type
}

print(isArray(type: [String].self))   // true
print(isArray(type: String.self))     // false
Ethan11
  • 76
  • 5
-1

You are not passing any argument, so there is no type and a generic function does not make sense. Either remove the generic type:

func() {}

or, if you want to pass an argument:

let array = ["test", "test"]
func test<T>(argument: T) {
    let isArray = argument is Array<Any>
    print(isArray)
}

test(argument: array)

Prints: true

shallowThought
  • 19,212
  • 9
  • 65
  • 112
  • 1
    @mayTree you still have no argument, i.e. you are not passing a type. How should the type of `T` be found out? What do you expect the method to return as `T`? – shallowThought Jan 07 '17 at 07:06
  • why do you need _generics_ for that? a simple `Any` could do the job as well – and who does return the `true` value? because it is defintely not your method. – holex Jan 07 '17 at 10:03
  • @holex I just wanted to keep the example. Certainly generics are not needed. Sorry, Replaced "returns" with "prints". – shallowThought Jan 07 '17 at 10:08