Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call another method from another class in swift?

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

This is how I will do it in Objective C. How do I do this in swift?

like image 469
zbz.lvlv Avatar asked Oct 29 '14 01:10

zbz.lvlv


1 Answers

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

If you have to pass a property with an external name it might be:

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

To call the method to do something, if it is returning a result of a method:

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

Example of accessing a property or a 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. “The Swift Programming Language.” iBooks.

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()
like image 138
Steve Rosenberg Avatar answered Oct 11 '22 15:10

Steve Rosenberg