集合タイプ(Array、Dictionary、Set)

3018 ワード

Array宣言

var integers: Array<Int> = Array<Int> ()
integers.append(1) (o)		-->[1]
integers.append(100) (o)	-->[1,100]
integers.append(1.1) (x)

integers.contain(1)		-->True
integers.contain(99)		-->False

integers.remove(at:0)		--> at 인덱스 삭제 //[100]
integers.removeLast()		--> 마지막요소 삭제 //[]
integers.removeAll()		--> 모두삭제 //[]

integers.count 	--> 0

비어있는상태에서 integers[0] 접근시 error

多様な表現

var doubles:Array<Double> = Array<Double>()
var doubles:Array<Double> = [Double]()
var doubles:Array<Double> = []

let immutbleArray = [1,2,3]
immutableArray.append(4) 	--> let 변경 (x)
immutableArray.removeAll() 	--> let 변경 (x)

Dictionary

var anyDict:Dictionary<String,Any> = [String:Any]()
var anyDict:Dictionary<String,Any> = [:]


anyDict["apple"] = 1
anyDict["banana"] = 2
anyDict["grape"] = 3	--> ["apple":1,"banana":2,"grape":3]

Dictionary는 순서정렬 x 단순히 키와 값만 매칭
넣어준 순서가 유지되지않는다

anyDict.removeValue(forKey:"apple")	--> ["banana":2,"grape":3]

Set

var integerSet:Set<Int> = Set<Int>()

integerSet.insert(1)		--> {1}
integerSet.insert(2)		--> {2,1}
integerSet.insert(3)		--> {3,1,2}
integerSet.contains(1)		--> true
integerSet.remove(1)		--> {2,3}
integerSet.removeFirst()	--> {3}
integerSet.count		--> 1

しゅうごうえんざん