Swift通过字符串调用类函数或属性 [英] Swift call class function or property by string
问题描述
class Foo {
public class func barFunction(){}
public class func barFunction2(){}
public class func barFunction3(){}
}
目前我有一个开关函数来决定调用哪一个bar方法
开关栏{
casebar:Foo.barFunction(param:Double)
casebar2:Foo.barFunction2(param:Double)
casebar3:Foo.barFunction3(param:Double)
默认值:return
}
如果我可以撰写,整件事情会容易得多bar函数的名称通过连接字符串并仅通过字符串名称调用方法。
我在Swift中读到过,闭包是要走的路,但我无法弄清楚闭包如何比开关操作更容易。 / p>
编辑:为了清楚起见,我想通过名称(字符串)调用类func / p> 编辑(2):我需要它这样工作,因为我有特定类型的db对象,或者在屏幕上绘制这些对象的特殊方法。当我从数据库中得到一个对象时,我将它的类型看作一个字符串,目前我正在用switch语句来确定它将如何绘制。 我觉得这是多余的,可以通过使用字符串来获取变量或函数来简化它。如果还有其他方法可以做到这一点,请告诉我。 / code>来调用选择器一次: 使用闭包和字典: I have this class At the moment i have a switch function to determine which bar method to call This whole thing would be a lot easier if i could compose the name of the bar function by concatenating strings and just call the method by string name. I've read that in Swift, closures are the way to go, but i can't really figure out how closures would make it easier than the switch operation. EDIT: Just to be clear, i want to call a class func with parameters by name (string) EDIT(2): I need it to work like this because i have db objects of certain types, and either special classes or a special methods to draw those objects on the screen. When i get an object from the database i get its type as a string, which currently i'm feeding in the switch statement to determine how it will be drawn.
Also, each object has different colours and i have this code right here to determine the color: I feel it's redundant and that it could be simplified a lot by using strings to get variables or functions. If there is any other way to do this, please tell me. You can use
Using closures and dictionary:
这篇关于Swift通过字符串调用类函数或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
另外,每个对象都有不同的颜色,我在此确定颜色代码:
public class func getColors(iconType:String) - > UIColor?{
switch iconType {
casetype1:return Assets.type1Color
casetype2:return Assets.type2Color
case type3:return Assets.type3Color
casetype4:return Assets.type4Color
...
default:return nil
}
}
pre $ NSTimer.scheduledTimerWithTimeInterval(0,target:self,selector:Selector( barFunction),userInfo:nil,重复:false)
另一个更优雅的解决方案
let options = [
bar1:{Foo.barFunction1()},
bar2:{Foo.barFunction2()},
bar3:{Foo.barFunction3 )}
]
//调用闭包的方法
options [bar2]!()
options [ bar1]?()
let key =bar3
如果let closure = options [key] {
closure()
}
class Foo{
public class func barFunction(){}
public class func barFunction2(){}
public class func barFunction3(){}
}
switch bar{
case "bar": Foo.barFunction(param:Double)
case "bar2": Foo.barFunction2(param:Double)
case "bar3": Foo.barFunction3(param:Double)
default: return
}
public class func getColors(iconType:String)->UIColor?{
switch iconType{
case "type1": return Assets.type1Color
case "type2": return Assets.type2Color
case "type3": return Assets.type3Color
case "type4": return Assets.type4Color
...
default: return nil
}
}
NSTimer
to call a selector once:NSTimer.scheduledTimerWithTimeInterval(0, target: self, selector: Selector("barFunction"), userInfo: nil, repeats: false)
Another more elegant solution
let options = [
"bar1": { Foo.barFunction1() },
"bar2": { Foo.barFunction2() },
"bar3": { Foo.barFunction3() }
]
// Ways of calling the closures
options["bar2"]!()
options["bar1"]?()
let key = "bar3"
if let closure = options[key] {
closure()
}