How to convert AnyClass to a specific Class and init it dynamically in Swift?

Swift

Swift Problem Overview


In Object-C I store Class objects in an array and init them dynamically like this:

self.controllers=@[[OneViewController class],[TwoViewController class]];
Class clz = self.controllers[index];
UIViewController *detailViewController = [[clz alloc] init];

In Swift i try this way but it raises an error:

var controllers:AnyClass[] = [OneViewController.self,TwoViewController.self]
var clz : AnyClass = controllers[index]
var instance = clz() // Error:'AnyObject' is not constructible with ()

I wonder Whether there is a way to convert AnyClass to a specific Class? Or any other good ideas?

Swift Solutions


Solution 1 - Swift

You can specify the array to be of the common superclass' type, then type deduction does the right thing (Beta-3 syntax):

let classArray: [UIViewController.Type] = [
    OneViewController.self, TwoViewController.self
]
let controllerClass = classArray[index]
let controller = controllerClass.init()

Solution 2 - Swift

i have found a way to solve this problem:

var controllers:AnyClass[] = [OneViewController.self,TwoViewController.self]
var clz: NSObject.Type = controllers[0] as NSObject.Type
var con = clz()

remember to add @objc in the class of ViewController

Solution 3 - Swift

An AnyClass variable must first be casted into a specific type in order to initialize an instance:

// Code checked to run on xCode 7.1.1
import UIKit

var aClass: AnyClass = UIButton.self

// Error: 'init' is a member of the type...
// let doesNotWork = aClass.init()

// aClass must be casted first
var buttonClass = aClass as! UIButton.Type
var oneButton = buttonClass!.init()
var otherButton = buttonClass!.init(frame: CGRectZero)

Solution 4 - Swift

Here is a pure swift implementation of dynamic class types. It does require the classes to extend the same protocol.

protocol ILayout{ init(_ a:String)}   
class A:ILayout{required init(_ a:String)}  
class B:ILayout{required init(_ a:String)}   
var instance:ILayout   
var classType:ILayout.Type

classType = A.self   
instance = classType.init("abc")

classType = B.self   
instance = classType.init("abc")

Solution 5 - Swift

If the classes are all Objective-C NSObject subclasses, you can do something like this (yes, the backticks are intentional):

var constructors: (Void -> NSObject!)[] = [ NSMutableString.`new`, NSMutableArray.`new`, NSMutableDictionary.`new` ]

let string = constructors[0]()
(string as NSMutableString).appendString("Hello")

let array = constructors[1]()
(array as NSMutableArray).addObject("Swift")

let dictionary = constructors[2]()
(dictionary as NSMutableDictionary).setObject("Objective-C", forKey: "Goodbye")

NSLog("dynamically created a string: \(string), an array: \(array), and a dictionary: \(dictionary)")

It should output:

dynamically created a string: Hello, an array: (
    Swift
), and a dictionary: {
    Goodbye = "Objective-C";
}

It does seem to me that there should be a more elegant way to do this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondrinkingView Question on Stackoverflow
Solution 1 - SwiftDarkDustView Answer on Stackoverflow
Solution 2 - SwiftXianngView Answer on Stackoverflow
Solution 3 - SwiftMaic López SáenzView Answer on Stackoverflow
Solution 4 - SwiftSentry.coView Answer on Stackoverflow
Solution 5 - SwiftmarcpruxView Answer on Stackoverflow