Hi everyone, to start I'm not entirely sure of my design, nevertheless here it is with my problem :
I'm currently developing an application with SpriteKit and the SpriteKit scene editor. To simplify the cumbersome var/optional writing of the elements of the scene I wrote this PropertyWrapper :
import SpriteKit
@propertyWrapper
@available(iOS 10.0, *)
public struct ChildNode<NodeType:SKNode>{
private var storage: NodeType?
private let childNodeName:String
public init(wrappedValue: @autoclosure @escaping () -> NodeType, withName name:String)
{
self.childNodeName = name
self.wrappedValue = wrappedValue()
}
public init(withName name:String)
{
self.childNodeName = name
}
public var wrappedValue:NodeType
{
get
{
guard storage != nil else {
fatalError("ChildNode property is nil")
}
return storage!;
}
set
{
storage = newValue
}
}
public mutating func setChild(from root:SKNode)
{
if let rawNode = root.childNode(withName: childNodeName)
{
if let node = rawNode as? NodeType
{
storage = node
}
else
{
fatalError("ChildNode \"\(childNodeName)\" of type \(type(of: rawNode)) can not be converted to \(NodeType.self) ")
}
}
else
{
fatalError("ChildNode \"\(childNodeName)\" can not be found ")
}
}
}
I use it like that :
@ChildNode(withName: "obj")
private var myObj: MyClass = MyClass()
And it works fine until here.
Now I want "myObj" to be of type "MyProtocol" the code becomes :
@ChildNode(withName: "obj")
private var myObj: MyProtocol = MyClass()
Where "MyProtocol" is :
import SpriteKit
public protocol MyProtocol:SKNode
{
var myProperty:String{get set}
}
Now when I try to access "myProperty" on "myObj" the compiler says :
Value of type 'SKNode' has no member 'myProperty'
How to get the compiler to understand that "myObj" is of type "MyProtocol" when it's already stated in the declaration?