1

Let's suppose I've a class

class Test {

}

Then in another part of the program I could do

   let testClass = NSClassFromString( "Test" ) as NSObject.Type
    let testInstance = testClass()

Is there a way to do the same thing from reference of Test itself, pass Test as a reference and then create an object from the reference

By reference I mean pass the Test itself and receiving part of the code would be able to create object out of it.

just like we used to do in Objective-C

TestClassRef = [Test class]

[[TestClassRef alloc] init]

Edit:

protocol ISomeProtocol {
    func action ()
    func init() 
}

class Test
    var classesMap = Dictionary<String,ISomeProtocol>()

    func init () {
        self.classesMap = Dictionary<String,ISomeProtocol>()
    }

   func myAction (name:String ) {
        var myClass:ISomeProtocol;

        if let myClass = self.classesMap[ name ] {
            var myInstance = myClass() as ISomeProtocol
            myInstance.action()
        } else {
            return
        }

    }
}

1 Answer 1

0

Yes, but you need to make some promises about the class. For example, the equivalent of your objC code is this:

let testClass: AnyClass = Test.self
let test = testClass()

That won't compile because Swift has no way to know that AnyClass has an available init(). You need to promise that it can be trivially constructed:

protocol Trivial {
   init()
}

Which means that your class must conform to that:

final class Test: Trivial {
    required init() {}
}

And then you're free to pass around and use references to the class:

func make<T: Trivial>(type: T.Type) -> T {
  return type()
}

let myType = Test.self

let something = make(myType)

(I wrap this into a function called make because some simpler examples without a function will currently cause the compiler to crash. But most of the time you want to pass this to a function anyway.)

Some more discussion on this issue coming from other directions:

Sign up to request clarification or add additional context in comments.

10 Comments

why class Test is marked as final? is it necessary? can you make an instance without involving the function make?
let's say they all are stored inside a dictionary associated by a string (key), we retrieve by key and create an object out of it
See the linked discussions on why it is easiest if it's final. It doesn't have to be, but then it needs a "required" initializer. If you want to create objects based on strings, see the discussion here for my recommendations: stackoverflow.com/questions/25864108/…
As I noted, you can do this without an extra function, but in some cases it will crash the compiler today. If it's not crashing the compiler, then that's fine.
we are getting close, so assuming no crash, how would you rewrite (my edit) to make it work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.