swift-protocols

Can I extend Tuples in Swift?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 18:17:52
问题 I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method: let t = (1, "one") let s = t.swap such that s would be of type (String, Int) with value ("one", 1) . (I know I can very easily implement a swap(t) function instead, but that's not what I'm interested in.) Can I do this? I cannot seem to write the proper type name in the extension declaration. Additionally, and I suppose the answer is the same, can I make a 2-tuple adopt a

Protocol Extension Initializer forcing to call self.init

有些话、适合烂在心里 提交于 2019-12-06 05:43:31
问题 I was just reading the Apple Swift 4 document regarding the Protocol Initializer Requirements and providing a default implementation in the protocol extension. import UIKit protocol Protocol { init() } extension Protocol { init() { print("SDf") self.init() // Line 1 // Compiler error occured if this is omitted //"'self.init' isn't called on all paths before returning from initializer" } } struct Structure: Protocol { init(string: String) { } } Structure() // Line 2 Now as you can see, the

How to use a protocol with optional class methods in an extension with generic in Swift?

怎甘沉沦 提交于 2019-12-06 00:23:33
I am trying using extension for an existing class with class method like: @objc public protocol MyProtocol { optional class func foo() -> Int } And I am using this protocol in an extension with generic like: extension MyClass { public func bar<T: MyProtocol>() { ... let x: Int = T.self.foo!() // if I do not use "self" or "!" here, I will have a compiler error ... } This should work but when I build it, Xcode says "Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1". If I do not use "optional" in the protocol, I do not

Limits of Protocol Extensions and defaults in swift2

安稳与你 提交于 2019-12-05 22:45:48
So I was playing around with protocol extensions and i ran into an "interesting" problem. I wanted to write a Meters and Kilometers units type for testing some things out. Its VERY easy to do this as a class where there is a base class and both sub classes override the base, while just overriding a simple value //Conversion factor between types enum DISTANCE_UNIT_TYPE : Double { case METER = 1.0; case KILOMETER = 0.001; } protocol DistanceUnit { var unitType : DISTANCE_UNIT_TYPE {get} var value : Double { get set } var baseValue : Double { get set } } struct Kilometers : DistanceUnit { var

Conforming a new protocol to Sequence with a default makeIterator() implementation

こ雲淡風輕ζ 提交于 2019-12-05 08:33:58
问题 I made a (very basic) BinaryTree protocol: public enum BinaryTreeChildSide { case left, right } public protocol BinaryTree { associatedtype Element associatedtype Index func child(of index: Index, side: BinaryTreeChildSide) -> Index? var rootIndex: Index? { get } subscript(position: Index) -> Element { get } } For a basic iterative in-order traversal, I made a BinaryTreeIterator (note that I don't implement Sequence just yet): public extension BinaryTree { func makeIterator() ->

When to use protocol in Swift

非 Y 不嫁゛ 提交于 2019-12-05 04:06:26
I ask a question today because I’m little bit lost today. It’s about Swift and protocol and more about Protocol Oriented Programming (POP). I read articles about it, even a book but I’m still confused. Everybody seems to say that Protocol is a great tool and so on but I don’t really understand its power. I’ve a question because I’m coding a class Volume which is the representation of a volume as an object. Let’s say struct Volume { var value: Float = 1 var isLogScale: Bool = false var maxLogVolume: Float = 6.0 // value in dB var maxLinearVolume: Float = 1.0 let dynamicRange: Float = 50.0 func

How to conform to a protocol's variables' set & get?

╄→гoц情女王★ 提交于 2019-12-05 00:33:13
I'm playing around with protocols and how to conform to them. protocol Human { var height: Int {get set} } struct boy : Human { var height: Int {return 5} // error! } I'm trying to learn different ways that I can implement set and get. However the code above throws the following error: type 'boy' does not conform to protocol 'Human' However writing as below won't have any errors: struct boy : Human { var height = 5 // no error } I don't understand the difference nor what exactly needs to be implemented when you can also set a variable. I looked into different questions and tutorials but they

Can I extend Tuples in Swift?

◇◆丶佛笑我妖孽 提交于 2019-12-04 23:35:05
I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method: let t = (1, "one") let s = t.swap such that s would be of type (String, Int) with value ("one", 1) . (I know I can very easily implement a swap(t) function instead, but that's not what I'm interested in.) Can I do this? I cannot seem to write the proper type name in the extension declaration. Additionally, and I suppose the answer is the same, can I make a 2-tuple adopt a given protocol? You cannot extend tuple types in Swift. According to Types , there are named types

Protocol Having generic function and associatedType

血红的双手。 提交于 2019-12-04 17:34:43
I've the following code: protocol NextType { associatedtype Value associatedtype NextResult var value: Value? { get } func next<U>(param: U) -> NextResult } struct Something<Value>: NextType { var value: Value? func next<U>(param: U) -> Something<Value> { return Something() } } Now, the problem is in the Something implementation of next . I want to return Something<U> instead of Something<Value> . But when I do that I got the following error. type 'Something<Value>' does not conform to protocol 'NextType' protocol requires nested type 'Value' ghashi I tested the following codes and they

Swift Protocols with Associated Type Requirement and Default Implementation

倾然丶 夕夏残阳落幕 提交于 2019-12-04 14:11:21
I have been struggling very hard with Swift Protocols and Associated Types for a long time. I started again with basic to really understand what is going wrong and I followed this article of TypeErasure in Swift Protocols with Associated Type Requirement by Rob Napier but still i have no luck. Find the code below // An Animal can eat protocol Animal { associatedtype Food func feed(food: Food) -> Void } struct AnyAnimal<Food>: Animal { private let _feed: (Food) -> Void init<Base: Animal where Food == Base.Food>(_ base: Base) { _feed = base.feed } func feed(food: Food) { _feed(food) } } // Kinds