Swift provides good support for object-oriented, and here are a few of its unique features.

Source: Internet
Author: User
Tags access properties

Swift provides good support for object-oriented, and here are a few of its unique features.

Lazy Load Properties

Swift provides support for lazy-loading properties in classes using the language level lazy as a keyword:

class Renderer {    lazy var loader = Loader() var data = [String]() var render() { // Do something... }}let renderer = Renderer()renderer.data.append("## Hello")renderer.data.append("## Hello Again")// loader不会被创建renderer.loader.loadData()// loader现在才会被创建
Attribute evaluation and Property observer

Similar to those in C # and Python @property , Swift also supports calculations when assigning values to attributes or fetching values:

class Square {    var length = 0.0 var size: Double { // 必须显式声明类型 get { return length * length } set (newSize) { length = sqrt(newSize) } }}

You can also set the property to read-only by using the following method:

class Square {    var length = 0.0 var size: Double { return length * length } }}

In addition to set and get, Swift also provides finer-grained observations of the change in property values-property observers Willset and Didset:

class Person {    var age: Int = 0 { willSet(newAge) { println("About to set new age") } didSet { println("Did set new age") } }}

Willset and Didset are called each time a property is assigned, regardless of whether the new value is the same as the old value, but not when the property is assigned a value in the INIT function.

Use Subscript to access properties

Swift provides the subscript syntax, which can be used to access the class's properties, similar to those in Python.__getitem__

class Dict {    subscript(key: String) -> String { get { // 取值 } set(newValue) { // 赋值 }}
Convenience initializer

Swift supports two levels of initialization, one called designated and the other called convenience, as the name implies, an initializer for easy initialization.

class Person {    var name: String init(name: String) { self.name = name } convenience init() { self.init(name: "Unknown") }}

For example the above code, we can use the parameterless init to quickly initialize a person instance.

You might want to ask, why not use the default parameters in the original init, but add an initialization function alone? My personal understanding is that when the initialization parameters are many and the types are different, using default parameters makes the initialization function overly complex and poorly readable. Using Convenicence, you can decouple all these parameters into different functions, making the code more beautiful. For example, this feature is used extensively in Swiftjson:

init(url:String) {//...}public convenience init(nsurl:NSURL) {//...}public convenience init(data:NSData) {//...}

With these initialization functions, we can easily construct JSON objects using String,url,nsdata and so on.

The difference from a struct

Like modern high-level languages such as C #, Swift's struct type is where properties and methods can be defined, similar to classes:

struct A{     var a = 10; func sayHello() { println("hello") }}

The main differences between classes and structs are the following:

    • Structs are value types, classes are reference types, which is the biggest and most important difference.
    • struct has auto-generated member initialization function (Memberwise Initializer), class does not have
    • Class can use the Deinit function to do the inverse initialization, the struct can not
    • Class supports method inheritance and overloading, structs do not support

In short, structs are better suited to encapsulate some simple data, such as CGRect. If you need too much logic and action, use classes more appropriately.

Swift provides good support for object-oriented, and here are a few of its unique features.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.