Import Foundation
Let str = "123"
var message = "Tuqiushuang, str"
message+= "T"//character Append
If str.isempty| | 200>90
{
Print ("Hao")
}
If str==message&&100>90
{
Print ("Hao")
}
Else
{
Print ("hao==")
}
var a = 1
var B = 2
Let M = "\ (str) and \ (Double (a) *0.1)"//Insert of String
var d = a>b? "Excellent": "Liang"
var arr: [String] = ["Eggs", "milk", "DD"]//array
For item in arr{//traversal
Print (item)
}
For (index, value) in Enumeratesequence (arr)
{
Print ("Item \ (index+1): \ (value)")//Traversal
Tuple type subscript and value
}
Arr.append ("Jidan")
arr+=["Wo", "he"]
Print (Arr.count)
Arr.insert ("Insert", atindex:2)//insert at a location
Let AA = Arr.removeatindex (5)//The value of the deletion
Let Apple:int? = 123//Optional Type
var httpstatus= (s:200,d: "OK", m:90)//tuple tuple type, similar to dictionary
Httpstatus.m
/*
Dictionary < angle brackets are: Specify Type >
*/
var airporits:dictionary <string,string>= ["K0": "V0", "K1": "V1"]
airporits["K2"]= "v2"//Append
The number of airporits.count//pairs
If Let Removevalue=airporits.removevalueforkey ("K1")
{
Print (RemoveValue)
}else{
Print ("NO")
}
For i in 0.. < 10 {
If i%2==0{
continue;//Jump Statement
}
Print ("i = \ (i)")
}
function nesting
Func choosestepfunction (Backwards:bool)-(INT)->int{
Func Stepforward (input:int)->int{
Return input+1
}
Func Stepbackward (input:int)->int{
Return input-1
}
return backwards? Stepbackward:stepforward
}
var CurrentValue =-4
Let Movenearertozero = Choosestepfunction (currentvalue>0)
While CurrentValue! = 0 {
CurrentValue = Movenearertozero (CurrentValue)
Print (CurrentValue)
}
Closed Package
struct imagefile{//structure picture file
var fileName = String ()
var FileID = Int ()
}
var images:[imagefile] = []
var new = ImageFile (fileName: "hehe", fileid:20)
var old = ImageFile (fileName: "Heihei", fileid:30)
Images.sort ({$0.fileid>$1.fileid})
Images.sort ({image1,image2 in Image1.fileid > Image2.fileid})
Old.fileid
New.fileid
Calculated properties
struct point{
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = point ()
var size = size ()
var Center:point {
get {
Let CenterX = origin.x + (SIZE.WIDTH/2)
Let CenterY = Origin.y + (SIZE.HEIGHT/2)
Return point (X:centerx, Y:centery)
}
Set (Newcenter) {
origin.x = newcenter.x-(SIZE.WIDTH/2)
ORIGIN.Y = Newcenter.y-(SIZE.HEIGHT/2)
}
}
}
var square = Rect (Origin:point (x:0.0, y:0.0), Size:size (width:10.0, height:10.0))
Let Initialsquarecenter = Square.center
Square.center = Point (x:15.0, y:15.0)
Print ("Rectangle edge length: \ (square.origin.x), \ (SQUARE.ORIGIN.Y)")
Swift, next door, first grown