swift-protocols

Not able to pass self (which implements a protocol) to init method of a class instantiation.(Swift)

杀马特。学长 韩版系。学妹 提交于 2019-12-23 01:12:09
问题 I have a protocol like so public protocol FooProtocol { associatedType someType func doFoo(fooObject:someType) -> Void } I have a class that implements this protocol public class Foo<T> : FooProtocol { private let _doFoo : (T) -> Void init<P : FooProtocol where P.someType == T>(_dep : P) { _doFoo = _dep.doFoo } public func doFoo(fooObject: T) { _doFoo(fooObject) } } Everything upto here looks good to me, Now in another class I implement the FooProtocol with someType = MyType , then when I try

Make swift class conform to protocol - at static/class level

被刻印的时光 ゝ 提交于 2019-12-22 05:45:13
问题 I'm trying to build a generic UITableViewController subclass in Swift that will accommodate any number of different kinds of table view cells without having internal knowledge of any of them. To do this I'm trying to use protocols for my models and for my table view cells. The protocol for the models will return which cell class I should be going to, and the protocol for the cells will return answers to questions like what the height of the cell should be for a given model. But I'm having a

Protocol extension in Swift 3 [duplicate]

。_饼干妹妹 提交于 2019-12-20 07:47:10
问题 This question already has an answer here : EXC_BAD_ACCESS Using IBInspectable (1 answer) Closed 2 years ago . I want to have a default property of UIImageView , which would be isFlipped . I am able to do it by subclassing UIImageView and adding one property isFlipped . But I want to user protocol and extensions for this , but it is crashing after sometime. Below is my code. How can I use it in right way? Thanks import Foundation import UIKit protocol FlipImage { var isFlipped: Bool { get set

Enum with Raw value, Codable

回眸只為那壹抹淺笑 提交于 2019-12-20 03:52:30
问题 The following code doesn't compile: enum Occupation: String { case designer = "Designer" case engineer = "Engineer" } public struct SteveJobs: Codable { let name: String let occupation: Occupation } On the other hand, it should compile since the Occupation is represented as a String which is Codable . Why can't I use enum with raw value in Codable structs? In particular, why automatic conformance isn't working in such a case. 回答1: Automatic Codable synthesis is “opt-in,” i.e. you have to

Need to satisfy Swift protocol requirement by using a specific subclass of the requirement (or type that conforms to it)

橙三吉。 提交于 2019-12-20 00:21:12
问题 I have a protocol that I've created (in Swift 4.2), and one of its requirements is a property that is of the same type as the protocol itself. As an example, I have a protocol defined like so: protocol A { var a: A? { get set } } I have several Models that conform to this protocol: class Model1: A { var a: A? } class Model2: A { var a: A? } For one of my models, I need to satisfy the protocol requirement by being more specific for the property defined by variable a (i.e. the variable with the

Why do we need a generic here? Isn't the protocol enough?

吃可爱长大的小学妹 提交于 2019-12-19 11:36:52
问题 I found the following example on the web about using generics together with protocols, however I don't understand why do we need generics at all, when all we need is to use a protocol. We define a protocol: protocol Healthy { mutating func setAlive(status: Bool) var health: Int { get } } And then a function using a generic adotping that protocol func check<T:Healthy>(inout object: T) { if (object.health <= 0) { object.setAlive(false) } } I've changed the code as below and everything is still

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

限于喜欢 提交于 2019-12-17 14:08:09
问题 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

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

China☆狼群 提交于 2019-12-17 11:07:09
问题 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,

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

旧城冷巷雨未停 提交于 2019-12-17 02:14:07
问题 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

How to add protocol type as subview

旧巷老猫 提交于 2019-12-13 19:24:30
问题 So I wrote a simple protocol: protocol PopupMessageType{ var cancelButton: UIButton {get set} func cancel() } and have a customView: class XYZMessageView: UIView, PopupMessageType { ... } and then I currently have: class PopUpViewController: UIViewController { //code... var messageView : CCPopupMessageView! private func setupUI(){ view.addSubview(messageView) } } But what I want to do is: class PopUpViewController: UIViewController { //code... var messageView : PopupMessageType! private func