Kashif
Kashif

Reputation: 4632

Swift: How to create instance of a class, if I have the name of the instance in a variable?

I have team, teamScore, userScore information on parse. I download it and now want to store it locally and update it. I will create Team() Class, have the scores as properties and then creating instances of the class with team names downloaded from parse. Problem is how to create instance of this class with names from the parse downloaded team names?

OR is there a simpler way to solve this problem without classes?

Upvotes: 0

Views: 215

Answers (1)

Greg
Greg

Reputation: 25459

You can try something like that:

var className = "TeamClassName"
var c = NSClassFromString(className) as? TeamScoreSuperclas.Type
if let cl = c {
    println("ok")
}
else {
    println("false")
}

You should have a superclass for your team, score, etc classes. In the example above you pass team class name as a parameter but if you change it to score it will be fine until the superclass is the same.

Upvotes: 1

Related Questions