ios – 如何在swift中从另一个类调用另一个方法?

mainController = (ViewController *)self.presentingViewController;   
mainController.pressureUnit = _pressureSettings.selectedSegmentIndex;

这就是我将如何在Objective C中完成它.我如何在swift中执行此操作?

最佳答案
otherClass().methodFromOtherClass()是swift的语法

如果您必须传递具有外部名称的属性,则可能是:

otherClass().methodFromOtherClass(propertyName:stringName)//传递字符串的示例

要调用方法执行某些操作,如果它返回方法的结果:

let returnValue = otherClass().methodFromOtherClass(propertyName:stringName)

访问属性或函数的示例

class CarFactory { 
   var name = ""
   var color = "" 
   var horsepower = 0 
   var automaticOption = false

   func description() { 
       println("My \(name) is \(color) and has \(horsepower) horsepowers") 
   }
} 

var myCar = CarFactory() 
myFirstCar.name = "Mustang"
myCar.color = "Red"
myCar.horsepower = 200 myCar.automaticOption = true
myFirstCar.description() // $: "My Mustang is Red and has 200 horsepowers and is Automatic"

摘录自:Apple Inc.“The Swift Programming Language.”iBooks.

在这里,您只需调用该方法,但不能调用该类的新实例.

class SomeClass {
    class func someTypeMethod() {
        // type method implementation goes here
    }
}

SomeClass.someTypeMethod()

转载注明原文:ios – 如何在swift中从另一个类调用另一个方法? - 代码日志