Swift's notes and tools class

Source: Internet
Author: User

Create Class (overload effect)

Create class MyClass {    //Properties member variable        init () {        ///Constructor constructor    }        //Method member methods 
   func doIt () {        println ("DoIt")    }        func doIt (), Int {        return 0    }        func doIt (a:int),  int {        return a    }        func doIt (A:int, b:int), int {        return a + b    }        func doIt (), String {        return "    }        func doIt (a:string)-String {        return a    }        func doIt (a:string, b:string ), String {        return a + b    }}//create/using an Instance Create/use an instance var a = MyClass () a.doit ("Wang", B: "Z Hipeng ")

Enums Enumeration

Enums enum enum Comcsofttype:int {case        Developmentengineer = 1 case        Testengineer = 2    }var myType = COMCSOFTT Ype. Developmentengineer

Declaring declaration of the Variables variable (optional variable)

declaring Variables variable declaration var mutabledouble:double = 1.0mutableDouble = 2.0let constantdouble:double = 1.0//constantdou ble = 2.0 error errors var autodouble = 1.0//Optional Value Optional variable (new mechanism) var optiondouble:double? At this moment optiondouble does not allocate memory at all, take address: &optiondouble is nulloptiondouble = 1.0//This time start optiondouble will start allocating memory if let Definedou ble = optiondouble {    println ("Allocated memory")}else {    println ("No Memory Allocated")}

Control Flow

Control flow controlled stream var condition = Trueif Condition {    println ("correct")}else {    println ("error")}var val = "Four" switch VA l {case    ' one ':        ' one ' case    ', ' three ': ' Three ', ' Default ':        ' default '}//omits Upper Value, use ... to include  omits the upper value, using ... Includes for I in 0..3 {    println ("i = \ (i)")}for var j = 0; j < 3; ++j {    println ("j = \ (j)")}//whilevar n = 2while n < {    n = n * 2}PRINTLN (n) var m = 2do {    m = m * 2} while M < 100println (m)

String Quick Examples Examples of strings

Examples of string Quick Examples strings var firstName = "Zhipeng" var lastName = "Wang" var hellostring = "Hello, \ (LastName) \ (first Name) "var tipstring =" 2499 "var tipint = tipstring.toint () extension Double {    init (string:string) {Self        = Double ( String.bridgetoobjectivec (). Doublevalue)    }}tipstring = "24.99" var tipdouble = Double (string:tipstring)

Array Quick Examples examples of arrays

Array Quick Examples example var person1 = "one" var Person2 = "One" var array:string[] = [Person1, Person2]array + = "three" For the in array {    println ("Person: \ (person)")}var Persontwo = Array[1]println ("Persontwo: \ (persontwo)")

Dictionary Quick Examples Dictionary example (generic effect)

Examples of Dictionary Quick Examples dictionary var dic:dictionary<string, string> = ["One": "1", "one":                                     "2",                                     "three": "3 "]dic[" three "] =" 4 "//Update threedic[" one "] = nil//Delete onefor (key, value) in dic {    println (" key: \ (key), value : \ (Value) ")}

function Functions

function Func getpirces () (double, double, double) {    return (1.1, 1.2, 1.3)//tuple}//remove tuple var (one, TW O, three) = Getpirces () println ("one = \ (one), both = \ (one), three = \ (three)") Func getsum (Numbers:int ...), Int {    var sum = 0 for number    in numbers {        sum + = number    }    return Sum}getsum () getsum (1, 3, 5, 7, 9)

function nesting

Function nesting func nestedfunction (), Int {    var x = 0    //    func Add () {        x + = 1    }    //    Add () 
   
    return x}
   

function passing

Function pass func makeincrementer ()--(int-int) {    func addone (number:int), int {        return number + 1    }< C3/>return Addone}var increment = Makeincrementer () increment (1) func makeIncrementer2 (), (String, String) String) {    func and (a:string, b:string), string {        return a + b    }    return And}var Increment2 = Makeincre Menter2 () Increment2 ("Wang", "Zhieng") func Lessthanone (number:int), Bool {    return number < 1}func MakeIncrementer3 (list:int[], condition:int-bool), BOOL {for number in    list {        if condition (number) {
   return true        }    }    return False}makeincrementer3 ([1,2,3,4,5], Lessthanone)

Extends Inheritance (polymorphic effect)

Extends Inheritance (polymorphic effect) println ("======================= Extends Inheritance") class people {//Eat eat func Eat () {println    ("Eat")} Sleep in Func sleep () {println ("Sleep")}//Work working func () {println ("work")}//said Said Func said () {println ("I am a person")}}class Chinese:people {//said said override Func said () {println ("I Am Chinese")}}    Class Americans:people {//said says override Func said () {println ("I am American")}}class Alien {//Eat Eat Func unknown () {println ("aliens")}}//does not support multiple inheritance//class Futuregenerations:chinese, Americans {//multiple Inheritanc    E from classes ' Chinese ' and ' Americans '////Said said//override Func said () {//println ("I am a descendant of Chinese and Americans")// }//}//Polymorphism Requirements: 1. Inheritance 2. Override 3. The parent class reference points to the subclass object var people = people () people.said () people = Chinese () people.said () people = Americans ( ) People.said ()//people = Alien () Error ' Alien ' is not convertible to ' people '//people.said () var Chinese = Chinese () ChiNese.said () var Americans = Americans () Americans.said () 

Swift's notes and tools class

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.