Bill3 Bill3 - 1 year ago 50
Swift Question

Return an array created from custom objects in Swift where they aren't known until runtime?

This function works fine except for the return

func fillAnObjectClassArrayGivenURL(URL: String, className: NSObject) -> [NSObject] {
var returnList = [className]
var objectToAddToList = className
//some code that appends objectToAddToList to returnList
return returnList

But this
isn't the right thing to return and I don't know what class it will be making the array from until runtime. Not that swift with Swift yet. Is there a way to return essentially

Answer Source

You can use generics.

func fillAnObjectClassArrayGivenURL<T: AnyObject>(urlString: String, className: T.Type) -> [T]