Bill3 Bill3 - 7 months ago 14
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
[NSObject]
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
[className]
?

Answer

You can use generics.

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