How do I make a private function in Swift?

ios8, swift

Solution

At the moment there are no visibility modifiers in swift.

On the developers forum, the language authors said it's on the top of their priority list.

Quoting greg parker from here:

We don't usually promise anything for the future, but in this case we are making an exception. Swift will have access control mechanisms.

In the same forum they suggest you can use nested classes, in this fashion, but this is really only for preventing the code-completion to catch the inner methods. They're not really private in the sense that anyone can instantiate the nested class and access those methods.

import Foundation

class KSPoint {

    class _KSPointInner {
        class func distance(point p1 : KSPoint, toPoint p2 : KSPoint) -> Double {
            return sqrt(pow(Double(p2.x - p1.x), 2) + pow(Double(p2.y - p1.y), 2))
        }
    }

    var x : Int

    func distance(point : KSPoint, toPoint : KSPoint) -> Double {
        return _KSPointInner.distance(point: point, toPoint: toPoint)
    }
}

Problem

How do I make a private function in Swift? Here is an example: ``` import UIKit class AnotherClass { var someVar = 1 let someConst = 2 func somePrivateFunc() -> Bool { return true } func someFunc() -> (Int -> Bool) { var someInnerFuncVar = { (num: Int)->Bool in return true } return someInnerFuncVar } init() { var vc = ViewController() } } ``` But if this is the only way to do it....

Original source

Related problems