Google News
logo
IOS - Interview Questions
What do you mean by property in iOS?
Properties are basically values that are associated with a class, struct, or enum.  They can be thought of as "sub-variables," i.e., parts of another object. 
 
Example :  
struct Icecream  
{ 
    var flavor: String = ""
} 
var choco = Icecream() 
choco.flavor = "Chocolate Icecream" 
 
In the above code, we created a structure called Icecream. One of its properties is called flavor, a String whose initial value is empty. 
 
Classification of Properties 
 
Stored Properties : This type of property can be used to store constant or variable values as instances and is usually provided by classes and structures.

Example :
class Programmer {
    var progName: String
    let progLanguage: String
    var totalExperience = 0
    var secondLanguage: String?
}
Above, the Programmer class defines four stored properties : progName, progLanguage, totalExperience, and secondLanguage. These are all stored properties since they can contain values that are part of the instance of the class. The above example shows properties with and without default values, as well as an optional one.
 
Computed properties : These properties can be used to calculate values instead of storing them and are usually provided by classes, enumerations, and structures.

Example :
struct Angle {
    var degrees: Double = 0

    var rads: Double {
        get {
            return degrees * .pi / 180
        }
        set(newRads) {
            degrees = newRads * 180 / .pi
        }
    }
}
As mentioned above, the angle structure has a stored property called degrees for storing angles in degrees. Moreover, angles can also be expressed in radians, so the Angle structure contains a Rads property, which is a computed property.
Advertisement