protocol-extension

Swift protocol extension method dispatch with superclass and subclass

寵の児 提交于 2019-12-29 06:52:27
问题 I found an interesting behaviour which seems like a bug... Based on the behaviour described the following articles: https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94 http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future The output is not what I expect, when I add SomeSuperclass , rather than directly adopting the protocol. protocol TheProtocol { func method1() } extension TheProtocol { func method1() { print("Called method1 from protocol

Swift 2: UITableViewDataSource protocol extension

a 夏天 提交于 2019-12-21 05:26:14
问题 I have been playing around with protocol extensions and I have a problem. Maybe what I want to achieve can’t be done. I have this playground: //: Playground - noun: a place where people can play import UIKit protocol ArrayContainer { typealias T var array: [T] { get } } class MyViewController: UIViewController, ArrayContainer, UITableViewDataSource { typealias T = String var array = ["I am", "an Array"] } extension UITableViewDataSource where Self: ArrayContainer { func

Swift property observer in protocol extension?

﹥>﹥吖頭↗ 提交于 2019-12-18 12:49:17
问题 Consider the following: protocol ViewControllable: class { typealias VM: ViewModellable var vm: VM! { get } func bind() } extension ViewControllable { var vm: VM! { didSet { bind() } } } I'm trying to observe vm property and call bind whenever it is injected. But this doesn't compile with error saying: Extensions may not contain stored properties which makes sense since protocol cannot enforce properties to be stored or computed . Is this possible to accomplish without introducing class

Protocol extension on an ObjC protocol

廉价感情. 提交于 2019-12-18 02:41:50
问题 I have an Objective-C protocol which is used by mostly objective-C objects and one or two Swift objects. I would like to extend the protocol in Swift and add 2 functions. One to register for a notification and another to handle the notification. If I add these func registerForPresetLoadedNotification() { NSNotificationCenter.defaultCenter().addObserver(self as AnyObject, selector: #selector(presetLoaded(_:)), name: kPresetLoadedNotificationName, object: nil) } func presetLoaded(notification:

How to set delegate in a protocol extension

我只是一个虾纸丫 提交于 2019-12-10 23:07:06
问题 I have multiple view controllers which shows same kind of cells. I want to set delegate in a protocol extension like this: class ProductsViewController: UIViewController, ProductShowcase { //other properties @IBOutlet weak var productCollectionView: UICollectionView! var dataSource: DataSource! override func viewDidLoad() { super.viewDidLoad() setupDataSource() setupCollectionView() } func didSelectProduct(product: Product) { print(product) } //other functions } protocol ProductShowcase:

Swift protocol extension implementing another protocol with shared associated type

笑着哭i 提交于 2019-12-08 19:36:24
问题 Consider the following: protocol Foo { typealias A func hello() -> A } protocol FooBar: Foo { func hi() -> A } extension FooBar { func hello() -> A { return hi() } } class FooBarClass: FooBar { typealias A = String func hi() -> String { return "hello world" } } This code compiles. But if I comment out explicit definition of associated type typealias A = String , then for some reason, swiftc fails to infer the type. I'm sensing this has to do with two protocols sharing the same associated type

Calling selector from protocol extension

三世轮回 提交于 2019-12-04 04:33:10
I'm building simple theme engine and would like have an extension which adds UISwipeGestureRecognizer to UIViewController Here is my code: protocol Themeable { func themeDidUpdate(currentTheme: Theme) -> Void } extension Themeable where Self: UIViewController { func switchCurrentTheme() { Theme.switchTheme() themeDidUpdate(Theme.currentTheme) } func addSwitchThemeGestureRecognizer() { let gestureRecognizer = UISwipeGestureRecognizer(target: self, action:#selector(Self.switchCurrentTheme)) gestureRecognizer.direction = .Down gestureRecognizer.numberOfTouchesRequired = 2 self.view

Swift 2 Protocol Extensions and Conformance for Objective-C Types

偶尔善良 提交于 2019-12-02 11:20:51
问题 I have a setup like this: @interface Model: NSManagedObject ... @end And a Swift protocol like this: @objc protocol Syncable { var uploadURL: String { get } var uploadParams: [String: AnyObject]? { get } func updateSyncState() throws } extension Syncable where Self: NSManagedObject { func updateSyncState() throws { ... /* default implementation */ ... } } In a new Swift file, I try to do this: extension Model: Syncable { var uploadURL: String { return "a url" } var uploadParams: [String:

Swift property observer in protocol extension?

别来无恙 提交于 2019-11-30 11:22:11
Consider the following: protocol ViewControllable: class { typealias VM: ViewModellable var vm: VM! { get } func bind() } extension ViewControllable { var vm: VM! { didSet { bind() } } } I'm trying to observe vm property and call bind whenever it is injected. But this doesn't compile with error saying: Extensions may not contain stored properties which makes sense since protocol cannot enforce properties to be stored or computed . Is this possible to accomplish without introducing class inheritance ? In other words, Can I observe the change of a property inside protocol extension? No, this is

Can Swift Method Defined on Extensions on Protocols Accessed in Objective-c

孤街醉人 提交于 2019-11-30 00:38:02
问题 Is it possible to call methods defined in a protocol extension in Swift from Objective-C? For example: protocol Product { var price:Int { get } var priceString:String { get } } extension Product { var priceString:String { get { return "$\(price)" } } } class IceCream : Product { var price:Int { get { return 2 } } } The price string of an instance of IceCream is '$2' and can be accessed in Swift, however the method is not visible in Objective-C. The compiler throws the error 'No visible