swift-protocols

Difference between Swift's hash and hashValue

纵然是瞬间 提交于 2019-12-04 08:03:18
问题 The Hashable protocol in Swift requires you to implement a property called hashValue : protocol Hashable : Equatable { /// Returns the hash value. The hash value is not guaranteed to be stable /// across different invocations of the same program. Do not persist the hash /// value across program runs. /// /// The value of `hashValue` property must be consistent with the equality /// comparison: if two values compare equal, they must have equal hash /// values. var hashValue: Int { get } }

Make a type itself — not its instances — conform to a protocol

╄→尐↘猪︶ㄣ 提交于 2019-12-04 04:10:16
I was wondering if it is possible in Swift to make a type conform to a protocol, so that I can treat the type itself as conforming to a protocol the way one normally treats instances as conforming to a protocol. Example code: protocol P { func f() } class C where C.self: P { // Not actual code static func f() { print("Because C.f: ()->() exists, C.self should satisfy the protocol.") } } 来源: https://stackoverflow.com/questions/42286462/make-a-type-itself-not-its-instances-conform-to-a-protocol

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

心已入冬 提交于 2019-12-03 20:55:57
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() -> BinaryTreeIterator<Self> { return BinaryTreeIterator(self) } } public struct BinaryTreeIterator<Tree: BinaryTree>:

testing protocol conformance with associated types

落爺英雄遲暮 提交于 2019-12-03 15:03:49
I have a protocol that uses an associated type, as such: protocol Populatable { typealias T func populateWith(object: T) } and classes that implement the protocol: class DateRowType: Populatable { func populateWith(object: NSDate) { print(object.description) } } class StringRowType : Populatable { func populateWith(object: String) { print(object) } } but when I try to cast or test for conformance, like this: let drt = DateRowType() let srt = StringRowType() let rowTypes = [drt, srt] let data = [NSDate(), "foo"] for (i, p: Populatable) in enumerate(rowTypes) { p.populateWith(data[i]) } I get

what is 'where self' in protocol extension

耗尽温柔 提交于 2019-12-03 11:06:51
问题 I saw so many examples with below format extension Protocolname where Self: UIViewController What is where Self in protocol extension. I couldn't find the documentation on this. 回答1: That syntax is: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521 Consider: protocol Meh { func doSomething(); } //Extend protocol Meh, where `Self` is of type `UIViewController` //func blah() will only

Self, protocol extension and non-final class

陌路散爱 提交于 2019-12-03 04:30:29
问题 I tried write a static method for UIView which instantiates view of that class from the nib. Method should be generic and work on every UIView subclasses. Also I want to save the type information – so, for example, in this code let myView = MyView.loadFromNib() compiler infers that myView has MyView class. After few trials I decided to use protocol extensions, because otherwise I wouldn't have access to Self inside method body. Looks like this should work: protocol NibLoadable { static func

what is 'where self' in protocol extension

半腔热情 提交于 2019-12-03 01:31:53
I saw so many examples with below format extension Protocolname where Self: UIViewController What is where Self in protocol extension. I couldn't find the documentation on this. That syntax is: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521 Consider: protocol Meh { func doSomething(); } //Extend protocol Meh, where `Self` is of type `UIViewController` //func blah() will only exist for classes that inherit `UIViewController`. //In fact, this entire extension only exists for

Difference between Swift's hash and hashValue

﹥>﹥吖頭↗ 提交于 2019-12-02 20:21:07
The Hashable protocol in Swift requires you to implement a property called hashValue : protocol Hashable : Equatable { /// Returns the hash value. The hash value is not guaranteed to be stable /// across different invocations of the same program. Do not persist the hash /// value across program runs. /// /// The value of `hashValue` property must be consistent with the equality /// comparison: if two values compare equal, they must have equal hash /// values. var hashValue: Int { get } } However, it seems there's also a similar property called hash . What is the difference between hash and

Swift Generic Protocol Function Parameters

大兔子大兔子 提交于 2019-12-02 19:25:40
问题 This seems like it should work to me. All I am trying to do is make the Rule protocol able to performRule on whatever struct adopts that Rule protocol and then return a boolean. However, with the way my code is currently I cannot access any properties on the performRule(:value) value parameter. I feel like I am missing an important concept or something is buggy. You should be able to copy the code below into a playground to see the issue for yourself. import Foundation protocol

Self, protocol extension and non-final class

倾然丶 夕夏残阳落幕 提交于 2019-12-02 17:42:39
I tried write a static method for UIView which instantiates view of that class from the nib. Method should be generic and work on every UIView subclasses. Also I want to save the type information – so, for example, in this code let myView = MyView.loadFromNib() compiler infers that myView has MyView class. After few trials I decided to use protocol extensions, because otherwise I wouldn't have access to Self inside method body. Looks like this should work: protocol NibLoadable { static func loadFromNib(name: String?) -> Self } extension NibLoadable where Self: UIView { static func loadFromNib