Transfer from http://www.cnblogs.com/gcb999/p/3775053.html
Bo Master language has been bad (if there are any typos, please comment) hope you understand, have not learned anything
Today encountered a very annoying thing is that 10 o'clock in the morning to open the computer, has been into the system (my system Mac OS X Yosemite 10.10 System), has been stuck in the landing interface, I think the new Apple system is so fragile, and now a lot of software is incompatible even if, do I still can't enter the system, It's depressing. Then Google search, Google also can not open (really sad, said the "organization" to meet, we do not use Google). Then use "degree niang" search, searched for half a day, finally found the solution side, because "installed Sogou Input Method". Living in the country to be a programmer are you saying it's easy? Good software does not let, chaos software pits you dead. If you encounter such a problem workaround refer to this link (http://jingyan.baidu.com/article/0964eca23388cf8285f5368b.html)
The basic types of swift languages that we introduce today
A: Introduction to the basic types of Swift languages
Integer Int float type float Double character type String collection type arrary Dictionary Note: 1: basic Type the first letter is capitalized. 2: Primitive types cannot directly define type variables (this differs from other languages)
Wrong wording
main.swift// Basic type//import foundationint A = 10//Error base type cannot directly define A variable
Second, variable
Description: The keyword defining the variable is var ; Format: var variable name = Variable Value interpretation: The compiler infers the type of the variable name through the variable value .
Example:
main.swift// Basic type//import foundation/* var is the variable keyword A is the variable name 10 is the variable value compiler is the type of the variable A that is inferred by the value of the variable 10来. That is, the type of a is shaped */var a = 10var b = "Swift"//compiler infers the type of variable B by the string "swift" i.e. B is the string type var c = True //compiler through Boolean value True to infer that the type of the variable C is C is the Boolean type//the contents of the variable value are printed separately println (a) println (b) println (c)
Run results
10SWIFTtrue
Make a mistake.
main.swift// Basic type//import Foundationvar a = 10// the correct equals sign with a space on both sides var b=10//the compiler will error because there are no spaces on either side of the equals sign.
Three: the use of println
Extend the usage of a little println 1:println () function directly output a string 2:println () function directly output a variable or constant 3:println () function
Example
1:PRINTLN () function Direct output string
main.swift// Basic type//import foundation//println function Direct Output string println ("Swift")
Run results
SWIFT
2:pirntln () function directly outputs a variable or constant
main.swift// Basic type//import foundation//println function direct output variable var a = 10println (a)//println direct output shaping variable var b = TRUEPRINTLN (b)//Direct output of a Boolean type variable
Run results
10true
The 3:pirntln () function can directly output strings and variables or constants
Where the output of the variable representation \ (variable name) \ () variable placeholder, the parentheses inside the variable
println function Direct output variable or constant string var c = 10println ("c=\ (c)")//\ () is the placeholder c is the variable name var d = trueprintln ("d=\ (d)")//\ () is a placeholder D is the variable name
Run results
C=10d=true
Four: Constants
Description: The keyword defining a constant is let ; Format: let variable name = Variable Value interpretation: The compiler infers the type of "constant name" by "Constant value" .
Example
main.swift// Basic type//import foundation/* let is a constant keyword A is a constant name 10 is a constant value the compiler infers the type of constant A by constant value 10来 c17/> is a is shaping */let a = 10let B = true//The compiler infers the type of B by Boolean true println (a)//println function direct output constant println (b)
Run results
10true
The difference between a constant and a variable:
1: Constants can only be assigned once and cannot be modified
2: A variable can be modified more than once, and the modified variable value must be of the same type.
Example:
Variables can be modified more than once, and the modified variable values must be of the same type.
main.swift// Basic type//import Foundationvar a = ten //compiler infers the type of variable a by variable value 10 to reshape a = 20//re-assign a value to the shaping variable 20pri Ntln ("a=\ (A)")//println function output string and variable
Output results
A=20
Wrong wording
main.swift// Basic type//import foundation//error usage var b = Ten //compiler infers the type of variable a by variable value 10 to reshape B = "Swift"//b for Shaping , there is a mismatch between the B-assignment string "Swift" type, and the value must be assigned to B
Constants can only be assigned one time and cannot be modified
main.swift// Basic type//import foundationlet V = 10println ("v=\ (v)")
Operation Result:
v=10
Wrong wording
main.swift// Basic type//import foundation//error notation let v = 10V = 11//error V is a constant can only be assigned once.
I have come back to learn from the Swift language of the knowledge to write to form a series. As a new language, personal understanding inevitably has shortcomings, welcome to give me advice. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply
Finally, a summary. Send the mind map as the end of the article
Introduction to Swift-Basic type (1)