2017-04-25 30 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) 
    } 
}