swift-protocols

Trying to extend IntegerType (and FloatingPointType); Why can't all Int types be converted to NSTimeInterval

孤人 提交于 2019-12-13 13:15:07
问题 (This probably needs a better title...) I would like to have a set of accessors I can use in code to quickly express time durations. E.g: 42.seconds 3.14.minutes 0.5.hours 13.days This post illustrates that you can't just do it with a simple new protocol, extension, and forcing IntegerType and FloatingPointType to adopt that. So I thought I'd just go the more redundant route and just extend IntegerType directly (and then repeat the code for FloatingPointType ). extension IntegerType { var

All Classes Conforming to Protocol Inherit Default Implementation

…衆ロ難τιáo~ 提交于 2019-12-12 19:06:19
问题 I've added a method to all my UIViewController subclasses that allows me to instantiate it from the class and the storyboard it's inside. All the methods follow this format: class func instantiateFromStoryboard() -> CameraViewController? { let storyboard = UIStoryboard(name: "Camera", bundle: nil) let initial = storyboard.instantiateInitialViewController() guard let controller = initial as? CameraViewController else { return nil } return controller } Instead , I would like to make a protocol,

Extending existing protocol to conform to another protocol

核能气质少年 提交于 2019-12-12 17:02:04
问题 Hello my goal here is to extend (in swift 3) a protocol I have in a framework to conform to another Protocol protocol SomeProtocol {} protocol SomeOtherProtocol {} extension SomeOtherProtocol: SomeProtocol {} The problem here is that I get an error: "Extension of protocol 'SomeOtherProtocol' cannot have an inheritance clause" If this is not possible how do I achieve such thing or what's the "swift way"? 来源: https://stackoverflow.com/questions/44425503/extending-existing-protocol-to-conform-to

Adopting CustomNSError in DecodingError

眉间皱痕 提交于 2019-12-12 09:52:57
问题 I'm writing an error logger using Crashlytics and I've come up against an issue that is making me question my understanding of protocols and dynamic dispatch. When recording non fatal errors using Crashlytics the API expects an Error conforming object, and an optional user info dictionary. I'm looking at JSON decoding errors at the moment, and I wasn't too happy with what I was seeing in the Crashlytics dashboard when I just sent the DecodingError along in recordError. So my solution was to

testing protocol conformance with associated types

老子叫甜甜 提交于 2019-12-12 08:03:29
问题 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

Class Protocol: Cannot use mutating member on immutable value: 'self' is immutable

落花浮王杯 提交于 2019-12-12 04:54:20
问题 I have protocol from framework public protocol BaseMappable { /// This function is where all variable mappings should occur. It is executed by Mapper during the mapping (serialization and deserialization) process. mutating func mapping(map: Map) } and i have my protocol with extension (default implementation) public protocol DtoResponseRequestActionable: class { func transferToResponceAction(_ dto: TransferToResponce) } extension DtoResponseRequestActionable where Self: BaseMappable { func

Deprecating protocols for Swift 3 upgrade

放肆的年华 提交于 2019-12-12 03:45:36
问题 I have an iOS framework that I am upgradding to Swift 3. I would like the API's method signatures to follow the Swift 3 conventions of using a first named parameter for methods while maintaining backward compatibility. It's easy enough to add new API method signatures and deprecate the old ones. But what is the best way to handle this with protocols that are used in delegates? API for Swift 2.x: @objc(FooManager) public class FooManager { public var delegate: FooManagerDelegate? public func

Doesn't conform to protocol error when trying to apply extension with PAT

守給你的承諾、 提交于 2019-12-12 02:26:46
问题 I don't understand why this is failing: import Foundation import simd protocol TestProtocol { associatedtype ElementType func reduce_add(x:Self) -> ElementType } extension float2 : TestProtocol { typealias ElementType=Float } I get a "Type 'float2' does not conform to protocol 'TestProtocol'" error in the Playground. Specifically it tells me: Playground execution failed: Untitled Page.xcplaygroundpage:3:1: error: type 'float2' does not conform to protocol 'TestProtocol' extension float2 :

UICollectionViewCell Protocol is not working correct

老子叫甜甜 提交于 2019-12-11 16:42:51
问题 I have a Protocol inside my CellClass which is detect the function 'editCommentInFeed'. I have create an 'editButton' inside my Cell and define the handler with 'editCommentField' which is calling per Delegate the function and over give the 2 Parameters. This 2 Parameters are important I need this Informations from the cell to detect in future actions which will be provided. For example I had tried to print something when the Button ist Pressed inside the Cell. But even this Statement is not

Fatal error when casting array of types to protocols: cannot be bridged from Objective-C

江枫思渺然 提交于 2019-12-11 11:32:19
问题 There's similar questions out there, but this one is on the latest Swift 2.2 version. Hopefully there's a solution by now because this seems like a big obstacle to Protocol-Oriented Programming in my mind. The below fails on assigning to let results with an error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0X0). protocol P: class { var value:Int {get} } class X: P { var value = 0 init(_ value:Int) { self.value = value } } func getItems() -> [P] { let