Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the calculation properties in Swift

Shulou Source: shulou.com Published: 2022-06-02 04:35:26 09月15日 Update

This article mainly shows you "what are the computing attributes in Swift", the content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn this article "what are the computing attributes in Swift".

I. the concept of computational attributes

The calculation attribute itself does not store data, but calculates the data from other storage properties. Unlike storage properties, classes, structures, and enumerations can all define calculation properties.

The calculated property provides a getter to get the value, and an optional setter to set the value of other properties or variables indirectly. The syntax for the calculated property is as follows:

Object-oriented type name {

Storage properties.

Var calculation attribute name: attribute data type {

Get {

Attribute value after return calculation

}

New set attribute value {

}

}

Example:

Class Employee {var no:Int = 0 var firstName:String = "" var lastName:String = "" var job:String? Var salary:Double = 0 lazy var dept:Department = Department () var fullname:String {get {return firstName+ "." + "lastName"} set (newFullName) the default variable given by the system is newvalue {var name = newFullName.componentsSeperatedByString (".") FirstName = name [0] lastName = name [1]}}

Second, read-only calculation properties

Computed properties can have only getter accessors, but no setter accessors, which is read-only computed properties

Example:

Class Employee {var no:Int = 0 var firstName:String = "" var lastName:String = "" var job:String? Var salary:Double = 0 lazy var dept:Department = Department () var fullname:String {get {return firstName+ "." + "lastName"}

III. Computational properties in structures and enumerations

Example

Struct Department {let no:Int = 0 var name: String = "sales" var fullName:String {return "swift" + name + ".D"}} var dept = Department () println (dept.fullName) enum WeekDays:String {case Monday = "Mon" case Tuesday = "Tue" case Wednesday = "Wed" case Thursday = "Thu" case Friday = "Fri" var message:String {return "Today is" + self.rawValue}} var day = WeekDays.Mondayprintln (day.message) these are all the contents of the article "what are the computational properties in Swift?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Attributes storage content data examples articles types variables structures learning help different only objects that is easy to understand more organization concepts knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Microsoft Xiaomi MariaDB