swift菜鸟教程11-12(数组与字典)

一个朴实无华的目录

  • 今日学习内容:
    • 1.Swift 数组
      • 1.1创建数组
      • 1.2访问数组
      • 1.3修改数组
        • 使用 append() 方法或者赋值运算符 += 在数组末尾添加元素
        • 通过索引修改数组元素的值:
      • 1.4遍历数组 使用for-in循环
        • 同时需要每个数据项的值和索引值
      • 1.5合并数组
      • 1.6count 属性
      • 1.7isEmpty 属性
    • 2.Swift 字典
      • 2.1定义 :**存储无序的相同类型数据的集合**
      • 2.2创建字典
      • 2.3访问字典
      • 2.4修改字典:使用 updateValue(forKey:) 返回Optional值。
      • 2.5移除 Key-Value 对:removeValueForKey()
      • 2.6遍历字典
      • 2.7字典转换为数组
      • 2.8使用只读的 count 属性来计算字典有多少个键值对:

今日学习内容:

1.Swift 数组

1.1创建数组

以下实例创建了一个类型为 Int ,数量为 3,初始值为 0 的空数组:

var someInts = [Int](repeating: 0, count: 3)

以下实例创建了含有三个元素的数组:

var someInts:[Int] = [10, 20, 30]

1.2访问数组

import Cocoa

var someInts = [Int](repeating: 10, count: 3)

var someVar = someInts[0]

print( "第一个元素的值 \(someVar!)" )
print( "第二个元素的值 \(someInts[1]!)" )
print( "第三个元素的值 \(someInts[2]!)" )

1.3修改数组

使用 append() 方法或者赋值运算符 += 在数组末尾添加元素
import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

通过索引修改数组元素的值:
import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

// 修改最后一个元素
someInts[2] = 50

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

1.4遍历数组 使用for-in循环

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Runoob")
someStrs += ["Google"]

for item in someStrs {
   print(item)
}

以上程序执行输出结果为:

Apple
Amazon
Runoob
Google

同时需要每个数据项的值和索引值
import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Runoob")
someStrs += ["Google"]

for (index, item) in someStrs.enumerated() {
    print("在 index = \(index) 位置上的值为 \(item)")
}

以上程序执行输出结果为:

在 index = 0 位置上的值为 Apple
在 index = 1 位置上的值为 Amazon
在 index = 2 位置上的值为 Runoob
在 index = 3 位置上的值为 Google

1.5合并数组

import Cocoa

var intsA = [Int](repeating: 2, count:2)
var intsB = [Int](repeating: 1, count:3)

var intsC = intsA + intsB

for item in intsC {
    print(item)
}

以上程序执行输出结果为:

2
2
1
1
1

1.6count 属性

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)

var intsC = intsA + intsB

print("intsA 元素个数为 \(intsA.count)")
print("intsB 元素个数为 \(intsB.count)")
print("intsC 元素个数为 \(intsC.count)")

以上程序执行输出结果为:

intsA 元素个数为 2
intsB 元素个数为 3
intsC 元素个数为 5

1.7isEmpty 属性

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = [Int]()

print("intsA.isEmpty = \(intsA.isEmpty)")
print("intsB.isEmpty = \(intsB.isEmpty)")
print("intsC.isEmpty = \(intsC.isEmpty)")

以上程序执行输出结果为:
intsA.isEmpty = false
intsB.isEmpty = false
intsC.isEmpty = true

2.Swift 字典

2.1定义 :存储无序的相同类型数据的集合

Swift 字典每个值(value)都关联唯一的键(key),键作为字典中的这个值数据的标识符。

2.2创建字典

var someDict = [Int: String]()
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

2.3访问字典

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var someVar = someDict[1]

print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

2.4修改字典:使用 updateValue(forKey:) 返回Optional值。

如果 key 不存在,则添加值,如果存在则修改 key 对应的值。

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var oldVal = someDict[1]
someDict[1] = "One 新的值"
var someVar = someDict[1]

print( "key = 1 旧的值 \(oldVal)" )
print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

以上程序执行输出结果为:

key = 1 旧的值 Optional("One")
key = 1 的值为 Optional("One 新的值")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

2.5移除 Key-Value 对:removeValueForKey()

如果 key 存在该方法返回移除的值,如果不存在返回 nil 。

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var removedValue = someDict.removeValue(forKey: 2)

print( "key = 1 的值为 \(someDict[1])" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

也可以通过指定键的值为 nil 来移除 key-value(键-值)对

someDict[2] = nil

以上程序执行输出结果为:

key = 1 的值为 Optional("One")
key = 2 的值为 nil
key = 3 的值为 Optional("Three")

2.6遍历字典

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

for (key, value) in someDict {
   print("字典 key \(key) -  字典 value \(value)")
}

2.7字典转换为数组

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

let dictKeys = [Int](someDict.keys)
let dictValues = [String](someDict.values)

print("输出字典的键(key)")

for (key) in dictKeys {
    print("\(key)")
}

print("输出字典的值(value)")

for (value) in dictValues {
    print("\(value)")
}

2.8使用只读的 count 属性来计算字典有多少个键值对:

import Cocoa

var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2:[Int:String] = [4:"Four", 5:"Five"]

print("someDict1 含有 \(someDict1.count) 个键值对")
print("someDict2 含有 \(someDict2.count) 个键值对")

你可能感兴趣的:(swift,ssh,服务器)