• swift(三)字典数组操作


    //1。数组的概念和定义
    
    var array = [2,3,4,5]
    var array1:Array = [2,3,4,5]
    var array2:Array<Int> = [2,3,4,5];
    
    var array3:[Int] = [2,3,4];
    var array4:[Double] = [2.2,3,4]
    var array5:[String] = ["a","b","c"]
    println(array)
    //数组可以方不同类型
    var array6:[Any] = ["a",1,2];
    
    //数组元素访问和在数组中插入删除
    var array7 = ["a","b","c"];
    array7.append("f")
    array7.insert("0", atIndex: 0)
    println(array7)
    
    
    //var array9 = [1,2,3,4,5]
    //var slice: Slice<Int> = [2,3,5]
    //slice = array9[0...3]
    //array9 = Array(slice)
    //slice = Slice(array9)
    //println(slice)
    
    //range
    var range: Range<Int> = Range(start: 0, end: 5)
    var range1 = 0...5
    var range2 = 0..<5
    println(range)
    println(range1)
    println(range2)
    
    //数组的批量操作
    var array10 = [1,2,3,4,5]
    //array10[0...1] = [9,9,9,9,9]
    array10.replaceRange(Range(start: 0, end: 2), with: [9,9,9,9,9])
    array10 += [9]
    array10.append(9)
    array10 += [10,10,10]
    //array10 += slice
    array10 += array10[0...1]
    println(array10)
    
    //数组的便利
    
    for elm in array10{
        println(elm)
    }
    
    for (index,value) in enumerate(array10)
    {
        println("index:(index) = (value)")
    }
    
    //字典的概念和定义
    
    
    var dic = [1:"1"]
    var dic2 :Dictionary<String,Int> = ["adf":1]
    var dic3 :Dictionary<String,String> = ["111":"111"]
    
    println(dic)
    
    //字典的操作
    var dict = ["apple":"苹果","banana":"香蕉"]
    dict["apple"] = "橘子"
    if let org = dict.updateValue("啦啦", forKey: "apple")
    {
        println(dict["apple"])
         println(org)
    }
    //println(dict["apple"])
    dict["green"] = "颜色"
    println(dict)
    
    dict.removeValueForKey("apple1")
    //dict.removeAll(keepCapacity: false)
    println(dict)
    //字典的便利
    
    for (key,value) in dict
    {
        println("(key) : (value)")
    }
    
    for key in dict.keys
    {
        println("(key)")
    }
    
    for key in dict.values
    {
        println("(key)")
    }
  • 相关阅读:
    toString的本质 以及String.valueOf()
    css3选择符
    HTML5标签
    css3-动画
    2D功能函数
    css过度
    css渐变
    BFC-块级格式化上下文
    表单补充
    表格补充:
  • 原文地址:https://www.cnblogs.com/keyan1102/p/4635265.html
Copyright © 2020-2023  润新知