Swift syntax quick Index

Source: Internet
Author: User

In the WWDC demonstration, we can see that swift, a language closer to the script, can use less code to complete the same functions as OC. However, for those who study war in war like me, every day they hold Big headers like swift programming language reference. After all, we still need to support our family. In addition, more than 1000 pages of content should not be used in normal work. Let's put all the things we used at ordinary times together, and forget to immediately open it and then become a habit without knowing it. This saves a lot of trouble.

Variable

1 // variable2 var int_variable = 1 // type inference 3 var message: string4 var x = 0.0, y = 0.0, Z = 0.0

 

Constant

// Constantlet const_int = 1//const_int = 10  ERROR: can not assign to let value

 

String

// String // 1. define var empty_string = "" Var another_empty_string = string () // 2. splicing var hello_string = "hello" Var world_string = "world" hello_string + = world_string // Hello world let multiplier = 3 // Let multiplier_message = "\ (mulitplier) times 2.5 is \ (double (multiplier) * 2.5) "// 3. compare VaR hello_world_string = "Hello World" hello_string = hello_world_string // All are "Hello World", result is trueif hello_string = hello_world_string {println ("these two are equal ")}

 

Tuple

 

// Tuple// 1. Unnamed tuplelet http_not_found = (404, "Not Found")println("tuple item 1 \(http_not_found.0), tuple item 2 \(http_not_found.1)")// 2. Named tuplelet (statusCode, statusMessage) = (404, "Not Found")statusCode      // 404statusMessage   // "Not Found"let http_not_found2 = (statusCode:404, statusMessage:"Not Found")http_not_found2.statusCode      // 404http_not_found2.statusMessage   // "Not Found"// 3. return tuplefunc getHttpStatus() -> (statusCode : Int, statusMessage : String){    // request http    return (404, "Not Found")}

 

 

Array

 

// Array // 1. Define // var empty_array = [] // The array is not defined in swift. This is nsarray type. It is generally array <t> var empty_array: [int] var empty_array2 = [int] () var fire_works = [String] () vaR colors = ["red", "yellow"] var fires: [String] = ["small fire", "big fire"]; // The array type in xcode6 beta3 is Var Red = colors [0] in square brackets. // 2. append & insertcolors. append ("black") colors + = "blue" Colors + = firescolors. insert ("no color", atindex: 0) // 3. updatecolors [2] = "light blue" // colors [5... 9] = ["pi NK "," orange "," gray "," Limon "] // 4. removecolors. removeatindex (5) // colors [0] = nil error! // Othercolors. isemptycolors. Count

 

 

Dictionary

 

 

// Dictionary // 1. define var airports: dictionary <string, string> = ["typ": "Tokyo", "Dub": "boublin"] var airports2 = ["typ": "Tokyo ", "Dub": "boublin"] var empty_dic = dictionary <string, string> () var empty_dic2 = [:] // 2. updateairports. updatevalue ("Dublin International", forkey: "Dub") airports ["Dub"] = "Dublin International" // 3. insertairports ["CHN"] = "China International" // 4. check existsif let airportname = airports ["Dub"] {println ("the name of the airport is \ (airportname ). ")} else {println (" that airport is not in the airports dictionary. ")} // 5. iteratefor (airportcode, airportname) in airports {println ("\ (airportcode): \ (airportname)")} // 6. removeairports. removevalueforkey ("typ") airports ["Dub"] = Nil

 

 

Enumeration

 

// Enum// 1. defination & usageenum PowerStatus: Int{    case On = 1    case Off = 2}enum PowerStatus2: Int{    case On = 1, Off, Unknown}var status = PowerStatus.Onenum Barcode {    case UPCA(Int, Int, Int)    case QRCode(String)}var product_barcode = Barcode.UPCA(8, 8679_5449, 9)product_barcode = .QRCode("ABCDEFGHIJKLMN")switch product_barcode{case .UPCA(let numberSystem, let identifier, let check):    println("UPC-A with value of \(numberSystem), \(identifier), \(check)")case .QRCode(let productCode):    println("QR code with value of \(productCode)")}

 

 

Method

 

 

// Function // 1. define func yourfuncname () {}// 2. return Value: func yourfuncnamewithreturntype ()-> string {return ""} // 3. parameter func funcwithparameter (parameter1: String, parameter2: string)-> string {return parameter1 + parameter2} funcwithparameter ("1", "2") // 4. external Parameter Name: func funcwithexternalparameter (externalparameter P1: string)-> string {return P1 + "" + p1} funcwithexternalparameter (externalparameter: "Hello World") func joinstring (string S1: string, tostring S2: String, withjoiner Joiner: string)-> string {return S1 + Joiner + S2} joinstring (string: "hello", tostring: "world", withjoiner: "&") // The external internal parameter func containscharacter (# string: String, # charactertofind: character) -> bool {for character in string {If character = charactertofind {return true} return false} containscharacter (string: "aardvark", charactertofind: "v ") // default value: func joinstringwithdefaultvalue (string S1: String, tostring S2: String, withjoiner Joiner: String = "") -> string {return S1 + Joiner + S2} joinstringwithdefaultvalue (string: "hello", tostring: "world ") // The default Joiner value is "" // inout parameter func swaptwoints (inout A: int, inout B: INT) {Let temporarya = A = B = temporarya} var someint = 3var anotherint = 107 swaptwoints (& someint, & anotherint) println ("someint is now \ (someint ), and anotherint is now \ (anotherint) ") // prints" someint is now 107, and anotherint is now 3

 

 

 

Class

 

// Class // 1. define class namedshape {var numberofsides: Int = 0 var name: String Init (Name: string) {self. name = Name} func simpledescription ()-> string {return "a shape with \ (numberofsides) sides. "}}// 2. inheritance & function overload & attribute getter setterclass square: namedshape {var sidelength: Double Init (sidelength: Double, name: string) {self. sidelength = sidelength super. init (Name: Name) numberofsides = 4} func area ()-> double {return sidelength * sidelength} // function overload override func simpledescription () -> string {return "a square with sides of length \ (sidelength ). "}} class equilateraltriangle: namedshape {var sidelength: Double = 0.0 Init (sidelength: Double, name: string) {self. sidelength = sidelength super. init (Name: Name) numberofsides = 3} // getter setter var perimeter of the property: double {get {return 3.0 * sidelength} set {sidelength = newvalue/3.0} override func simpledescription () -> string {return "an equilateral triagle with sides of length \ (sidelength ). "}}// 3. use VaR triangle = equilateraltriangle (sidelength: 3.1, name: "A triangle") triangle. perimetertriangle. perimeter = 9.9triangle.sidelength

 

 

Additional information

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.