Swift学习笔记-字典Dictionary

        //字典
        //声明
        //1.泛型语法声明
        var myDict : Dictionary<String,String>
        //2.使用简化语法声明
        var dictText : [String:Int]
        var dictText1 : [String:String]
        //赋值
        //1.创建一个字典结构体,使用默认的参数
        myDict = Dictionary<String, String>()
        println(myDict)//[:]
        dictText = Dictionary<String ,Int>(minimumCapacity: 5)
        println(dictText)
        //2.使用简化语法创建字典
        dictText1 = ["high":"20","fat":"110"]
        println(dictText1)//[fat: 110, high: 20]
        //创建不包含任何key-value对的字典
        var emptyDic : [String:Double] = [:]
        //isEmpty属性可判断数组,字典是否不包含任何元素
        println(emptyDic.isEmpty) //true

        var em : [String:String]
        em = Dictionary()
        println(em.isEmpty)

        //字典的使用
        var high = dictText1["high"]
        println(high)//Optional("20")
        //访问不存在的key返回nil
       var tro = dictText1["tro"]
        println("\(tro)")//nil
        //修改指定的key对应的value
        dictText1["fat"] = "200"
        println(dictText1)//[fat: 200, high: 20]
        //如果key不存在,则添加
        dictText1["pressure"] = "112"
        println(dictText1)//[fat: 200, high: 20, pressure: 112]
        //如果需要返回key对应的value需要加“!”强制解析
        var englis : String? = dictText1["high"]
        if englis != nil
        {
            println(englis!)
        }

var result = dictText1.updateValue("289", forKey: "fat")
        println("result is \(result)")//result is Optional("200")
        println(dictText1)//[fat: 289, high: 20, pressure: 112]

        /**
        for in遍历字典
        */
        var seasons = ["a":"测试","b":"summer","c":"autumn"]
        /**
        *  b:summer
        a:测试
        c:autumn
        */
        for (season , desc) in seasons
        {
            println("\(season):\(desc)")
        }

        /**
        单独使用字典的keys和values
        */
        var keys = Array(seasons.keys)
        var valuess  = Array(seasons.values)
        println("\(keys)....\(valuess)")//[b, a, c]....[summer, 测试, autumn]

        //删除
        seasons.removeValueForKey("d")
        /**
        [b: summer, a: 测试, c: autumn]
        */
        println(seasons)
        seasons.removeValueForKey("c")
        //[b: summer, a: 测试]
        println(seasons)
        //通过将value赋值为nil来删除
        seasons["b"] = nil
        println(seasons)//[a: 测试]

部分内容来自于<疯狂Swift讲义>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SwiftUI is a new framework introduced by Apple in 2019 for building user interfaces on all Apple platforms (iOS, iPadOS, macOS, watchOS, and tvOS). It provides a new way of declaratively building user interfaces using Swift code. Objective-C, on the other hand, is an object-oriented programming language that has been widely used for building Mac and iOS applications. It has been around for much longer than SwiftUI, and has a large and established developer community. Here are some key differences between the two: 1. Syntax: SwiftUI has a simpler and more intuitive syntax compared to Objective-C, which can be more verbose and difficult to read. 2. Declarative vs Imperative: SwiftUI is a declarative framework, which means you describe what your user interface should look like, and the framework takes care of rendering it on the screen. Objective-C, on the other hand, is an imperative language, which requires you to write code to manipulate the user interface elements directly. 3. Interoperability: SwiftUI and Objective-C can coexist in the same project, and it is possible to use SwiftUI components in Objective-C code. However, the reverse is not true, as Objective-C code cannot be used in SwiftUI. 4. Learning Curve: SwiftUI is generally considered easier to learn for new developers, whereas Objective-C has a steeper learning curve. Ultimately, the choice between SwiftUI and Objective-C depends on the specific needs and preferences of each developer and the project they are working on. Both frameworks have their strengths and weaknesses, and both can be used to build high-quality and performant applications.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值