Quickly move an object to type and protocol simultaneously

This question is about pre-Swift4 . In Swift 3 and the auricle, it was impossible to make the aforementioned cast directly, but only with the help of complex workarounds.

However, now the task is performed in the same way as the compilation of the protocol:

var myVar = otherVar as! (Type & Protocol)

No more extensions needed.


How can I pass a given object to a type and protocol to call some methods that are defined as an extension

Example:

extension Identifiable where Self: NSManagedObject, Self: JsonParseDescriptor {
    func someMethod() { }
}

Now I have an object that I extracted from Core data, and I would like to pass it to the above protocols in order to call someMethod on it. I could use for protocols with protocol<Identifiable, JsonParseDescriptor>, but how can I include the NSManagedObejct type in it as well?

thank

+4
2

, , . , Swift.

SR-1009 SR-1447 . .

while NSManagedObject , :

protocol _NSManagedObject {
    //the methods you want
}

extension NSManagedObject: _NSManagedObject {}

extension Identifiable where Self: _NSManagedObject, Self: JsonParseDescriptor {
    func someMethod() { }
}
+2

Swift 4, . , :

var myVar = otherVar as! (Type & Protocol)

.

0

Source: https://habr.com/ru/post/1651429/


All Articles