2017-04-25 61 views
-2

我已經開始學習斯威夫特3幾個星期,我有一個項目,我必須轉換從斯威夫特2斯威夫特3.轉換功能與通用型SWIFT 2〜3迅速

我讀過許多文件,但我不知道如何轉換這個功能(我不知道要搜索的關鍵字)。

func pickImageFromCamera<T: UIViewController(_ delegate: T) where T: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>(){ 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){ 
     let myPickerController = UIImagePickerController() 
     myPickerController.delegate = delegate; 
     myPickerController.sourceType = UIImagePickerControllerSourceType.camera 
     delegate.present(myPickerController, animated: true, completion: nil) 
    } 
} 

它提供了以下錯誤信息:

expected '>' to complete generic argument list 

更新: 這是原代碼:

func pickImageFromCamera<T: UIViewController where T: protocol<UIImagePickerControllerDelegate, UINavigationControllerDelegate>>(delegate: T){ 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ 
     let myPickerController = UIImagePickerController() 
     myPickerController.delegate = delegate; 
     myPickerController.sourceType = UIImagePickerControllerSourceType.Camera 
     delegate.presentViewController(myPickerController, animated: true, completion: nil) 
    } 
} 

有人能幫助我嗎?非常感謝。

+4

請用您試圖使用的Swift 3代碼編輯您的問題,並清楚地指出您遇到問題的部分。 – rmaddy

+0

'協議<>'表示法不再使用。 – matt

+0

對不起。我已經更新了這個問題。我認爲代碼已部分轉換,但尚未完全。 – thaotruong58

回答

0

當我第一次遇到它時,我掙扎於這個自我。

func pickImageFromCamera(){ 
    if UIImagePickerController.isSourceTypeAvailable(.camera){ 
     let myPickerController = UIImagePickerController() 
     myPickerController.delegate = self 
     myPickerController.sourceType = .camera 
     self.present(myPickerController, animated: true, completion: nil) 
    } 
} 
+0

你可以用'.camera'代替'UIImagePickerControllerSourceType.camera'。 – rmaddy

+0

@maddy Yup,I忘了解決,謝謝指出。 – MwcsMac

+0

你錯過了一個。 – rmaddy

0

謝謝大家,我找到了解決方案。因爲自動轉換會使這個功能變得如此混亂。

func pickImageFromCamera<T: UIViewController>(delegate: T) where T: UIImagePickerControllerDelegate & UINavigationControllerDelegate { 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){ 
     let myPickerController = UIImagePickerController() 
     myPickerController.delegate = delegate; 
     myPickerController.sourceType = UIImagePickerControllerSourceType.camera 
     delegate.present(myPickerController, animated: true, completion: nil) 
    } 
}