ios - 如何快速调用另一个类的另一个方法?

标签 ios objective-c class swift

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

这就是我在 Objective C 中的做法。我如何在 swift 中做到这一点?

最佳答案

otherClass().methodFromOtherClass() 是 swift 的语法

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

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

调用方法做某事,如果它返回方法的结果:

让 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”。电子书。

在这里你只是调用方法而不是类的新实例。

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

SomeClass.someTypeMethod()

关于ios - 如何快速调用另一个类的另一个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26621148/

相关文章:

iOS12,kReachabilityChangedNotification 不起作用

iphone - 获取-(无效)viewDidAppear :(BOOL)animated to be called

Python 列表类 __contains__ 方法功能

iphone - 带有 "+"的电话号码与没有 "+"的电话号码不同吗?

ios - CLLocationManager 不回调委托(delegate)中的方法

ios - 我的 iOS 应用程序上的评价应用程序按钮

objective-c - 为什么我的 UINavigationController 右键加载后就消失了

ios - UIActivityIndi​​catorView 未居中

java - 对另一个类中的非静态方法进行静态引用

python - 根据类 python 2.7 中的变量按字母顺序对类列表进行排序