如何从swift中的另一个类调用另一个方法?

时间:2014-10-29 01:00:41

标签: ios objective-c class swift

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

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

1 个答案:

答案 0 :(得分:18)

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()