swift-protocols

why swift compiler behaves differently with equality operator with/without Equatable protocol

痴心易碎 提交于 2019-12-11 06:54:27
问题 I have a very simple class in a Playground in Swift 4.0 that overrides the == operator. I'm not understanding why the Swift complier doesn't behave the same when the class inherits/doesn't inherit Equatable protocol. Here the class when inheriting Equatable protocol class Test: Equatable { var value = 0 init(_ initialValue:Int) { value = initialValue } static func == (lhs:Test, rhs:Test) -> Bool { return lhs.value == rhs.value ? true : false } } let test1 = Test(0) var test4:Test? = nil if

Protocol methods in a class extension are not called under specific conditions

六月ゝ 毕业季﹏ 提交于 2019-12-11 06:24:27
问题 I encountered a weird behavior. The best way I can put it is … Not overridden protocol methods in a class extension are not called while the superclass already conforms to the protocol (via extension) . However this happens only while it's build with the release build configuration. class A: UIViewController {} extension A: UIScrollViewDelegate { func scrollViewDidScroll(_ scrollView: UIScrollView) { print("scrollViewDidScroll in superclass") } } class B: A { // A tableView (and its data

Protocol bridging NSMutableSet and NSMutableOrderedSet together

旧街凉风 提交于 2019-12-11 05:17:35
问题 In Swift 3 , I would like to be able to create a protocol which allows me to add elements and iterate through using for element in . The protocol should works on both NSMutableSet and NSMutableOrderedSet (since they do not inherit from the same class). I know there are good reasons why NSMutableSet and NSMutableOrderedSet do not inherit from the same class, it is explained here and here. But I want to create a protocol which only makes use of a fraction of all the methods inside NSMutableSet

Unable to see public protocol from framework after converting to Swift 4.1

左心房为你撑大大i 提交于 2019-12-11 05:07:21
问题 I have framework in Swift 2.3 which I have successfully converted to Swift 3.3 and it works as expected on Xcode 8.3.3. I can even see the public protocols in it. After I converted the project to Swift 4.1 and built it, I am unable to see the public protocols in the header file eg: public class CommunicationManager: NSObject { public static var sharedInstance = CommunicationManager() } Communication manager is not visible in Swift4 framework header files. I tried clearing derived data too and

.sort in protocol extension is not working

泄露秘密 提交于 2019-12-11 04:45:36
问题 I have a protocol as well as a protocol extension and I'd like to implement a function in the protocol extension to sort an array defined by the protocol with custom objects, but it's not working. protocol MyProtocol { var myArray: [MyObject] { get set } } extension MyProtocol { func sortArrayByCreationTime() { myArray.sort { $0.created > $1.created } } } Xcode is telling me that 'sort' has been renamed to 'sorted(by:)', but if im using this a new array gets created, but I need the old array

Swift subprotocol with associated type

懵懂的女人 提交于 2019-12-11 04:38:56
问题 I'd like to know how this type of relation (example in kotlin) would be expressed in Swift interface Index<K, V> { fun getAll(key: K): Sequence<V> } I tried to use protocols with associated types, like this: protocol Index { associatedtype Key associatedtype Value associatedtype Result: Sequence where Sequence.Element == Value func getAll(key: Key) -> Result } but this didn't work ( Associated type 'Element' can only be used with a concrete type or generic parameter base ) Then, as a

Swift - internally implement public protocol

别等时光非礼了梦想. 提交于 2019-12-11 04:27:08
问题 Let say I've a protocol that I need to make public. Additionally I've a class that implements this protocol, but in a way that I want to keep internal. So I'd like to do something like: public protocol CustomDelegate { func didLoad() } public class Foo { public var delegate: CustomDelegate? var internalDelegate: CustomDelegate? ... } public class Bar { var foo: Foo init() { self.foo = Foo() self.foo.internalDelegate = self } } extension Bar: CustomDelegate { func didLoad() { print("bar has

where did _ArrayType go in Swift 3?

让人想犯罪 __ 提交于 2019-12-11 02:25:33
问题 I have some code that used to relay on _ArrayType in pre Swift 3. I am trying to understand what happened to public protocol _ArrayType . Any idea? 回答1: _ArrayType was renamed _ArrayProtocol . You can see this in the ArrayType.swift file and this commit. internal protocol _ArrayProtocol : RangeReplaceableCollection, ExpressibleByArrayLiteral { // ... } 来源: https://stackoverflow.com/questions/39708106/where-did-arraytype-go-in-swift-3

How to achieve Protocol Oriented Programming in Python?

醉酒当歌 提交于 2019-12-11 01:46:43
问题 I'm new in Python programming with a strong background in Objective-C and Swift. One of the cool thing I love in Swift is the Protocol Oriented Programming with default implementation. I would like to know if it's possible to have the same behaviour in Python? I'm not sure if it would be a good solution because POP solve problems of the single inheritance but in python we can use multiple inheritance. The thing is that I've heard since the beginning of my programming journey that the multiple

How can you use weak references in Swift generic data structure typed as a protocol?

隐身守侯 提交于 2019-12-10 17:42:35
问题 I am wanting to use weak references in generic data structures; in the example below an Array, but in general any generic type. I can almost get it to work :( My experiments started off well; the following works: // Array of weak references OK struct WeakReference<T: AnyObject> { weak var value: T? } class C { var i: Int = 0 } let c = C() // Strong reference to prevent collection let weakCs = [WeakReference(value: c)] // OK print("C: \(weakCs[0].value!.i)") // 0 I can add a protocol: // Array