所以可以说我有一堂课叫做数学
class Math{
func add(numberOne: Int, numberTwo: Int) -> Int{
var answer: Int = numberOne + numberTwo
return answer
}
在此类中,有一个函数允许用户添加两个数字。
我现在有另一个类,它是UIViewController的子类,我想使用Math类中的add函数,该怎么做?
class myViewController: UIViewController{
//Math.add()???
}
如果您想说,可以Math.add(...)
使用类方法-只需在class
前面添加func
:
class Math{
class func add(numberOne: Int, numberTwo: Int) -> Int{
var answer: Int = numberOne + numberTwo
return answer
}
}
然后,您可以从另一个Swift类中调用它,如下所示:
Math.add(40, numberTwo: 2)
要将其分配给变量i
:
let i = Math.add(40, numberTwo: 2) // -> 42
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句