Swift Learning Notes (variables, arrays, and dictionaries)

Source: Internet
Author: User

variables(1) Let declares a constant, Var declares a variable, and a constant can only be assigned once. (2) Constants and variables are assigned at the same time as the declaration, and the compiler automatically infers the type. (3) without assigning a value or specifying a type at the time of Declaration, compilation can be an error, i.e. a variable of type uncertainty cannot be declared. (4) Swift's variable is strongly typed, and after the type of the variable is determined, it is not possible to assign other types of data to the variable. (5) When the variable is declared (which shows how the specified type is declared), add one after the type. To mark the value of this variable is optional and the default value is nil. (6) A variable uses a compilation error before it is initialized, except that a default value (7) cannot assign nil directly to a variable, except that a variable that is marked as a default value (8) can assign a value missing variable to any variable, and if the value missing variable is a nil assignment statement returns FALSE, the value returns TRUE. Can be used as a conditional judgment. var temp = 2                         //  compiler automatically infers type mode sound Variable TEMP = 1.0                           //Compile error (4) var temp:double = 2            //  Displays the specified type way declaring variables and assigning var test                                 //Compilation error (3)  var test:string                       //  display the specified type way declare variable println (test) &nbsp ;                       &NBSp; //Compilation error (6) test = nil                           & nbsp    //compilation error (7)  var test:string?                     //Specify variable value missing if var name = Test {  &nbs P println ("Assignment succeeded")
}
else {
println ("Default value Assignment Failed")}   conversion between value typesA transform must be displayed between Swift values, and you can use \ () to convert a value to a string in "" Let label = "the width is" allow width = 98let height = 100let str1 = label + string (width ) Let str2 = "the height = \ (height)" Arrays and dictionariesUsing [] to create arrays and dictionaries, use subscripts or keys to access the element var shoplist = ["Catfish", "water", "tulips"]shoplist[2] = "Bottle for water" var dict = [
"Malcolm": "Captain",
"Kaylee": "Mechanic"
]dict["Jayne"] = "public" array and inside the dictionary elements are modifiable, without providing a way to delete and add elements directly. Arrays support "+", "+ =" operations, and "-" operations are not supported. The dictionary does not support the "+", "-" operations, but adds the non-key directly to the dictionary. Create an empty array or dictionary let Emptyarray = string[] () Let emptydictionary = Dictionary<string,float> () The array and dictionary of Let declarations cannot be modified, Also cannot manipulate internal elements

Swift Learning Notes (variables, arrays, and dictionaries)

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.