所以说我有一个叫Math的课程
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(…),你将需要使用一个类
方法 – 只需在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