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

swift 之 数组集合字典的简单使用

2016-06-26 11:04 507 查看
//: Playground - noun: a place where people can play

import UIKit

//有序集合

var str = "Hello, playground"

var array1: Array<Int> =
Array<Int>()

var array2: [Int] =
array1

var array3 = array2

array1.count

if array1.isEmpty{
    print("kong");
}

var fiveInts = [1,2,3,4,5]

fiveInts.count

fiveInts.append(6)

fiveInts += [7,8,9]

fiveInts.insert(0, atIndex:
0)

fiveInts.endIndex.predecessor()
fiveInts.startIndex.successor()

fiveInts[0]
fiveInts[0...1]
fiveInts[1...2]
fiveInts[0..<2]
fiveInts[0...1] = [0]
fiveInts

fiveInts.removeAtIndex(0)
fiveInts

for intvalue in
fiveInts{
 print(intvalue)
}

fiveInts.enumerate()

for(index,value) in
fiveInts.enumerate(){
 print("index--\(index),value---\(value)")
}

fiveInts.reverse()

//无序集合  set
let number = 10;
number.hashValue

let  pi = 3.14

pi.hashValue

let string = "swift"

string.hashValue

var  emptySet = Set<Character>()

emptySet = ["a","e","i","o","u"]

var  evenSet:Set = [2,4,6,8,10]

emptySet.isEmpty

emptySet.count

evenSet.insert(13)
evenSet.remove(13)
evenSet.contains(10)

for number in
evenSet.sort(){
print(number)
}

//union join about set

var setA:Set = [1,2,3,4,5,6]
var setB:Set = [4,5,6,7,8,9]
//交集
let interSectAB: Set =
setA.intersect(setB)//{5, 6, 4}
//补集
let exclusiveAB: Set = 
setA.exclusiveOr(setB)//{9, 7, 2, 3, 1, 8}

let unionAB: Set =
setA.union(setB)//{2, 4, 9, 5, 6, 7, 3, 1, 8}

let astructB : Set =
setA.subtract(setB)//{2, 3, 1}

var setC: Set = [1,2,3]

if setA ==
setC{
    
}else{
    print("not equal")
}

setA.isSupersetOf(setC)
//true

setC.isSubsetOf(setA)
//true

setA.isStrictSupersetOf(setC)
//true

setC.isStrictSubsetOf(setA)
//true

setC.isStrictSubsetOf(setB)
//false

setB.isDisjointWith(setC)
//true

//dictionary

var int2String = [Int :String]()

int2String[10] =
"TEN"
int2String[20] =
"TWENTY"

int2String//[20: "TWENTY", 10: "TEN"]
int2String = [:]

var capitalNumber = [
    1:"one",
    2:"two",
    3:"three",
    4:"four",
    5:"five",
    6:"six",
    7:"seven",
    8:"eight",
    9:"nine",
    10:"ten"
]

capitalNumber[1]//"one"
capitalNumber[10]

capitalNumber.updateValue("一", forKey:
1)//"one"
capitalNumber

capitalNumber.count

var opiontalNine =
capitalNumber.removeValueForKey(9)//"nine"
opiontalNine.dynamicType

for(key ,value ) in
capitalNumber{

   print("\(key):
\(value)")
}

for number in
capitalNumber.keys{
  print(number)
}

capitalNumber.keys//LazyMapCollection<Dictionary<Int, String>, Int>

let keyArray = [Int](capitalNumber.keys)//[10,
2, 4, 5, 6, 7, 3, 1, 8]

let valueArray = [String](capitalNumber.values)//["ten",
"two", "four", "five", "six", "seven", "three"

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