Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are private properties used in the Swift language? [duplicate]

Tags:

swift

It seems that there is not a private, or for that matter public, keyword in swift language. So it is possible at all to have a private property?

On a side note, Swift is very much similar to Typescript per my observation so far.

like image 841
Sean Dong Avatar asked Jun 03 '14 17:06

Sean Dong


People also ask

What are properties in Swift language?

Swift - Properties. Swift 4 language provides properties for class, enumeration or structure to associate values. Properties can be further classified into Stored properties and Computed properties.

What is stored property in Swift?

Such properties are known as type properties. Property observers are also used Swift 4 introduces Stored Property concept to store the instances of constants and variables. Stored properties of constants are defined by the 'let' keyword and Stored properties of variables are defined by the 'var' keyword.

How to observe and respond to property values in Swift?

In Swift 4 to observe and respond to property values Property Observers are used. Each and every time when property values are set property observers are called. Except lazy stored properties we can add property observers to 'inherited' property by method 'overriding'.

How do you define a static variable in Swift?

In C and Objective-C, you define static constants and variables associated with a type as global static variables. In Swift, however, type properties are written as part of the type’s definition, within the type’s outer curly braces, and each type property is explicitly scoped to the type it supports.


Video Answer


1 Answers

Update:

As of Xcode 6 beta 4, Swift has Access Control - https://developer.apple.com/swift/blog/?id=5


Swift doesn't have access modifiers yet. But there are plans to add them in the future

There are also some workarounds mentioned in the above linked thread, like using nested classes:

import Foundation

class KSPoint {

    /*!
     * Inner class to hide the helper functions from codesense.
     */
    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

    var y : Int

    init(x : Int = 0, y : Int = 0) {
        self.x = x
        self.y = y
    }

    func distance(point : KSPoint, toPoint : KSPoint) -> Double {
        return _KSPointInner.distance(point: point, toPoint: toPoint)
    }
}
like image 114
manojlds Avatar answered Oct 23 '22 18:10

manojlds