2014-08-30 2 views
2

사전에 값이 사전, 배열 또는 기타인지 확인해야하는 사전이 있어야합니다. 나는 다음과 같은 오류가 발생합니다 : Swift - 사례 명세서 및 메타 유형 확인?

// Type of dictionary to enumerate through 
public typealias SourceDictionary = [String: AnyObject] 
var dictionary: SourceDictionary 


for (key, value) in dictionary { 
    switch (value) { 
     case value as SourceDictionary : 
     print("Dictionary") 

     case value as Array : 
     print("Array") 

     default : 
     print("Other") 
    } 
} 

Downcast pattern value of type Dictionary cannot be used

은 또한 당신의 구문은 단지 매우 옳지 않아,

case let someValue as SourceDictionary 

답변

0

당신은 switch 또는 if 문 중 하나에 확인할 수 있습니다했습니다.

스위치 :

for (key, value) in dictionary { 
    switch value { 
    case let v as Dictionary<String, AnyObject>: 
     println("Dictionary in \(key)") 
    default: 
     println("other") 
    } 
} 

경우

for (key, value) in dictionary { 
    if let v = value as? Dictionary<String, AnyObject> { 
     println("Dictionary in \(key)") 
    } 
} 
+0

이 제네릭 유형에 따라 다릅니다? 문서를 보면 Stirng, Int 등으로 비슷한 것을하는 샘플이 있습니다. https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html – aryaxt

+0

죄송 합니다만, 전에는 답이 잘못되었습니다 - '스위치'구문이 약간 누락되었습니다. –

+1

switch 문에서 여전히 같은 오류가 발생합니다. – aryaxt