swift-protocols

How Does AnyObject Conform to NSObjectProtocol?

自作多情 提交于 2019-11-30 03:48:59
问题 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

Type CCC doesnt conform to protocol 'NSObjectProtocol'

微笑、不失礼 提交于 2019-11-30 00:40:25
问题 I don't understand why my code doesn't work. Here it is: class Test: NSURLSessionDataDelegate { func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) { if(error == nil) { print("Hallo") } else { print(error?.userInfo) } } func createRequest() { let dictionary = [ "mailAddress":"foo@example.com", ..... ] let nsData: NSData? do { nsData = try NSJSONSerialization.dataWithJSONObject(dictionary, options: NSJSONWritingOptions(rawValue:0)) } catch _ {

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

我的未来我决定 提交于 2019-11-29 16:53:16
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 read only. What am I doing wrong to make it a true read only property to where I can't set it? protocol

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

老子叫甜甜 提交于 2019-11-29 10:34:40
问题 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

Function that takes a protocol and a conforming class (!) instance as parameters

依然范特西╮ 提交于 2019-11-29 07:20:40
I am trying to figure out how to define a function which takes the following two parameters: A protocol. An instance of a class (a reference type ) conforming to that protocol. For example, given protocol P { } class C : P { } // Class, conforming to P class D { } // Class, not conforming to P struct E: P { } // Struct, conforming to P this should compile: register(P.self, obj: C()) // (1) but these should not compile: register(P.self, obj: D()) // (2) D does not conform to P register(P.self, obj: E()) // (3) E is not a class It is easy if we drop the condition that the second parameter is a

Arrays of Generics in Swift

孤街醉人 提交于 2019-11-28 17:24:21
I've been playing around with arrays of generic classes with different types. It's easiest to explain my problem with some sample code: // Obviously a very pointless protocol... protocol MyProtocol { var value: Self { get } } extension Int : MyProtocol { var value: Int { return self } } extension Double: MyProtocol { var value: Double { return self } } class Container<T: MyProtocol> { var values: [T] init(_ values: T...) { self.values = values } func myMethod() -> [T] { return values } } Now if I try to create an array of containers like so: var containers: [Container<MyProtocol>] = [] I get

Implementing a function with a default parameter defined in a protocol

♀尐吖头ヾ 提交于 2019-11-28 12:10:35
Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback" : It's executed when a type conforms to the protocol but doesn't provide its own implementation. At least that's how I read The Swift Programming Language guide: If a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension. Now I ran into a situation where my

Make property of type and also conform to protocol in Swift

纵饮孤独 提交于 2019-11-28 10:00:28
I would like to make a property that is of a certain type and also conforms to a protocol, which I would have done in Objective-C like this: @property (nonatomic) UIViewController<CustomProtocol> *controller; What I am looking for is to specify that the property can be set with an object of type UIViewController that also conforms to CustomProtocol, so that it's clear what the base class is. I know I could probably just use a short class stub to get the same results, i.e. class CustomViewController : UIViewController, CustomProtocol {} But this doesn't seem like the cleanest way to do it. I

A Swift protocol requirement that can only be satisfied by using a final class

纵然是瞬间 提交于 2019-11-28 09:07:23
I'm modeling a owner/ownee scheme on Swift: class Owner<T: Ownee> { // ... } protocol Ownee { var owner: Owner<Self> { get } } Then I have a pair of classes professor/student that adhere to the modeled types above: class Professor: Owner<Student> { // ... } class Student: Ownee { let professor: Professor var owner: Owner<Student> { // error here (see below) return professor } init(professor: Professor) { self.professor = professor } } However I get the following error on the definition of var owner in the Student class: Protocol 'Ownee' requirement 'owner' cannot be satisfied by a non-final

Function that takes a protocol and a conforming class (!) instance as parameters

限于喜欢 提交于 2019-11-28 00:56:49
问题 I am trying to figure out how to define a function which takes the following two parameters: A protocol. An instance of a class (a reference type ) conforming to that protocol. For example, given protocol P { } class C : P { } // Class, conforming to P class D { } // Class, not conforming to P struct E: P { } // Struct, conforming to P this should compile: register(P.self, obj: C()) // (1) but these should not compile: register(P.self, obj: D()) // (2) D does not conform to P register(P.self,