swift-protocols

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

旧时模样 提交于 2019-12-01 15:23:52
问题 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

Why do we need a generic here? Isn't the protocol enough?

这一生的挚爱 提交于 2019-12-01 12:30:34
I found the following example on the web about using generics together with protocols, however I don't understand why do we need generics at all, when all we need is to use a protocol. We define a protocol: protocol Healthy { mutating func setAlive(status: Bool) var health: Int { get } } And then a function using a generic adotping that protocol func check<T:Healthy>(inout object: T) { if (object.health <= 0) { object.setAlive(false) } } I've changed the code as below and everything is still fine. func check( object: inout Healthy) { if (object.health <= 0) { object.setAlive(status: false) } }

Implement protocol through extension [duplicate]

白昼怎懂夜的黑 提交于 2019-12-01 08:17:32
问题 This question already has answers here : Swift: Using protocol extension results in “unrecognized selector sent to instance” (2 answers) Closed 3 years ago . I'm trying to create a protocol that wraps the process of using the UIImagePickerController to make it more stream-lined in my apps. I essentially have something like this: public protocol MediaAccessor : UIImagePickerControllerDelegate, UINavigationControllerDelegate { func mediaCaptured(title: String, fileData: NSData, fileType: String

Creating a generic UViewController initialiser

此生再无相见时 提交于 2019-12-01 06:47:56
I'm trying to create a UIViewController extension that I can use to initialise new instances. For each view controller in my project I have a corresponding storyboard. i.e. EditSomethingViewController.swift EditSomethingViewController.storyboard This is what I have so far: extension UIViewController { static func initalize() -> UIViewController? { let name = String(self) let storyboard = UIStoryboard(name: name, bundle: nil) return storyboard.instantiateInitialViewController() } } However this means that when I use it, I still have to cast the response. i.e. if let viewController =

How Does AnyObject Conform to NSObjectProtocol?

我的未来我决定 提交于 2019-11-30 19:35:41
This question was inspired by mz2's answer on the question Check for object type fails with "is not a type" error . Consider an empty Swift class: class MyClass { } Attempting to call any NSObjectProtocol methods on an instance of this class will result in a compile-time error: let obj = MyClass() obj.isKindOfClass(MyClass.self) // Error: Value of type 'MyClass' has no member 'isKindOfClass' However, if I cast the instance as AnyObject , my object now conforms to NSObjectProtocol and I can call the instance methods defined by the protocol: let obj: AnyObject = MyClass() obj.isKindOfClass

How to make protocol associated type require protocol inheritance and not protocol adoption

戏子无情 提交于 2019-11-30 18:05:43
问题 In my swift project I have a case where I use protocol inheritance as follow protocol A : class{ } protocol B : A{ } What Im trying to achieve next is declaring another protocol with associated type, a type which must inherit from protocol A . If I try to declare it as : protocol AnotherProtocol{ associatedtype Type : A weak var type : Type?{get set} } it compiles without errors but when trying to adopt AnotherProtocol in the following scenario: class SomeClass : AnotherProtocol{ typealias

Creating a generic UViewController initialiser

一个人想着一个人 提交于 2019-11-30 17:20:40
问题 I'm trying to create a UIViewController extension that I can use to initialise new instances. For each view controller in my project I have a corresponding storyboard. i.e. EditSomethingViewController.swift EditSomethingViewController.storyboard This is what I have so far: extension UIViewController { static func initalize() -> UIViewController? { let name = String(self) let storyboard = UIStoryboard(name: name, bundle: nil) return storyboard.instantiateInitialViewController() } } However

How to make an enum conform to a protocol in Swift?

送分小仙女□ 提交于 2019-11-30 10:07:35
问题 Swift documentation says that classes , structs , and enums can all conform to protocols, and I can get to a point where they all conform. But I can't get the enum to behave quite like the class and struct examples: protocol ExampleProtocol { var simpleDescription: String { get set } mutating func adjust() } class SimpleClass: ExampleProtocol { var simpleDescription: String = "A very simple class." var anotherProperty: Int = 69105 func adjust() { simpleDescription += " Now 100% adjusted." } }

Why am I allowed to set a read only property of a protocol using a struct that inherits said protocol?

点点圈 提交于 2019-11-30 09:47:54
问题 I'm following a tutorial on the protocol oriented programming paradigm in which I'm confused by something I thought was rather simple which is read only properties of protocols or getters and setters. My understanding is that a read only property is signified by using the keyword 'get' when declaring a variable within a protocol. I was excited so I quickly coded created a playground to see if my thinking was accurate however it appears that I can still change the property which I thought was

Swift 2 Error using mutating function in Protocol extension \"Cannot use mutating member on immutable value: 'self' is immutable

China☆狼群 提交于 2019-11-30 05:35:31
Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc , when I try and use mtkAnimQueAppend declared in the extension, I get this error: "Cannot use mutating member on immutable value: 'self' is immutable. protocol MTKAnimateValueDelegate { var mtkAnimQue:[MTKAnimateValue]? {get set} } extension MTKAnimateValueDelegate { ///Adds element to que mutating func mtkAnimQueAppend(element:MTKAnimateValue) { if mtkAnimQue != nil { mtkAnimQue?