Swift Basic Learning 01

Source: Internet
Author: User
Tags case statement

Related sites: http://www.cnblogs.com/tt_mc/p/3871295.html related auxiliary actions: You can see the output value directly on the right side of the FAQ: 1. General = Pay attention to the same spacing as 2. The program sometimes prompts for errors, and the workaround: Restart Xcode/*-----------1. Constants and Variables--------------*/
1. Modification of variables in swift with the modification of Var constants with Let
The difference between the 2.print and the println is that a line is not changed.
The 3.swift data type is written after the variable name in the format: var variable name: type
4. Equal sign with spaces on both sides there are no spaces (implicit type inference)
5 first letter capitalization of data type
var a = 10
var a1:int//specified type
A1 = 2
println (A1)

println (a)
println (a)

Let B = 8
println (b)

Other languages use ASCII encoding, and Swift uses Uncode encoding. The following naming methods do not recommend naming rules that still start with the letter/number/underscore
Var?? = 20
println (??)

-------------Data Type----------------

var f:float=10.6
var f1 = 10.6//default to double type

var w:int
The format of the cast type is: type (variable name that needs to be converted)
All languages, data is lost when converting from high precision to low precision in data conversion
W = Int (F1)

Strings string Manipulation
var str = "Sister is"
var str1 = "Xiaolong"
Stitching
var ss = str + STR1
println (SS)
String add parameter \ () representation placeholder in Swift
println ("Pretty \ (str) \ (STR1)")
Convert an integral type to a string
var sss = String (w)
println (SSS)

----------------Array----------------
Arrays in the C language must hold the same type of data type the array in OC must hold the object
Arrays in Swift can only hold the same data type (different versions)

var array = [[+]
var array1:array=[1, 2, 3, 4]
The data type in Var array2:array<int>=[1,2,3,4]//<>
var array3:[int]=[2,3,4]//this way is officially recommended

var arr:[string]=["String 1", "String 2"]

Methods for placing different types must be used <Any> decorated
var array4:array<any>=[1, "w2e", 3.3]

adding elements
Array4.append (4)
Array4.append (12)
inserting elements
Array4.insert ("Hello", atindex:1)
Array4.insert (3.5, Atindex:2)
Delete
Array4.removeatindex (1)
Array4.removeatindex (2)
println (ARRAY4)

Replace the value of 0-2 position
array4[0...2]=[6,7,8]
println (ARRAY4)

For value in Array4
{
println (value)
}
Print Index
for (Indext,value) in enumerate (ARRAY4)
{
println ("indext=\ (Indext), value=\ (value)")
}


-------------------tuple-----------
Very similar to the structural body

var person: (name:string,age:int,address:string) = ("Xiaolong", 30, "Guangzhou")//The essence is to define three variables name,age,address
println (person)
println (Person.age)
println (person.2)

Assign a tuple to another tuple
var (name,age,address) =person
var (names,ages,_) =person//omit the third unwanted element with an underscore
println (names)

----------------Dictionary---------------
All the KE values of a dictionary must be of the same type, and all value values must be of the same type as key and value may not be of the same type (different versions, where mandatory write data types must be the same)

Dictionary with []
var dic:dictionary = ["Ke1": "VA1", "Ke2": "Va2"]
println (DIC)
var dic1:dictionary<int,int> = [1:11,2:22]//Force Int type

Common methods
Add a key-value pair
When the dictionary does not have this key value pair to represent the Add, otherwise modify (overwrite)
dic["Ke3"]= "VA3"
dic["Ke1"]= "Va11"
println (DIC)
Delete
Dic.removevalueforkey ("Ke1")
println (DIC)
Get all the key values in the dictionary
println (Array (Dic.keys))
Get all the value values in the dictionary
println (Array (dic.values))

--------------------Optional Type---------
The optional data type is added after our base data type.
Optional () is an optional type
var aaw:int?
AAW = 6
When we want to output what's inside, we need to unpack it. Note: The value of the optional type variable before unpacking can not be null the need to assign a value to the empty forced unpacking causes the program to crash
println (AAW)
println (aaw!)

------------------Process Control Statements----------

There's no non 0 that's true for the statement only ture and False
var g = 1
The If statement () may not be written
If G==1
{
println ("yes")
}else
{
println ("no")
}
Switch
The 1switch statement must have the default statement
2switch statement There must be a code in the case
3 after each case statement comes with a break
var s = 2
Switch S
{
Case 1:
println ("1")
Case 2:
println ("2")
Case 3:
println ("3")
Default
println ("Default")

}
//
var tt = 2
Switch TT
{
Case 1:
println ("1")
Case 2, 3:
println ("2 or 3")
fallthrough//executes regardless of whether the next statement is correct
Case 4:
println ("4")
DEFAULT://must have this statement
println ("Default")
}

The case can be a range, and the range can also be crossed, and the crossover value only goes to the first situation
var s2=10
Switch s2
{
Case 1...15:
println ("1...15")
Case 5...15:
println ("5...15")
Default
println ("Default")
}
Double judgment
Switch s2
{
Case 1...15 where tt==3:
println ("S2 between 1 and 15 TT has a value of 3")
Default
println ("Default")
}

Switch can pass a tuple
var point= (0,4)
Switch point
{
Case (_,0):
println ("Above the x-axis")
Case (0,_):
println ("Above the y-axis")
Case (0,4):
println ("Just My point")
Default
println ("Default")
}


------------------Loop Structure----------

and C language is not much different is not add ()
for Var i=0;i<10;i++
{
for Var j=0;j<10;j++
{
println ("i=\ (i) j=\ (j)")
If i==3&&j==4
{
break;//terminating the inner layer loop
}
}
}
Use of the switch tag
aa:for var i=0;i<10;i++
{
for Var j=0;j<10;j++
{
If i==3&&j==4
{
println ("HD")
Break aa;//
}

}
}


---------------------function---------
Func (keyword of the function)
The format of the switch function definition func function name (parameter name:), return value type {statement block}

Func sayhi (name:string)->string
{
return name + "Hello"
}
var sa = Sayhi ("World")
println (SA)

Sum of two numbers
Func sum (summ1:int, Summ2:int)->int
{

return SUMM1 + summ2
}
var summ = SUM (10,10)
println (Summ)

Return value if multiple can be returned in tuples
Func CountNumber () (Saa:int,sbb:int)
{
var saa=1
var sbb=2
Return (SAA,SBB)
}
var s_n = CountNumber ()
println (S_N.SAA)


---------------------Closures----------
Closures are a closed block of code, similar to block
Syntax format ((parameter list) return value type in return value)

A normal function call
Array sorting
var mans = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu"]
Func sortbyname (s1:string,s2:string)->bool
{
Return S1&GT;S2
}var s3 = sorted (mans,sortbyname)//Select two elements from the array and pass them to sortbyname for comparison
println (S3)

Closure method
var s4=sorted (Mans, {(s1:string, s2:string), Bool in
Return S1&GT;S2
}) println (S4)

Swift Basic Learning 01

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.