2016-11-16 2 views
0

다음 코드를 Swift 3으로 변환하려고합니다. 다른 포스터가이 문제에 답변 한 것처럼 느껴지지만 그 사실을 알 수없는 것 같습니다. classType에 모두 참조에Swift 3 클래스 배열을 반복합니다.

func allowTouchForSubview(subview: UIView) -> Bool { 
    let allowedClasses: [AnyClass] = [UITextField.self, UISearchBar.self, UITextView.self] 
    for classType in allowedClasses { 
     if subview is classType { 
      return true 
     } 
     if let superview = subview.superview { 
      if superview is classType { 
       return true 
      } 
     } 
    } 
    return false 
} 

그것은 오류 및 '선언되지 않은 타입 ClassType의 사용'라고 :

- (BOOL)allowTouchForSubview:(UIView *)subview { 
    NSArray *classes = @[[UITextField class], [UISearchBar class], [UITextView class]]; 
    for (Class class in classes) { 
     if ([subview isKindOfClass:class]) { 
      return YES; 
     } 
     if ([subview.superview isKindOfClass:class]) { 
      return YES; 
     } 
    }; 
    return NO; 
} 

는 여기에 지금까지 무슨이다. 여기서 무슨 일이 일어나고있는거야?

답변

2

이 시도 :

func allowTouchForSubview(subview: UIView) -> Bool { 
     let allowedClasses: [AnyClass] = [UITextField.self, UISearchBar.self, UITextView.self] 

    for classType in allowedClasses { 
     if subview.isKind(of: classType) || (subview.superview?.isKind(of: classType) ?? false) { 
      return true 
     } 
    } 
    return false 
} 

시험 :

let test = UITextView() 
allowTouchForSubview(subview: test)//prints true 

let textField = UITextField() 
allowTouchForSubview(subview: textField)//prints true 


let subview = UIView() 
allowTouchForSubview(subview: subview) //prints false 
test.addSubview(subview) 
allowTouchForSubview(subview: subview) //prints true 
+0

이 일 것으로 보인다. 'is'가 왜 작동하지 않는지, 또는 isKindOf가 실제로하고있는 것과 다른 점은 무엇입니까? – Alex

+0

아마도 브리징과 관련이 있습니다. .kind (of : _)는 동적 디스패치를 ​​사용하여 인스턴스 유형을 해결하는 것처럼 보입니다. –