swift-protocols

Non-'@objc' method does not satisfy optional requirement of '@objc' protocol

a 夏天 提交于 2019-11-26 21:34:31
Overview: I have a protocol P1 which provides a default implementation of one of the Objective-C optional functions. When I provide a default implementation of the optional function there is a warning Compiler Warning: Non-'@objc' method 'presentationController(_:viewControllerForAdaptivePresentationStyle:)' does not satisfy optional requirement of '@objc' protocol 'UIAdaptivePresentationControllerDelegate' Version: Swift: 3 Xcode: 8 (public release) Attempts made: Tried adding @objc but doesn't help Question: How do I resolved this ? Is there a work around ? Code: @objc protocol P1 :

Usage of protocols as array types and function parameters in swift

試著忘記壹切 提交于 2019-11-26 18:47:52
问题 I want to create a class that can store objects conforming to a certain protocol. The objects should be stored in a typed array. According to the Swift documentation protocols can be used as types: Because it is a type, you can use a protocol in many places where other types are allowed, including: As a parameter type or return type in a function, method, or initializer As the type of a constant, variable, or property As the type of items in an array, dictionary, or other container However

Why can we not cast to protocol types with associated types but achieve the same effect using generics?

对着背影说爱祢 提交于 2019-11-26 18:38:35
问题 Consider this code: extension Collection { func foo() -> Int { if self.first is Collection { return (self.first as! Collection).underestimatedCount // ERROR } else { return self.underestimatedCount } } } We get the dreaded and apparently widely puzzling: protocol 'Collection' can only be used as a generic constraint because it has Self or associated type requirements. However, this happily compiles: func foo<C: Collection>(_ c: C) -> Int where C.Iterator.Element: Collection { if let first = c

How to define optional methods in Swift protocol?

走远了吗. 提交于 2019-11-26 18:06:57
Is it possible in Swift? If not then is there a workaround to do it? 1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform structs, enums and non- NSObject classes to it. Also, this means you can take advantage of powerful generics system. You can always be sure that all requirements are met when

Pass data between ViewController and ContainerViewController

半腔热情 提交于 2019-11-26 16:43:07
问题 I'm working on an app, and need to pass data between view and containerView. I need to send data and receive data from both Views. Let me explain better: I can change the Label Master (Touch the Container Button) by protocol , but I can not change the Label Container (Touch the Master button). What happens is the Master connects with the container by a following. But do not have a follow Container linking to the Master. I tried to add but segue to, but it worked. The Master View Controller:

What does “Protocol … can only be used as a generic constraint because it has Self or associated type requirements” mean?

会有一股神秘感。 提交于 2019-11-26 14:24:43
I am trying to create a Dictionary (actually a HashSet ) keyed on a custom protocol in Swift, but it is giving me the error in the title: Protocol 'myProtocol' can only be used as a generic constraint because it has Self or associated type requirements and I can't make heads nor tails of it. protocol Observing: Hashable { } var observers = HashSet<Observing>() Protocol Observing inherits from protocol Hashable , which in turn inherits from protocol Equatable . Protocol Equatable has the following requirement: func ==(lhs: Self, rhs: Self) -> Bool And a protocol that contains Self somewhere

How can I make a weak protocol reference in &#39;pure&#39; Swift (without @objc)

我们两清 提交于 2019-11-26 13:53:23
weak references don't seem to work in Swift unless a protocol is declared as @objc , which I don't want in a pure Swift app. This code gives a compile error ( weak cannot be applied to non-class type MyClassDelegate ): class MyClass { weak var delegate: MyClassDelegate? } protocol MyClassDelegate { } I need to prefix the protocol with @objc , then it works. Question: What is the 'pure' Swift way to accomplish a weak delegate ? flainez You need to declare the type of the protocol as class . protocol ProtocolNameDelegate: class { // Protocol stuff goes here } class SomeClass { weak var delegate:

Swift Protocol inheritance and protocol conformance issue

半腔热情 提交于 2019-11-26 12:19:45
问题 protocol BasePresenterProtocol : class {} protocol DashboardPresenterProtocol : BasePresenterProtocol {} final class DashboardPresenter { weak var view: DashboardPresenterProtocol? init() { self.view = DashboardViewController() } func test() { print(\"Hello\") } } extension DashboardPresenter: DashboardViewProtocol { } protocol BaseViewProtocol : class { weak var view: BasePresenterProtocol? { get set } } protocol DashboardViewProtocol : BaseViewProtocol { } class DashboardViewController { }

In Swift, how can I declare a variable of a specific type that conforms to one or more protocols?

别说谁变了你拦得住时间么 提交于 2019-11-26 11:14:34
In Swift I can explicitly set the type of a variable by declaring it as follows: var object: TYPE_NAME If we want to take it a step further and declare a variable that conforms to multiple protocols we can use the protocol declarative: var object: protocol<ProtocolOne,ProtocolTwo>//etc What if I would like to declare an object that conforms to one or more protocols and is also of a specific base class type? The Objective-C equivalent would look like this: NSSomething<ABCProtocolOne,ABCProtocolTwo> * object = ...; In Swift I would expect it to look like this: var object: TYPE_NAME,ProtocolOne/

Non-&#39;@objc&#39; method does not satisfy optional requirement of &#39;@objc&#39; protocol

試著忘記壹切 提交于 2019-11-26 07:59:41
问题 Overview: I have a protocol P1 which provides a default implementation of one of the Objective-C optional functions. When I provide a default implementation of the optional function there is a warning Compiler Warning: Non-\'@objc\' method \'presentationController(_:viewControllerForAdaptivePresentationStyle:)\' does not satisfy optional requirement of \'@objc\' protocol \'UIAdaptivePresentationControllerDelegate\' Version: Swift: 3 Xcode: 8 (public release) Attempts made: Tried adding @objc