Write 12 person to review, but to complete the same code need to hit the keyboard is a lot less than OC, which is due to the Swift does not write a semicolon, and less oc in brackets.
I. CLASS and Structure
There's not much difference between the two in Swift.
There are many similarities between classes and structs : ( 2, 3 points are not present in other languages )
1) You can define properties /Methods /subscripts (structs can also define methods )
2) can be initialized (by means of construction )
3) can use extension (extension) and Protocol (protocol)
4) Global Properties/methods in classes and structs (enumerations ) : In classes with the class keyword , but in structs with static
Multi-function analogy structure :
1) can Inherit
2) The type of class object can be checked at runtime
3) destructor Release resources
4) Reference counting allows a class instance to have multiple references
[Java]View Plaincopy
- Class person1{
- var name = "Jack"
- class var height:int = 10//Error! A class cannot have a global storage property, only a computed property. See note below
- }
- struct person2{ //struct more like a class in Java
- var age:int
- static var Height:int = 10//can set global properties and need to be initialized.
- }
- 1.1 Instantiating classes and structs, default constructors
- Let P1 = Person1 ()
- Let P2 = Person2 (age: Ten) //when using the default constructor, all of the properties inside must be initialized
Second, attribute (store attribute --COMPUTE attribute --Class attribute )
2.1 Store Properties : is to store a constant or variable , similar to a member variable in Java
[Java]View Plaincopy
- Class person3{
- var name:string = "Xuneng" //Manual initialization required
- var age:int = Ten
- Let height = 175
- Lazy var p1:person1 = Person1 () //delay, must be var
- }
- var p3 = Person3 ()
- P3.name //access by DOT syntax
- P3.age = One //set
- Defer Lazy Store Properties: Initialize when using properties, the benefit of avoiding wasted space
- println (P3.P1) //This is called when P1 is initialized
2.2 Computed Properties : Do not store values directly , but instead use get/set . To manipulate changes in other property values
Get: Take , the process used to encapsulate a value
Set: Set , used to encapsulate the process of setting a value
[Java]View Plaincopy
- Class person4{
- var name:string = "Jack"
- var jjlength:int = Ten
- var age:int{ //can only be var
- get{
- return jjlength * 2
- }
- Set (NewAge) {
- Jjlength = newage/ 2
- }
- }
- }
- var P4 = Person4 ()
- P4.age = Ten //When set to age 10 o'clock, Jjlength becomes 5
2.2.1 Simple calculation of attribute notation
[Java]View Plaincopy
- Default parameter NewValue, which represents the newly assigned value
- Class person5{
- //With NewValue
- var jjlength:int = Ten
- var age:int{ //can only be var
- get{
- return jjlength * 2
- }
- set{
- Jjlength = newvalue/ 2
- }
- }
- //read-only computed properties
- var height:int{
- get{
- return jjlength * 4
- }
- }
- var height2:int{ //read-only direct can omit get
- return jjlength * 4
- }
- }
- var P5 = Person5 ()
- P5.height
- P5.height = 10//read-only cannot be assigned
2.2.2 Note the dead loop condition of the computed attribute
The value of the computed property is not fixed, so it cannot be decorated with a let and can only be used with var
Computed properties cannot be directly assigned to a value
[Java]View Plaincopy
- Class person6{
- //With NewValue
- var jjlength:int = Ten
- var height2:int = 10{//cannot be assigned directly, otherwise it is stored as a property.
- Return Jjlength * 4
- // }
- var age:int{
- get{
- return age //Dead loop. Age calls the Get method
- }
- set{
- Age = NewValue //Dead loop. The set method is called when the age value is set
- }
- }
- }
Class 2.3 Attributes (or type attributes ) are decorated with the Class keyword . Class properties can only be computed properties .
A class property is a property similar to the static decoration in Java . A class has only one copy , and multiple instance objects are shared . Can be called directly with the class name
[Java]View Plaincopy
- Class person7{
- class var name:string{
- return "Xuneng"
- }
- }
- Person7.name
2.4 Property Monitor: 2 methods that can be used to monitor property changes willset, Didset
Computed properties because of the get and set methods , the monitor is actually not very useful for calculating properties . Useful for storing properties
willset and Didset are not called when initializing a value
Willset, Didset and set, get cannot coexist
[Java]View Plaincopy
- Class person8{
- var name:string = "Xuneng" {
- willset{
- println ("new value: \ (newvalue)") //newvalue new value
- }
- didset{
- println ("new value is: \ (oldValue)") //oldvalue represents the old value
- }
- }
- }
- var P8 = Person8 ()
- P8.name = "Jack" //This sentence is called
Three, function and method
3.1 Object Methods
The difference between a method in a class and a function : After the first argument of the method , it is all an external parameter . Name is the parameter name
[Java]View Plaincopy
- Class person9{
- Func sum (num1:int, num2:int) {
- println (NUM1 + num2)
- }
- }
- var p9 = Person9 ()
- P9.sum (num2: Ten) //After the first parameter, it is all external parameters. The equivalent of adding #
The 3.2 class method. Modify with class
[Java]View Plaincopy
- Class person10{
- class func sum (Num1:int, num2:int) {
- println (NUM1 + num2)
- }
- }
- Person10.sum (Ten, num2: ten)
3.3 Self vs. super, same as OC
[Java]View Plaincopy
- Class person11{
- Func sum (num1:int, num2:int) {
- Self.fun () //Object method Call object method, class method call class method
- }
- Func Fun () {
- println ("Hello")
- }
- }
Four, subscript (secondary script)
Subscript, Some are called subordinate scripts (subscript), new things.
Can be defined in the class /struct /enumeration for quick access.
[Java]View Plaincopy
- Format: You can also set Set/get
- /*
- Subscript (index:int), int{
- get{
- }
- set{
- }
- }
- */
[Java]View Plaincopy
- struct person12{ // The official structure of many
- let jjLength:Int //struct here can not initialize
-
- subscript (index:int) -> int{
Li class= "alt" > return jjlength * index
- }
- }
-
- var p12 = person12 (Jjlength: 10"   
- p12[3] //accesses the . directly like an array, according to the JJ length, index is 3, and the value is 30
See Example code: http://github.com/xn4545945/SwiftLearning
Reference:
The Swift programming Language
Apple Dev Center
Reprint Please specify source: http://blog.csdn.net/xn4545945
Swift classes and structures, storage properties, computed properties, functions and methods, subordinate scripts, etc.