我正在尝试创建一个协议,包含使用UIImagePickerController的过程,使其在我的应用程序中更流线型.我基本上有这样的事情:
public protocol MediaAccessor : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func mediaCaptured(title: String, fileData: NSData, fileType: String)
}
Run Code Online (Sandbox Code Playgroud)
和一个扩展,它完成了请求权限和处理委托方法的所有繁重工作:
public extension MediaAccessor where Self : UIViewController {
public func captureMedia() {
//All sorts of checks for picker authorization
let picker = UIImagePickerController()
picker.delegate = self
self.presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
//implementation of the delegate in extension
//even though everything compiles, this method is not called on picker completion
}
} …Run Code Online (Sandbox Code Playgroud) 我想创建一个继承UICollectionViewDataSource和使用协议扩展的协议,以提供所需UICollectionViewDataSource方法的默认实现.但是,当我尝试声明符合此协议的类时,编译器会说Type 'MyClass' does not conform to protocol UICollectionViewDataSource'.
protocol MyDataSource : UICollectionViewDataSource {
var values: [String] { get }
}
extension MyDataSource {
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = UICollectionViewCell() // TODO: dequeue
// TODO: configure cell ...
return cell
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.values.count
}
}
class MyClass : NSObject, MyDataSource {
var values = [String]()
}
Run Code Online (Sandbox Code Playgroud)
我也尝试将扩展名声明为以下内容,但仍然收到相同的编译器错误:
extension UICollectionViewDataSource …Run Code Online (Sandbox Code Playgroud)