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....
Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.
In Swift 4, an extension can reach across to a private declaration. This only applies to extensions that live in the same source file as the private declaration. In other words, an entity that is declared private is accessible from within any extensions for that type within the same source file.
In Swift, a function can exist inside the body of another function. This is called a nested function.
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)
}
}
Alternatively, you can use Nested Classes
like so:
// Public interface
class Bakery
{
// Private implementation
class Baker
{
func prepareToMakeBread(numberOfLoafs:Int)
{
println("Preparing to make \(numberOfLoafs) loafs of bread...")
}
func bakeBread()
{
println("Baking the bread...")
}
func packageBread()
{
println("Packaging the freshly baked bread...")
}
}
// Public method
func buyBread(numberOfLoafs:Int)
{
println("Customer requests \(numberOfLoafs) loafs of bread!")
let baker = Bakery.Baker()
baker.prepareToMakeBread(numberOfLoafs)
baker.bakeBread()
baker.packageBread()
}
}
let bakery = Bakery()
bakery.buyBread(4)
Output:
Customer requests 4 loafs of bread!
Preparing to make 4 loafs of bread...
Baking the bread...
Packaging the freshly baked bread...
However, technically they are not private as someone can instantiate a Bakery.Baker
and call the methods. Functions inside functions do work but are ugly...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With