본문 바로가기
ios 뽀개기/스위프트 정리

7. 튜플 배열 딕셔너리 셋 (복습)

by 인생여희 2017. 11. 14.
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
 
 
 
 
 
 
 
 
//튜플 - 지정된 데이터의 묶음
//예1)
 
 
var person: (StringInt, Double) = ("kang",100,145.2)
 
//인덱스를 통해 값을 빼 올 수 있다.
print("\(person.0),\(person.1),\(person.2)")
 
//값 할당
person.1 = 99;
person.2 = 140.3
 
 
print("\(person.0),\(person.1),\(person.2)")
 
 
//예2)튜플 이름 요소 지정
var person2: (name:String, age: Int, height: Double) = ("kang",100,123.3)
 
//요소 이름 통해서 값 가져오기
print("\(person2.name),\(person2.age),\(person2.height)")
 
person2.age = 22
person2.2 = 123
 
 
print("\(person2.0),\(person2.1),\(person2.2)")
 
 
//예3
 
typealias PersonTuple = (name:String, age: Int, height: Double)
 
let kang: PersonTuple = ("kang",100,123.3)
let kim: PersonTuple = ("kim",200,223.3)
 
 
 
print("\(kang.name),\(kang.age),\(kang.height)")
 
print("\(kim.name),\(kim.age),\(kim.height)")
 
 
 
 
 
//  array 같은 데이터를 일렬로 나열한 후 순서대로 저장하는 형태의 컬렉션 타입
// isEmpy - 빈 배열 확인
// count - 배열 요소 개수 확인
 
 
 
var names: Array<String> = ["abcnt","chulsoo","younghee","yahoo"]
 
//var names: [String] = ["abcnt","chulsoo","younghee","yahoo"] 위와 같음
 
var emptyArray: [Any] = [Any]() //any데이터를 요소로 갖는 빈 배열 생성
//var emptyArray: [Any] = Array<Any>() 위와 같음
 
//배열의 타입을 정확히 명시해줬다면 [] 만으로도 빈 배열을 생성할 수 있습니다.
 
//var emptyArray: [Any] = [] 위와 같음
 
print(emptyArray.isEmpty)
print(names.count)
 
 
print(names[2])
names[2= "abcabc"
print(names[2])
//print(names[4]) 오류
 
// names[4] = "elsa" 범위 벗어나서오류
 
names.append("elsa")
names.append(contentsOf:["aaaa","bbbb"])
names.insert("happy",at:2// 인덱스 2에 삽입
names.insert(contentsOf:["gggg","hhh"], at:5)
 
 
 
 
 
print(names[5])
 
//print(names.index(of: "abcabc"))
//print(names.index(of: "abcabc123"))
 
print(names.first)
print(names.last)
print(names)
 
 
 
 
 
let firstItem: String = names.removeFirst()
let lastItem: String = names.removeLast()
let indexZeroItem: String = names.remove(at: 0)
 
print(firstItem)
 
print(lastItem)
 
 
print(indexZeroItem)
 
 
print(names[1 ... 3])
 
 
 
 
 
 
 
//  딕셔너리
//  키 : 값
 
 
 
//타입 알리아스를 이용
typealias StringIntDic = [String:Int]
 
 
// 키는 String, 값은 Int
//var numberForName: Dictionary<String,Int> = Dictionary<String,Int>()
//var numberForName: [String: Int] = [String: Int]()
//var numberForName: StringIntDictionary = StringIntDictionary()
//var numberForName: [String: Int] = [:]
 
 
//초기값 주어서 생성하기
var numberForName: [StringInt= ["kang":100"chulsoo":200"kim":433]
print(numberForName.isEmpty)
print(numberForName.count)
 
 
 
 
print(numberForName["kang"])
 
numberForName["kang"= 500;
 
print(numberForName["kang"])
 
 
 
numberForName["hong"= 5500;
 
print(numberForName["hong"])
 
//키에 해당하는 값 삭제
print(numberForName.removeValue(forKey: "kang"))
 
print(numberForName.removeValue(forKey: "kang")) // 해당하는 값 없으면 nil 반환
 
 
//print(numberForName["kang",default:2]) //0
 
 
 
 
 
//  set : 순서가 중요하지 않거나 각 요소가 유일한 값이어야 하는 경우
// 세트의 선언과 생성
 
//var names4: Set<String> = Set<String>()  //빈 세트 생성
//var names4: Set<String> = []             // 빈 세트 생성
 
//array와 마찬가지로 대괄호 사용
var names4: Set<String> = ["abcnt","eeee","ccccc","bbbbb","abcnt"]
 
//그렇기 때문에 타입 추론을 사용하게 되면 컴파일러는 Set가 아닌 Array로 
//타입을 지정한다.
 
var numbers = [100,200,300]
print(type(of: numbers))
print(type(of: names4))
 
 
 
 
print(names.isEmpty); //false
print(names4.count)   //4
 
print(names4.count)   //4
 
names4.insert("hi")
 
 
print(names4.count)    //5
 
 
 
print(names4.remove("abcnt")) //abcnt 반환
 
 
print(names4.remove("fff")) //nil반환
 
 
 
 
 
//세트의 활용 - 집합 연산
 
 
let englishClass: Set<String> = ["a","b","c"];
 
let koreanClass: Set<String> = ["a","b","f","g"];
 
//교집합
let intersectSet: Set<String> = englishClass.intersection(koreanClass);
print(intersectSet);
 
 
//여집합의 합
let symmetricDiffSet: Set<String> = englishClass.symmetricDifference(koreanClass)
print(symmetricDiffSet)
 
 
//합집합
 
let unionSet: Set<String> = englishClass.union(koreanClass);
print(unionSet);
 
//차집합
 
let subtractSet: Set<String> = englishClass.subtracting(koreanClass)
print(subtractSet)
 
//정렬
 
print(unionSet.sorted())
 
 
 
 
 
let 새: Set<String> = ["비둘기","닭","기러기"];
let 포유류: Set<String> = ["사자","호랑이","곰"]
let 동물: Set<String> = 새.union(포유류)
 
 
print(새.isDisjoint(with:포유류)) // 서로 배타적인가? true
print(새.isSubset(of: 동물)) // 새가 동물의 부분집합인가요 -? true
print(동물.isSuperset(of:포유류)) // 동물은 포유류의 전체집합인가요?  true
print(동물.isSuperset(of:새)) // 동물은 새의 전체집합인가요 ? true
 
cs


반응형

'ios 뽀개기 > 스위프트 정리' 카테고리의 다른 글

9. 연산자의 종류  (0) 2017.11.15
8. 열거형  (0) 2017.11.15
6. 복습정리  (0) 2017.11.14
5.함수 조건문 반복문  (0) 2017.11.08
4. 컬렉션 타입 array dictionary set  (0) 2017.11.08

댓글