swift-protocols

Swift protocol property in protocol - Candidate has non-matching type

让人想犯罪 __ 提交于 2019-12-10 15:23:43
问题 I have a protocol (ProtocolA) containing a single property conforming to a second protocol (ProtocolB). public protocol ProtocolA { var prop: ProtocolB? { get } } public protocol ProtocolB { } I'm trying to declare two classes that will implement those: private class ClassA : ProtocolA { var prop: ClassB? } private class ClassB : ProtocolB { } But I get an error: Type 'ClassA' does not conform to protocol 'ProtocolA' Protocol requires property 'prop' with type 'ProtocolB?' Candidate has non

How can i stop my tableView cell from reusing the button inside it?

流过昼夜 提交于 2019-12-10 12:08:49
问题 I have a function inside a protocol that takes a TableViewcell as an argument. protocol GoingButtonDelegate { func goingButtonPressed(cell: TableViewCell) } class TableViewCell: UITableViewCell { // Outlets @IBOutlet weak var goingButton: UIButton! var delegate: GoingButtonDelegate? @IBAction func goingButtonTapped(_ sender: Any) { delegate?.goingButtonPressed(cell: self) } I then go over to my ViewController and implement the delegate and it's function, which is to change the image of a

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

老子叫甜甜 提交于 2019-12-10 10:27:35
问题 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

Limits of Protocol Extensions and defaults in swift2

隐身守侯 提交于 2019-12-10 09:56:31
问题 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

Mocking a static class method in a swift unit test in a swifty way?

自闭症网瘾萝莉.ら 提交于 2019-12-10 01:58:49
问题 I'm a seasoned Objective-c programmer but I can't say the same for Swift, I'm having a hard time unit testing a class in swift without using frameworks like OCMock. The Problem : I'm integrating Firebase into a mixed Objective-C/Swift project, and I need to configure it based on the build configuration of the app. I've written a Swift class for that (that will be used by the obj-c app delegate), however since the firebase framework is configured trough a static class method, precisely FIRApp

Protocol Having generic function and associatedType

﹥>﹥吖頭↗ 提交于 2019-12-09 20:17:50
问题 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

AnyObject try cast to Equatable

≯℡__Kan透↙ 提交于 2019-12-07 15:05:18
问题 I have an Equatable class class Item: Equatable { var value: AnyObject? var title: String init(title: String, value: AnyObject?) { self.title = title self.value = value } //Equatable public static func ==(lhs: Item, rhs: Item) -> Bool { return ((lhs.title == rhs.title) && (lhs.value === rhs.value)) } } But I want cast try var value to Equatable, So that get soft equatable result if let lValue = lhs.value as? Equatable, // Error let rValue = rhs.value as? Equatable { // Error valueEq = (lValue

Swift 2 to 3 Migration for Swift Sequence Protocol

 ̄綄美尐妖づ 提交于 2019-12-07 05:19:29
问题 I'm attempting to convert the following code from this library (https://github.com/dankogai/swift-json) into Swift 3 Compatible code. I'm having a tough time figuring out how to convert the Sequence protocol used in Swift 2 with the correct version for Swift 3. I can't find any documentation on Swift 2 Sequence protocol changes as compared to 3. Here is the code that I currently have converted as much as possible to Swift 3 extension JSON : Sequence { public func generate()->AnyIterator<

When to use protocol in Swift

℡╲_俬逩灬. 提交于 2019-12-07 01:06:09
问题 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

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

删除回忆录丶 提交于 2019-12-06 19:01: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