Swift basics 1 (CODE)

Source: Internet
Author: User
Tags case statement
Import foundationprintln ("Hello, world! ") Var string1 =" Hello Beijing "// defines a variable (string) // var string1: String =" hello, beijing "// The system will automatically deduce the type for this expression println (" string1 = \ (string1) ") Let string2 =" hello, everybody "// Let defines a constant println (" string2 = \ (string2) ") string1 =" hello "Let number1 = 20 // Let number1: int = 20 // in swift, the integer is automatically deduced as Int. if the system is a 32-bit system, the integer is int32. if the system is a 64-bit system, the integer is int64let number2: int8 = 20 // If I need an 8-bit integer number, the int 8let Number3: Double = 12.0 // in swift, the floating point type is doublelet number4 by default: float = 28.0 // If the float type is required, the specified var number = double (number2) + Number3 // SWIFT must be displayed. implicit conversion is not supported, if you want to perform operations on two different types of data, you need to display the Conversion Type: type name (constant/variable name) let Hello = "hello" println ("Hello = \ (Hello)") // tuples derived from relational databases, each record in a relational database is equivalent to a single tuples, and the data in the tuples can be of different types. <PRE name = "code" class = "objc"> var request404error :( int, string) = (404, "request error message") // defines a tuple, which is equivalent to (INT, string) type of the tuples println ("requestcode = \ (request404error. 0), requesterrormessage = \ (request404error. 1) ") // you can use subscript to retrieve the elements in the tuples. var request404errora = (errorcode: 404, errormessage:" request error message ") // when creating a tuples, you can give the element type name println ("errorcode = \ (request404errora. errorcode), errormessage = \ (request404errora. errormessage) ") // array (array elements in Swift can be read from the array only if they are of the same type, that is, they can only store the same type of elements) var names: string [] = ["Taiyi", "jiaer", "A and", "a Wu"] var name1 = ["Meimei ", "photon Lang"] println ("names = \ (names)") names. append ("surana") // add elements to the array. You can use appendvar name = name1 + names // to add an array to the array, you can use "+" println ("name = \ (name)") println ("names = \ (names )") names [0] = "" // use a subscript to modify the element println ("names = \ (names)") names in the array. removeatindex (0) // Delete the element println ("names = \ (names)") names. removelast () println ("names = \ (names)") // The array declared by the let is an immutable array, you cannot add or delete elements, but you can replace let namesa = ["", ""] // namesa. removeatindex (0) namesa [0] = "a helper" println ("namesa = \ (namesa)") // var namesb = ["Lu Fei", "suo long ", "namesb", 32] // namesb. append ("xiangjishi "))

Dictionary
// The dictionary requires that all keys of the same type and all values of the same type be var person: dictionary <string, string> = ["name": "Lu Fei ", "Sex": "male"] println ("person = \ (person)") person ["habit"] = "soccer" println ("person = \ (person )") // person ["key"] = newvalue. If the key already exists, replace the key. If the key does not exist, then a group of key values are added: person ["name"] = "suo long" println ("person = \ (person)") person. updatevalue ("Nana", forkey: "name") // println ("person = \ (person)") Let oldvalue = person. updatevalue ("qiaoba", forkey: "name") // when updatevalue (value, forkey: Key) is used to update the elements in the dictionary, returns the replaced old value println ("person = \ (person) \ n oldvalue = \ (oldvalue)") Let persona = ["name": "Robin ", "Sex": "female"] // persona ["name"] = "luobin" // variable and immutable arrays and dictionaries are determined through VaR and let, the difference is that if the array is declared as let, elements cannot be added or deleted, but they can be changed. you cannot add or delete a dictionary declared by let, or change the traversal of the for name in names {// array, for in println ("name = \ (name) ")} // traverse the array and obtain the subscript for (index, name) in enumerate (names) {println (" Index = \ (INDEX ), name = \ (name) ")} // dictionary traversal to obtain the key and value (the items printed in the dictionary are unordered) for (Key, value) in person {println ("Key = \ (key), value = \ (value )")}

// Control statement var isnumber: bool = truevar anumber = 3; var bnumber = 5 // if you want to have multiple statements in a row, use "; "Split // in swift, the IF statement can only be of the bool type if isnumber {println (" anumber = \ (anumber) ")} for VAR I = 0; I <10; I ++ {println ("I = \ (I)")} For J in 0 .. 9 {//".. "indicates an interval, including the left value, but not the right value println (" J = \ (j) ")} for K in 0 .. 9 {//".. "includes both the left value and the right value println (" k = \ (k )")}
// Switchvar A = 11 Switch A {Case 1: println ("A = 1") case 11: println ("A = 11 ") fallthrough // force execute the next case statement default: println ("A = other number")} // The case statement can be a range and the range can be cross, but only execute the first statement that meets the case condition Switch A {Case 1 .. 20 where a = 12: // you can use where a = 12 println ("this is a single digit") case 11 .. 100: println ("this is a ten-digit number") Default: println ("other numbers")} var point = (4, 0) switch point {Case (0, 0 ): println ("Origin") Case (Let X, 0): // use (Let X, 0) to retrieve the element println ("X axis, X = \ (x) ") Case (0, _): //" _ "indicates ignoring the element println (" Y axis ") default in the corresponding position in the tuples: println ("in quadrant ")}



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.