2017-01-07 21 views
6

Chcę sprawdzić, czy typ rodzajowy klasy jest tablicą:Jak sprawdzić ogólny typ klasy to tablica?

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

Ale ostrzega

odlewany z '' T.type niepowiązanym typu 'Array' zawsze kończy się niepowodzeniem

Jak mogę rozwiązać ten problem?

dodał: Przesłałem kody do Gist. https://gist.github.com/nallwhy/6dca541a2d1d468e0be03c97add384de

Co chcę zrobić, to sparsować odpowiedź JSONA zgodnie z tablicą modelu lub tylko jednego modelu.

+0

related: [Jak mogę sprawdzić, czy obiekt jest zbiorem? (Swift)] (http://stackoverflow.com/q/41236021/2976878) – Hamish

Odpowiedz

2

Jak mówi komentator @Holex, można użyć Any. Połączyć je z Mirror i można na przykład zrobić coś takiego:

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] 
-1

Nie przekazujesz żadnego argumentu, więc nie ma typu, a funkcja ogólna nie ma sensu. Albo usunąć typ rodzajowy:

func() {} 

lub, jeśli chcesz przekazać argument:

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

test(argument: array) 

Drukuje: true

+0

@shallowThrought Zmieniłem przykładowy kod. – mayTree

+1

@mayTree nadal nie masz żadnych argumentów, tzn. Nie przekazujesz typu. W jaki sposób należy odróżnić typ "T"? Czego oczekujesz, że metoda powróci jako 'T'? – shallowThought

+0

dlaczego potrzebujesz _generics_ do tego? proste "Any" również może wykonać zadanie - i kto zwraca wartość "true"? ponieważ nie jest to z pewnością twoja metoda. – holex

Powiązane problemy