Class Structure Body Properties

Source: Internet
Author: User

Keyword class to define a class using a struct to define the struct body

Class Person {

}

struct SomeStruct {

}

Create a

struct Otherstruct {

var name = "SLC"

var age = 24

}

Class Videmmodel {

var stru = otherstruct ()

var boo = False

var name:string?

}

The syntax for creating an instance of a struct and a class is very similar:

Both structs and classes can use the initializer syntax to generate new instances. The simplest initializer syntax is to follow an empty parenthesis, such as Resolution () or Videomode (), after the name of the class or struct. This creates an instance of a new class or struct, and any properties are initialized to their default values. In the initialization chapter there is a more detailed description of the initialization of classes and structs.

Let someresolution = Otherstruct ()

Let Somevideomode = Videmmodel ()

You can use DOT syntax to access the properties of an instance

Print ("Mingzi\ (someresolution.name)")

Print ("shuxing\ (Somevideomode.boo)")

Print ("shuxing\ (someVideoMode.stru.age)")

Use the DOT syntax to specify a new value into a variable property:

SomeVideoMode.stru.name = "S"

All structs have an automatically generated member initializer that you can use to initialize the member properties of the new struct instance. The initialization value of the new instance property can be passed to the member initializer through the property name:

Unlike structs, class instances do not receive the default member initializer

Let vgr = otherstruct (name: "SS", age:23)

Lazy is the lazy load in OC

The initial value of an attribute may depend on some external factors, and when the values of these external factors are not available until the initialization of the instance is complete, the deferred attribute will work. And when the initial value of a property requires a complex or expensive configuration to get, and you want to do it when you need it, the deferred attribute can be useful.

Class dateinterface{

var filename = "Name"

}

Class Datastring {

Lazy var interface:dateinterface = Dateinterface ()

var data = ""

}

Let da = datastring ()

Da.data.append ("Iloc")

Let file = da.interface.filename//At this time only the lazy decorated

/*

If you have objective-c development experience, you should know that there are two ways to store values and references in a class instance. Alternatively, you can use instance variables as backup storage for the values stored in the attribute.

Swift unifies these concepts into attribute declarations. The Swift property does not have an instance variable corresponding to it, and the backing store for the property cannot be accessed directly. This avoids confusion about the access to values in different environments and simplifies the declaration of attributes into a single, qualified statement. All information about the attribute-including its name, type, and memory management characteristics-is placed in the same place as the definition of the class.

*/

----------------------------------Computed Properties--------------------------------------------

In addition to storing properties, classes, structs, and enumerations can also define computed properties, which do not actually store values. Instead, they provide a reader and an optional set of settings to indirectly get and set other properties and values.

struct Point {

var x = 0.0,y = 0.0

}

struct Size {

var width = 0.0, height = 0.0

}

struct Rect {

var origin = point ()

var size = size ()

var Center:point {

get {

Let CenterX = origin.x + (size.width/2.0)

Let CenterY = Origin.y + (size.height/2.0)

Return Point (X:centerx,y:centery)

}

Set (Newcenter) {

origin.x = newcenter.x-(size.width/2.0)

ORIGIN.Y = Newcenter.y-(size.height/2.0)

}

}

}

var square = Rect (Origin:point (X:1, Y:2), Size:size (Width:2,height:3))

Let cen = point (X:2,y:3)

Square.center = cen

Print ("Square.origin is today at (\ (square.origin.x), \ (SQUARE.ORIGIN.Y))")

/*

The attribute observer observes and responds to changes in the property values. Whenever a property's value is set, the property watcher is called, even if the value is the same as the current value of the property.

You can add property observers for any of the storage properties you define, in addition to delaying storage properties. You can also add a property observer to any inherited property, whether it is a stored property or a computed property, by overriding the property in the subclass. The property overload is described in detail in the override.

*/

Class stepcounter{

var totalstep:int = 0{

Willset (newtotal) {

Print (Newtotal)

}

didset{

If OldValue > Totalstep {

Print (Oldvalue-totalstep)

}

}

}

}

Let Step:stepcounter = Stepcounter ()

Step.totalstep = 10

Step.totalstep = 5

A computed property with a reader but no setting is called a read-only computed property. A read-only computed property returns a value that can also be accessed through the point syntax, but cannot be modified to another value.

struct cooid{

var width = 0.0,height = 0.0,length = 0.0

var square:double{

Return width * Height * length

}

Suqare is read-only

}

Let cod = Cooid.init (width:3, Height:4, Length:5)

Print (Cod.square)

Class struct Body properties

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.