There are several parts to this question:
Type array generation
An array declaration expects an array of objects A , not A To create an array with types A, you can pass the expression Postfix self : ( link )
var array = [ A.self ]
This would define an array as an A.Type array, called a metatype type ( same reference ).
You can also create an empty array with this type of metatype:
var array:[A.Type] = []
If you need an array with A.self and B.self , you can specify it as [Any] ...
var array:[Any] = [A.self,B.self]
... or use the Initiatable protocol you created:
var array:[Initiatable.Type] = [A.self,B.self]
Moving an array to an array of types in your update method
You are having trouble converting any object to an array of types. Here is my updated update method:
func update(object: Any){ if let array = object as? [Initiatable.Type] { //1 process(array: array) } }
- Now you can optionally convert the array passed to your
update method. Drag it into the array of Initiatable metadata Initiatable : (This is the only line I changed from your method)
Getting type as a parameter in a process method
I assume that you just want your process method to get an array of types and instantiate a variable based on one of these types. You did not specify which element in the array, so I just went with the first one.
func process(array: [Initiatable.Type]){ //1 if let firstType = array.first { //2 let firstObject = firstType.init() //3 } }
- The
process method can get an array of types that accept the Initatable protocol. - I use an optional binding to get the value of the first element in the array. It must be a type.
- Create an object based on the first element of the array.
source share