您的位置:首页 > 移动开发 > Swift

[ios] Swift Basis

2015-08-07 09:51 477 查看
swift

//——————– string—————————

// 1. String()
// 2. "\()"
var age = 10
var hand = 2
var num = 12.444
var num2: Float = 12.0
var floatStr = String(stringInterpolationSegment: num2)
var c = "\(num)"
//var k = String()
var str = "i have " + String(hand) + " hands, \(age) years old" ;

println(str)


//——————-variable———————–

//specific the type

var num3:Int  = 10

let f:Float = 3.14

let f2:Float32 = 3.14

let money = 1

//modulo
8 % 2.5

//loop
// a...b =  a..< (b+1) = [a,b]
// 1...4 = [1,4]
for i in 1...4{
println(i)
}
// if no need i ,use "_"to skip and save time
for _ in 1...4{
println("***")
}

for j in 1..<5{
println(j)
}
let x = Int8.max
let y = Int8.min
let ux = UInt8.max
let uy = UInt8.min

UINT8_MAX
UINT16_MAX
UINT32_MAX


//——————-tuple元组——————————

//元组: N个任意类型的数据组成(N >= 0)

let position2 = (10.5,20,30,"string")
var position = (x : 10.5 , y : 20)
// 1. access
position.x
position.y
// 2. access
position.0
position.1
// 3. assign
// 4. error
//position.0 = "str"
position.0 = 12
position

// 1. specify the tuple type

var person : (Int,String) = (23,"tianhang")

// 在明确指定元素类型的情况下不能加上元素名称
//下面的语句是错误的:

//var p : (Int , String) = (age: 23 , name : "rose")

// 2 . use more than one variable to receive tuple data

var (i, j) = (10 , 20)

var point = (i , j)

// user "_" to skip a element
var (_ , b) = point


//———————switch———————

// switch , no need break
//but there should be a executable code in each case
let grade = "B"
switch grade{
case "A":
println("A")
case "B":
println("B")
//Mandatory for default
default:
println("C")
}

let score = 95
switch score/10 {
case 9:
println("*")
case 8, 7, 6:
println("*")
default:
println("*")
}
// use "..." scope
switch score{

case 90...100 :
println("A")
case 60...89:
println("B")
default:
println("D")
}

// match tuple by switch

// judge where is the point
let pt = (1,1)

switch pt {
case (0,0):
println("in origin");
case (_,0):
println("in x-axis")
case(2,_):
println("in y-axis")
case(-2...2,-2...2):
println("inside the rectangle")
default:
println("outside the rectangle")
}

// case , value binding, no need default
let pt2 = (0,0)
switch pt2{
case (let x ,0):
println("\(x)")
case (0 ,let y):
println("\(y)")
case let (x, y):
println("\(x),\(y)")
}
// add where
switch pt2{
case (let x ,0):
println("\(x)")
case (0 ,let y):
println("\(y)")
case let (x, y) where x == y :
println("\(x),\(y)")
case let (x, y) where x == -y :
println("\(x),\(y)")
default:
println("")
}
// fallthrough
//执行玩一个case,接着执行下一个case
switch pt2{
case (0 ,0):
println("")
fallthrough
case (0 ,1):
println("")
default:
println("")
}


//——————function——————————

// no return value
func printStr(){}
// no parameter return Int
func printStr2()->Int{return 1 }
// return more than one value , use tuple
func getValue()->(Int,Int){return (1,1)}
// use the return tiple clverly
func find(id:Int)->(name:String ,age:Int ){return ("william",25)}

var people = find(12)
// use the parameter name (name and age)
println("name=\(people.name),age=\(people.age)")


//调用find(12),我们一眼看不出来12是什么意思,所以下文就会用到外部参数明来提醒自己,某个参数是什么意思


//—————外部参数名——————————

// add external parameter name to make the code easily to read

func addStudent(name:String,stu_age age:Int,stu_no no:Int){}

// notice : if the functon has external parameter name , you must
// add the external parameter name when you call the function!

addStudent("william", stu_age: 23, stu_no: 12345)

// simplify the external parameter name
// add # , which means parameter name is also
// external parameter name

func addStudent2(name:String,#age:Int,#no:Int){}
addStudent2("william", age: 23, no: 123455)

//default parameter value , notice the automatical
// external parameter name ->"age"
func addStudent3(name:String,age:Int = 10){}
addStudent3("william", age: 30)
// use to skip the external parameter name
func addStudent4(name:String, _ age:Int = 10){}
addStudent4("william", 30)


//——————–常量参数和变量参数—————————

//常量参数: 参数在函数内不可改变,一般参数默认用let,所以不能改变

//变量参数: 用var改变默认的let就能改变了

//func changeNum(num:Int){ num = 4}  error
func changeNum(var num:Int){num = 4} // correct


//———————-输入/输出参数—————————

//在函数内部改变传进来的参数(与c语言指针原理相似)

func change(inout num : Int){ num = 10}
var a = 200
change(&a)
a
func swap(inout x :Int, inout y:Int){
let temp = x;
x = y;
y = temp ;
}
func swap2(inout x :Int, inout y:Int){
x = x*y
y = x/y
x = x/y
}
func swap3(inout x :Int, inout y:Int){
x = x+y
y = x-y
x = x-y
}
var para1 = 10;
var para2 = 20;

swap(¶1, ¶2)
swap2(¶1, ¶2)
swap3(¶1, ¶2)

para1
para2

2^1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  swift