2014-09-23 75 views
1

我創建了UIView的擴展,在UIKeyboard上爲UITextField/UITextView添加UIToolbar。Swift:將對象轉換爲其他對象

extension UIView { 

    public func addDoneOnKeyboardWithTarget (target : AnyObject, action : Selector) { 

     //Creating UIToolbar 
     var toolbar = UIToolbar() 

     //Configuring toolbar 
     var items = NSMutableArray() 
     var nilButton = IQBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil) 
     items.addObject(nilButton) 
     var doneButton = IQBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: target, action: doneAction) 
     items.addObject(doneButton) 
     toolbar.items = items 

     //Now typecasting self to UITextField for compilation purposes because `inputAccessoryView` is readonly for UIView. it's readwrite for UITextField and UITextView both. 
     var textField : UITextField = self as UITextField //Runtime error for UITextView 

     //Setting new toolbar as inputAccessoryView 
     textField.inputAccessoryView = toolbar 
    } 
} 

它適用於UITextField,但是當我在UITextView上調用上述函數時,它在類型轉換時崩潰。


嘗試1: 我試過將其轉換爲可選

var textField : UITextField? = self as? UITextField 
textField?.inputAccessoryView = toolbar 

現在,它不會對UITextView的崩潰,但也沒有設置任何inputAccessoryView。當我試圖打印它。它在控制檯上打印nil

println(textField?.inputAccessoryView) //It prints 'nil' 

嘗試2: 我想它轉換爲AnyObject

var textField : AnyObject = self as AnyObject 
textField.inputAccessoryView = toolbar //Compile time error 

如果我更換的UITextField到那麼明顯的UITextView它工作正常進行的UITextView但不適合的UITextField。

有什麼建議嗎?

+1

一個TextView沒有文本框和任何對象有沒有方法inputAccessoryView - SWIFT強制類型安全 – 2014-09-23 21:13:56

+0

是的,我知道,但我應該如何克服上述問題。我將我的開源庫轉換爲swift https://github.com/hackiftekhar/IQKeyboardManager/blob/master/IQKeyBoardManager/IQUIView%2BIQKeyboardToolbar.m見175行 – 2014-09-23 21:17:57

回答

4

1一個TextView沒有文本框
至2任何物體都有inputAccessoryView
沒有方法 - >> SWIFT強制類型安全

所以你不能只投對象的東西,他們不

從文檔


「的UIResponder類聲明瞭兩個屬性輸入視圖和輸入附件的觀點:
@property(只讀,保留)UIView * inputView;
@property(只讀,保留)的UIView * inputAccessoryView;」

但它是隻讀的,有


所以檢查類

var xy:AnyObject!; 

    if(xy.isKindOfClass(UITextField)) { 
     var t = xy as UITextField; 
     //... 
    } 
    else if(xy.isKindOfClass(UITextView)) { 
     var t = xy as UITextView; 
     //... 
    } 
+0

是的,但是UIResponder聲明它們是隻讀的。我無法克服這個問題。我在UIView上創建了擴展,因爲它包含在UITextField和UITextView類層次結構中。對於UIView,這些屬性仍然是隻讀的。 – 2014-09-24 13:06:33

+0

編輯一個簡短的如果基於類切換[未經測試,但應] – 2014-09-24 13:19:54

+0

它工作,但如何檢查我的實例是否響應'setInputAccessoryView:'然後調用'setInputAccessoryView:'這種方法? – 2014-09-24 15:47:33

2

在斯威夫特,你可以使用type check運算符(is)來檢查實例是否屬於某個子類型。類型檢查操作如果實例是該子類的實例,則爲returns true,如果不是,則爲false

let aViewInstance : UIView = UITextView() 

if aViewInstance is UITextField 
{ 
    // here, aViewInstance must be an instance of UITextField class & can't be nil 
    print(aViewInstance) 
    let textField : UITextField = aViewInstance as! UITextField 
    print(textField) 
} 
else if aViewInstance is UITextView 
{ 
    // here, aViewInstance must be an instance of UITextView class & can't be nil 
    print(aViewInstance) 
    let textView : UITextView = aViewInstance as! UITextView 
    print(textView) 
} 
else 
{ 
    print(" other object ") 
} 

所以你不需要檢查subclass type,使用NSObject類的isKindOfClass方法斯威夫特因爲isKindOfClass方法僅在NSObject的信息,並將它的子類(所有的Objective-C類)。和Swift,可以創建一個沒有基類或超類的類。 例如 -

class BaseClass { 
//properties and methods 
} 

let anObject : AnyObject = BaseClass() 

if anObject is BaseClass { 
    print(anObject) // here, must be an instance of BaseClass, you can use it 
} else { 
    print(" other object ") 
}