티스토리 뷰
출처
컬렉션 타입
컬렉션의 수정
Array
선언
var array = [Int]()
var array2 = Array<Int>()
var array3 : Array<Int>
기본값으로 배열 생성하기
var threeDoubles = Array(repeating: 0.0, count: 3)
두개의 다른 배열을 더하여 만들기
var threeDoubles = Array(repeating: 0.0, count: 3)
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
var sixDoubles = threeDoubles + anotherThreeDoubles
배열의 접근과 수정
var shoppingList = ["Eggs", "Milk"]
print("The shopping list contains \(shoppingList.count) items.")
if shoppingList.isEmpty {
print("The shopping list is empty.")
} else {
print("The shopping list is not empty.")
}
var firstItem = shoppingList[0]
firstItem = shoppingList.first!
shoppingList.append("Flour")
shoppingList += ["Baking Powder"] // shoppingList now contains 4 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
shoppingList.insert("Maple Syrup", at: 0)
shoppingList[0] = "Six eggs"
shoppingList[4...6] = ["Bananas", "Apples"]
let sixEggs = shoppingList.remove(at: 0)
let butter = shoppingList.removeLast()
for item in shoppingList {
print(item)
}
for (index, value) in shoppingList.enumerated() {
print("Item \(index + 1): \(value)")
}
Set
선언
var set = Set<Character>()
var set2 : Set<Character>
var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
접근
var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
print("I have \(favoriteGenres.count) favorite music genres.")
if favoriteGenres.isEmpty {
print("As far as music goes, I'm not picky.")
} else {
print("I have particular music preferences.")
}
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
} else {
print("It's too funky in here.")
}
수정
favoriteGenres.insert("Jazz")
if let removedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre)? I'm over it.")
} else {
print("I never much cared for that.")
}
반복
for genre in favoriteGenres {
print("\(genre)")
}
for genre in favoriteGenres.sorted() {
print("\(genre)")
}
집합 연산
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]
oddDigits.union(evenDigits).sorted() // 합집합
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersection(evenDigits).sorted() // 교집합
// []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted() // 차집합
// [1, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted() // 교집합의 여집합
// [1, 2, 9]
let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "🐱"]
let cityAnimals: Set = ["🐦", "🐭"]
houseAnimals.isSubset(of: farmAnimals) // house 가 farm 의 subset
// true
farmAnimals.isSuperset(of: houseAnimals) // farm 이 house 의 superset
// true
farmAnimals.isDisjoint(with: cityAnimals) // 공통된게 하나도 없음 (분리되어있음)
// true
Dictionary
선언
var dictionary = [Int : String]()
var dictionary2 = Dictionary<Int, String>()
var dictionary3 : Dictionary<Int, String>
var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
var airports2 = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
접근
var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
print("The airports dictionary contains \(airports.count) items.")
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
if let airportName = airports["DUB"] {
print("The name of the airport is \(airportName).")
} else {
print("That airport is not in the airports dictionary.")
}
수정
airports["LHR"] = "London"
airports["LHR"] = "London Heathrow"
if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
print("The old value for DUB was \(oldValue).")
}
airports["APL"] = "Apple International"
// "Apple International" is not the real airport for APL, so delete it
airports["APL"] = nil
반복
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
// YYZ: Toronto Pearson // LHR: London Heathrow
for airportCode in airports.keys {
print("Airport code: \(airportCode)")
} // Airport code: YYZ // Airport code: LHR
for airportName in airports.values {
print("Airport name: \(airportName)")
}
to Array
let airportCodes = [String](airports.keys)
// airportCodes is ["YYZ", "LHR"]
let airportNames = [String](airports.values)
// airportNames is ["Toronto Pearson", "London Heathrow"]
'iOS 개발 > Swift' 카테고리의 다른 글
[Swift 3] 함수 정의와 호출 및 함수 타입 (Function) (1) | 2017.04.14 |
---|---|
[Swift 3] 흐름 제어 (Flow Control / for, while, if-else, switch, fallthrough, guard, label, available) (0) | 2017.04.13 |
[Swift 3] 문자열과 문자 (String, Characters) (0) | 2017.04.12 |
[Swift 3] 기본 연산자 (Basic Operators) (0) | 2017.04.12 |
[Swift 3] 기본사항 -2 (Optional, Error Handle) (0) | 2017.04.12 |
- Total
- Today
- Yesterday
- CIImage
- docker
- RunLoop
- workerThread
- Swfit
- AWS
- Swift3
- ios
- applicationWillResignActive
- 꺼내먹어요
- CGImage
- dictionary
- NSManagedObjectContext
- NSManagedObject
- thread
- EffectiveObjectiveC
- 읽기 좋은 코드가 좋은 코드다
- Swift
- Arc
- NSManagedObjectModel
- Swift 3
- delegate
- optional
- UIView
- HTTP
- Block
- coredata
- Swift 3.0
- set
- string
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |