swift-protocols

Usage of protocols as array types and function parameters in swift

我的梦境 提交于 2019-11-27 16:54:53
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 the following generates compiler errors: Protocol 'SomeProtocol' can only be used as a generic constraint

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

做~自己de王妃 提交于 2019-11-27 16:27:00
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.first { return first.underestimatedCount // * } else { return c.underestimatedCount } } Why?! In

Swift: Failed to assign value to a property of protocol?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 15:42:46
Class A provides a string value. Class B has two members of A type inside itself, and provide a computed property "v" to choose one of them. class A { var value: String init(value: String) { self.value = value } } class B { var v1: A? var v2: A = A(value: "2") private var v: A { return v1 ?? v2 } var value: String { get { return v.value } set { v.value = newValue } } } This code is simple and it works. Since both the A and B have a member "value", I make it a protocol like this: protocol ValueProvider { var value: String {get set} } class A: ValueProvider { var value: String init(value: String

Pass data between ViewController and ContainerViewController

隐身守侯 提交于 2019-11-27 14:22:06
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: import UIKit protocol MasterToContainer { func changeLabel(text:String) } class Master: UIViewController

Why do Self and self sometimes refer to different types in static functions?

无人久伴 提交于 2019-11-27 14:05:58
Recently I have been developing multiple heavily protocol-oriented application frameworks with Swift and noticed a few (seemingly) odd behaviors with static functions in protocol extensions, specifically where the extension functions are invoked from metatypes. The way I initially discovered these behaviors was in troubleshooting a bug where the type of an object changed in a seemingly impossible way. I traced the problem down and eventually determined that it is because in a static function, Self and self can potentially hold different types (note: I've taken to calling these "Big S Self" and

Arrays of Generics in Swift

天涯浪子 提交于 2019-11-27 10:26:17
问题 I've been playing around with arrays of generic classes with different types. It's easiest to explain my problem with some sample code: // Obviously a very pointless protocol... protocol MyProtocol { var value: Self { get } } extension Int : MyProtocol { var value: Int { return self } } extension Double: MyProtocol { var value: Double { return self } } class Container<T: MyProtocol> { var values: [T] init(_ values: T...) { self.values = values } func myMethod() -> [T] { return values } } Now

Storing/passing Function Types from swift protocols

萝らか妹 提交于 2019-11-27 08:45:10
问题 This bit of code crashes the swift (3, 3.1, 4) compiler: protocol Test { func f() } let x = Test.f // crash I would expect, perhaps naively, that the compiler would infer x as a Function Type with the signature (Test) -> (Void) -> Void , and that later on, I could call it like so: let y = SomeClassConformingToTest() x(y)() I guess my question is: clearly the compiler should do something other than crash, but should Swift currently support this syntax? 回答1: As you say, the compiler should

Make property of type and also conform to protocol in Swift

心已入冬 提交于 2019-11-27 03:21:01
问题 I would like to make a property that is of a certain type and also conforms to a protocol, which I would have done in Objective-C like this: @property (nonatomic) UIViewController<CustomProtocol> *controller; What I am looking for is to specify that the property can be set with an object of type UIViewController that also conforms to CustomProtocol, so that it's clear what the base class is. I know I could probably just use a short class stub to get the same results, i.e. class

A Swift protocol requirement that can only be satisfied by using a final class

限于喜欢 提交于 2019-11-27 02:44:46
问题 I'm modeling a owner/ownee scheme on Swift: class Owner<T: Ownee> { // ... } protocol Ownee { var owner: Owner<Self> { get } } Then I have a pair of classes professor/student that adhere to the modeled types above: class Professor: Owner<Student> { // ... } class Student: Ownee { let professor: Professor var owner: Owner<Student> { // error here (see below) return professor } init(professor: Professor) { self.professor = professor } } However I get the following error on the definition of var

Swift Protocol inheritance and protocol conformance issue

徘徊边缘 提交于 2019-11-26 23:19:11
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 { } extension DashboardViewController: DashboardPresenterProtocol { } In the above code, I get an error at