Generics call with Type T in Swift

后端 未结 7 1999
故里飘歌
故里飘歌 2021-02-12 17:20

In my application I want to create an generic method which creates an array of object depening on the given type T.

I created the following function:

fun         


        
7条回答
  •  执笔经年
    2021-02-12 18:07

    To answer the first part of your question

    You need not add an additional parameter to the method to specify the type of T as you already bat [T] as return type

    so you replace this line

    object.getArray("key")
    

    with this

    object.getArray("key") as [Document]
    

    This way you get an opportunity to specify the type to T.

提交回复
热议问题