2017-12-13 8 views
1

내가 스위프트에에 : #selector (세터 : object.font)

+ (void)setFontName:(NSString*)fontName onObject:(id)object { 
    if ([object respondsToSelector:@selector(setFont:)]) { 
     UIFont* font = [UIFont fontWithName:fontName size:((UIFont *)[object font]).pointSize]; 
     [object setFont:font]; 
    } 
} 

완벽하게 잘 작동이 목표 - C 방법을 재 작성하기 위해 노력하고있어 :

public static func setFontRewriting(fontName: String, object: AnyObject) { 
    if object.responds(to:#selector(setter: object.font)) { 
     let currentFont: UIFont = object.font 
     let fontToSet = UIFont(name: fontName, size: currentFont.pointSize) 
     object.font = fontToSet //doesn't work, getting various syntax errors 
    } 
} 

개체가이 선택기에 응답하는 경우에도 글꼴을 설정할 수 없습니다. object.setFont도 작동하지 않습니다. 어떤 제안?

+1

객체 타입 AnyObject이다. 세터를 부르기 전에 그것을 캐스팅해야합니다. – ClemensL

+1

다음을 읽어주십시오 : [SWIFT와 CUSTOM OF ANYOBJECT] (http://blog.scottlogic.com/2014/09/24/swift-anyobject.html) 아마 스위프트와 AnyObject에 대해 더 잘 이해할 수 있습니다. – Kevinosaurio

답변

1

는 다음과 같은 작업을 수행 할 수 있습니다

object.setValue(fontToSet, forKey: "font")