How to call another method from another class in swift?

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

Here's how I do it in Objective C. How to do it fast?

+6
source share
1 answer

otherClass (). methodFromOtherClass () is the syntax for quick

If you need to pass a property with an external name, it could be:

otherClass (). methodFromOtherClass (propertyName: stringName) // example passing a string

To call a method for something if it returns the result of the method:

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

Example of accessing a property or function

 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" 

Excerpt from: Apple Inc. "Fast programming language." interactive books.

Here you just call the method, but not a new instance of the class.

 class SomeClass { class func someTypeMethod() { // type method implementation goes here } } SomeClass.someTypeMethod() 
+18
source

Source: https://habr.com/ru/post/977386/


All Articles