Hi Everyone,
I am still pretty new to Swift and have been trying to implement a node graph, and have hit a wall.
I am trying to store a list of ports, that could be made up of different generic types. I have tried to implement it using Generics and protocols. Where I get stuck is how to I convert the anyObject or Any back into a Plug
Thanks for any assistance
Regards,
Simon
//———————————————————————————————
protocol genericPlug {
associatedtype dataType
var value:dataType? {set get}
}
class Plug<T>: genericPlug {
typealias dataType = T
var value:dataType?
var name:String
init (name:String, value:T){
[self.name](http://self.name) = name
self.value = value
}
}
var plugs: [AnyObject] = []
// I have also tried with [genericPlug]
var plug_1 = Plug(name:"input", value:0)
var plug_2 = Plug(name:"output", value:0)
plugs.append(plug_1)
plugs.append(plug_2)
var plug_3 = Plug(name:"input2", value:16.0)
plugs.append(plug_3)
var doublePlg = Plug(name:"a_plug", value:[2.1, 3.0])
var intListPlg = Plug(name:"array_plug", value:[2, 1, 34])
plugs.append(doublePlg)
plugs.append(intListPlg)
intListPlg.value
doublePlg.value
plugs[3].value[0] + plugs[2].value