swift-protocols

Protocol extension in Swift 3 [duplicate]

我们两清 提交于 2019-12-02 13:02:18
This question already has an answer here: EXC_BAD_ACCESS Using IBInspectable 1 answer I want to have a default property of UIImageView , which would be isFlipped . I am able to do it by subclassing UIImageView and adding one property isFlipped . But I want to user protocol and extensions for this , but it is crashing after sometime. Below is my code. How can I use it in right way? Thanks import Foundation import UIKit protocol FlipImage { var isFlipped: Bool { get set } } extension UIImageView:FlipImage{ var isFlipped: Bool { get { return self.isFlipped } set { self.isFlipped = newValue } } }

Declaring conformance to @objc protocol in empty extension breaks with EXC_BAD_INSTRUCTION

孤街醉人 提交于 2019-12-02 11:26:21
问题 Been having lots and lots of trouble with Swift protocols in combination with arrays, but I couldn't even reproduce my whole problem before things started to break in playground. Here's a minimal example. I have two protocols and a class Bus which declares conformance to one of the protocols. In addition, an empty extension of Bus declares conformance to the other protocol: import Foundation @objc protocol Displayable { var name: String {get} } @objc protocol Utterable { var utterance: String

Enum with Raw value, Codable

点点圈 提交于 2019-12-02 07:23:18
The following code doesn't compile: enum Occupation: String { case designer = "Designer" case engineer = "Engineer" } public struct SteveJobs: Codable { let name: String let occupation: Occupation } On the other hand, it should compile since the Occupation is represented as a String which is Codable . Why can't I use enum with raw value in Codable structs? In particular, why automatic conformance isn't working in such a case. Automatic Codable synthesis is “opt-in,” i.e. you have to declare the conformance explicitly: enum Occupation: String, Codable { // <--- HERE case designer = "Designer"

Declaring conformance to @objc protocol in empty extension breaks with EXC_BAD_INSTRUCTION

。_饼干妹妹 提交于 2019-12-02 05:13:30
Been having lots and lots of trouble with Swift protocols in combination with arrays, but I couldn't even reproduce my whole problem before things started to break in playground. Here's a minimal example. I have two protocols and a class Bus which declares conformance to one of the protocols. In addition, an empty extension of Bus declares conformance to the other protocol: import Foundation @objc protocol Displayable { var name: String {get} } @objc protocol Utterable { var utterance: String {get} } class Bus : Displayable { var name = "a bus"; var utterance = "this is a bus"} extension Bus :

Swift: Implementing Protocol Initializer in a Class

依然范特西╮ 提交于 2019-12-02 05:02:00
问题 I am trying to understand why Swift enforces a class that conforms to a protocol with an initializer to be marked as required. This essentially enforces any subclasses to also implement that initializer. Surely the designated superclass initializer would be inherited? The quotes below are taken from the Swift Language Guide: https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID272

Why does the compiler not see the default code in a protocol?

半城伤御伤魂 提交于 2019-12-02 04:58:56
问题 Edit: I have restated and hopefully clarified this question over here. Now I've added the solution. I've defined a function (see foo() in attached example) as a default function for struct s adopting my protocol . It applies the + operator defined in respect of two other variables which themselves adopt other protocols and + is defined in one of those protocols. The variables are typed using associatedtype s. I get the message: Binary operator '+' cannot be applied to operands of type 'Self

Can Swift protocols be singleton?

假如想象 提交于 2019-12-02 02:21:49
问题 I have tried using the single line singleton initialization(as in a class) for a singleton, here are some error screenshots: Can you help me understand these errors, and also, if a singleton protocol is even possible or not? Thanks in advance 回答1: A protocol itself can't be a singleton. That wouldn't make any sense. A protocol is something that other types conform to. But if you wanted to declare that things that conform to Singleton follow some rule, such as offering a sharedInstance , then

Protocol extending Encodable (or Codable) does not conform to it

天大地大妈咪最大 提交于 2019-12-02 01:09:48
问题 I have 2 protocols, Filters and Parameters , both of which extend Encodable protocol Filters: Encodable { var page: Int { get } } protocol Parameters: Encodable { var type: String { get } var filters: Filters { get } } I create structs conforming to these protocols, thusly… struct BankAccountFilters: Filters { var page: Int var isWithdrawal: Bool } struct BankAccountParamters: Parameters { let type: String = "Bank" var filters: Filters } let baf = BankAccountFilters(page: 1, isWithdrawal:

Protocol extending Encodable (or Codable) does not conform to it

流过昼夜 提交于 2019-12-01 20:58:17
I have 2 protocols, Filters and Parameters , both of which extend Encodable protocol Filters: Encodable { var page: Int { get } } protocol Parameters: Encodable { var type: String { get } var filters: Filters { get } } I create structs conforming to these protocols, thusly… struct BankAccountFilters: Filters { var page: Int var isWithdrawal: Bool } struct BankAccountParamters: Parameters { let type: String = "Bank" var filters: Filters } let baf = BankAccountFilters(page: 1, isWithdrawal: true) let bap = BankAccountParamters(filters: baf) Which fails because… error: type 'BankAccountParamters'

Swift 3: Is there a way to cast an object to a class and protocol at the same time?

假如想象 提交于 2019-12-01 16:12:22
I've read through the relevant sections of Apple's Swift iBook (Type Casting & Protocols) but I can seem to find a way to specify that an object is an instance of a particular class that conforms to a specific protocol. As an example in tableView(_: , cellForRowAt: ) I would like to cast the cell returned by tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) as being a subclass of UITableViewCell that conforms to the RLMEntityCapableCell protocol (Just specifies that conformers have a variable called item that is an instance of Object , or one of its subclasses). This route