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

[swift]的简单使用常量变量。字典 数组的初始化

2016-09-08 21:02 701 查看
 //let 声明常量

        

      let label ="the width is"

      let width =94

      let widlabel  = label +String (width)

        /**

        *  the width is94

      

        */

        print(widlabel)

        //显式指定类型

        let exp :Double =70

        /**

        *  70.0

        */

        print(exp);

        

        let  apples =3

        let oranges =5

        

        let applesum ="i have
\(apples) apples"

        let fruitsum ="i have
\(oranges+apples)fruit"

        print(applesum)

        /**

        i have 3 apples

        */

        print(fruitsum)

        /**

        i have 8 fruit

        */

        let  apps =70.1

        let hello = "hello\(apps)"

        print(hello)

        

       
//使用 []来创建字典和数组,并使用键(key)值去访问数组

        //数组的创建

        var shoplist = ["catfish" ,"water","tulips"]

        

        shoplist[1] ="water is clear"

        

        

        print(shoplist)

         //["catfish", "water is clear", "tulips"]

        

        //字典的创建用:相隔

        var  occp = ["mack":"1","yew":"2"]

        occp["jayne"] ="public"

        //为字典添加新元素

        print("occp is\(occp)")

       // occp is ["yew": "2", "mack": "1", "jayne": "public"]

     

 var shops = ["catfish","water","tuips"]

        shops[1] =
"water is clear"

        //键
:值 
等号
左右俩边需要留空格

        var occp = [
"mac":"1","windows":"2"]

        occp["jsr"] =
"pulic"

        print(occp["jsr"])

        print("occp is
\(occp)")

        print("shops is\(shops)")

        

       
//要创建一个空数组
或者字典
,使用初始化语法

        let  emptyArr = [String]()

        //objectForKey(aKey: AnyObject) -> AnyObject?

        let dict3 : [String :
Int] = [:]

        print(dict3)

        print(emptyArr)

        //[:]

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