swift极简文档(2)数组与字典

 

        array初始化和拼接

        

        创建

        var shopNumList = [Int]()

        var shopNumList2:[Int] = []

        var shopNumList3:Array<Int> = []

        var shopNumList4 = Array<Int>()

        print("someInts is of type [Int] with \(shopNumList.count) items.")

        打印 "someInts is of type [Int] with 0 items."

        

        拼接

        shopNumList.append(3)

        someInts 现在包含一个 Int

        shopNumList = []

        someInts 现在是空数组,但是仍然是 [Int] 类型的。

        

        


        var threeDoubles = Array(repeating: 0.0, count: 3)

        threeDoubles 是一种 [Double] 数组,等价于 [0.0, 0.0, 0.0],repreting:初始值

        

        var anotherThreeDoubles = Array(repeating: 2.5, count: 3)

        anotherThreeDoubles 被推断为 [Double],等价于 [2.5, 2.5, 2.5]


        组合拼接

        var sixDoubles = threeDoubles + anotherThreeDoubles

        sixDoubles 被推断为 [Double],等价于 [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

        

        

        

        根据字面量直接创建

        var shoppingList: [String] = ["Eggs", "Milk","hehe"//等于 var shoppingList = ["Eggs", "Milk"]

        shoppingList 已经被构造并且拥有两个初始项。

        

        使用加法赋值运算符(+=)也可以直接在数组后面添加一个或多个拥有相同类型的数据项:

        

        shoppingList += ["Baking Powder"]

        shoppingList 现在有四项了

        shoppingList += ["Chocolate Spread", "Cheese", "Butter"]

        shoppingList 现在有6项了

        

        

        

        

        array访问和修改

        

        shoppingList = ["Eggs", "Milk"]

        

        print("The shopping list contains \(shoppingList.count) items.")

        输出 "The shopping list contains 2 items."(这个数组有2个项)

        

        

        使用布尔属性isEmpty作为一个缩写形式去检查count属性是否为0

        

        if shoppingList.isEmpty {

            print("The shopping list is empty.")

        } else {

            print("The shopping list is not empty.")

        }

        打印 "The shopping list is not empty."shoppinglist 不是空的)

        

        

        

        调用数组的insert(_:at:)方法来在某个具体索引值之前添加数据项:

        

        shoppingList.insert("Maple Syrup", at: 0)

        print("start array访问和修改")

        print(shoppingList)

        shoppingList 现在有3

        "Maple Syrup" 现在是这个列表中的第一项

        

        

        let mapleSyrup = shoppingList.remove(at: 0)

        print(shoppingList)

        print(mapleSyrup)

        索引值为0的数据项被移除

        shoppingList 现在只有2项,而且不包括 Maple Syrup

        mapleSyrup 常量的值等于被移除数据项的值 "Maple Syrup"

        

        let Milk = shoppingList.removeLast()

        print(shoppingList)

        print(Milk)

        print("end array访问和修改")


        数组的最后一项被移除了

        shoppingList 现在只有1项,不包括 Milk

        Milk 常量的值现在等于 "Milk" 字符串

        

        获取最小,最大值

        

        shoppingList.insert("hans", at: 0)

        shoppingList.insert("whaps", at: 0)

        shoppingList.insert("apple", at: 0)

        

        shoppingList.min() //1

        shoppingList.max()  //U

        

        

        包含

        shoppingList.contains("apple")

        shoppingList.contains("hehe")

        

        

        

        

        array遍历

        

        for item in shoppingList {

            print(item)

        }

        

        for (index, value) in shoppingList.enumerated() {

            print("Item \(String(index + 1)): \(value)")

        }

        

        

        

        

        

        

        

        dictionary使用

        

        var namesOfIntegers = [Int: String]()

        namesOfIntegers 是一个空的 [Int: String] 字典

        

        namesOfIntegers[16] = "sixteen"

        namesOfIntegers 现在包含一个键值对

        

        namesOfIntegers = [:]

        namesOfIntegers 又成为了一个 [Int: String] 类型的空字典

        

        根据字面量直接创建

        var airports1: [String: String] = ["airports1": "ch999", "airports2": "999ch"]

        

        var airports2 = ["airports1": "ch999", "airports2": "999ch"]

        

        

        访问和修改字典

        

        也可以用count

        print("The dictionary of airports contains \(airports1.count) items.")

        打印 "The dictionary of airports contains 2 items."(这个字典有两个数据项)


        

        使用布尔属性isEmpty作为一个缩写形式去检查count属性是否为0

        

        if airports1.isEmpty {

            print("The airports dictionary is empty.")

        } else {

            print("The airports dictionary is not empty.")

        }

        打印 "The airports dictionary is not empty."

        

        

        

        除了直接用下表,字典的updateValue(_:forKey:)方法可以设置或者更新特定键对应的值。updateValue(_:forKey:)方法在这个键不存在对应值的时候会设置新值或者在存在时更新已存在的值。和下标方法不同的是,updateValue(_:forKey:)这个方法返回更新值之前的原值。这样使得我们可以检查更新是否成功。

        

        updateValue(_:forKey:)方法会返回对应值的类型的可选值。举例来说:对于存储String值的字典,这个函数会返回一个String?或者可选 String”类型的值。

        

        如果有值存在于更新前,则这个可选值包含了旧值,否则它将会是nil

        

        if let oldValue = airports1.updateValue("hans", forKey: "airports1") {

            print("The old value for DUB was \(oldValue).")

        }

        输出 "The old value for DUB was Dublin."

        

        

        dictionary遍历

        

        for (airportCode, airportName) in airports1 {

            print("\(airportCode): \(airportName)")

        }

        

        for airportCode in airports1.keys {

            print("Airport code: \(airportCode)")

            print("Airport code: \(airports1[airportCode])")


        }

        

        

        for airportName in airports1.values {

            print("Airport name: \(airportName)")

        }

        

        如果我们只是需要使用某个字典的键集合或者值集合来作为某个接受Array实例的 API 的参数,可以直接使用keys或者values属性构造一个新数组:

        

        let airportCodes = [String](airports1.keys)

        airportCodes ["airports1", "airports2"]

        

        let airportNames = [String](airports1.values)

        airportNames ["hans", "999ch"]

        

        

        


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值