字符与字符串

字符:

 

Swift中的字符串不是指针,而是实际的值

用Character定义一个字符

countElements(字符串)//计算字符个数

 

 

循环

//如果不需要知道区间内每一项的值,可以使用 _ 来代替变量名忽略对该值的访问

 1 for _ in 1...power
 2 
 3 {
 4 
 5     answer*=base
 6 
 7 }
 8 
 9  
10 
11  
12 
13 let numberOfLegs = ["spider":8,"ant":6,"cat":4]
14 
15  
16 
17 for(animalName ,legCount) in numberOfLegs
18 
19 {
20 
21     println("\(animalName):\(legCount)")
22 
23  
24 
25 }
26 
27  

 

//贯穿

let integer = 5

var desc = "The number \(integer) is"

switch integer {

case 2,3,5,7,11:

    desc+="  a prime number,and also"

    fallthrough     //贯穿关键字

default:

    desc+="  an interger"

}

println("desc is \(desc)")

 

 

 

//标签语句

var score = [86,77,87,-4,89,99]

First: for s in score

{

    switch s/10

    {

    case 10:

        continue

    case 9:

        println("\(s)分为优秀")

    case 8:

        println("\(s)分为良好")

    case 7:

        println("\(s)分为中等")

    case 0:

        break First

    default:

        println("\(s)分没有及格")

    }

}

 

posted @ 2014-07-17 15:12  zhanggui  阅读(178)  评论(0编辑  收藏  举报