반응형
연산자
nil 병합 연산자
- 형태 : a ?? b
- 의미 : a가 nil(값이 없다)일 경우, b를 반환한다는 의미.
let userColor = "green"
var nilColor: String?
var colerNilCheck = nilColor ?? userColor // nilColor가 nil 값, 즉 userColor가 들어간다.
문자열과 문자
문자열 리터럴
- 형태 : let something = "something"
여러줄 문자열 리터럴
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
문자열 결합
- 형태 : "abc" + "def" = "abcdef"
문자열 삽입
let mutiplier = 3
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"
// message : "3 times 2.5 is 7.5"
문자열 인덱스
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a
- 위의 메소드들은, Array, Dictionary, Set에서도 동일하게 사용 가능.
문자열의 개별 문자들을 접근하기 위해서는 indices 속성을 사용.
for index in greeting.indices {
print("\(greeting[index]) ", terminator: "")
// G u t e n T a g !
문자의 삽입과 삭제
- 문자의 삽입과 삭제에는 insert(:at:), insert(contentsOf:at:), remove(at:), removeSubrange(:) 메소드들이 있음.
콜렉션 타입
- Array, Set, Dictonary가 존재.
Array(순서가 있음)
빈 배열의 생성
- 형태 : var someInts = [Int]()
- append 사용 시 : someInts.append(3) --> [3]
사용법
// 1. 원소 갯수 확인
print("The shopping list contains \(shoppingList.count) items.")
// The shopping list contains 2 items.
// 2. 배열이 비었는지 확인
if shoppingList.isEmpty {
print("The shopping list is empty.")
} else {
print("The shopping list is not empty.")
}
// The shopping list is not empty.
// 3. 배열에 원소 추가
shoppingList.append("Four")
// shoppingList.count = 3
// 4. 특정 위치의 배열 확인
var firstItem = shoppingList[0]
// firstItem : "Eggs"
// 5. 특정 위치에 원소 추가
shoppingList.insert("Maple Syrup", at:0)
// 6. 특정 위치 원소 삭제
let mapleSyrup = shoppingList.remove(at: 0)
let apples = shoppingList.removeLast()
Set(순서가 없고, 중복되지 않음)
- Set 형태로 저장되기 위해선, 형태가 반드시 hashable 이어야 함.
// hashable : 해쉬 값을 구할 수 있는 형태.
// hash-value : 데이터를 간단한 숫자로 변환한 것.
빈 배열 생성
var letters = Set<Character>()
print("letters is of type Set<Character> with \(letters.count) items.")
// letters is of type Set<Character> with 0 items.
사용법
// 1. Set 갯수 확인
print("I have \(favoriteGenres.count) favorite music genres.")
// I have 3 favorite music genres.
// 2. Set이 비었는지 확인
if favoriteGenres.isEmpty {
print("As far as music goes, I'm not picky.")
} else {
print("I have particular music preferences.")
}
// I have particular preferences.
// 3. Set의 추가/삭제
favoriteGenres.insert("Jazz")
if let removedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre)? I'm over it.")
} else {
print("I never much cared for that.")
}
// Rock? I'm over it.
// 4. Set의 값 확인
if favoriteGenres.contains("Funk") {
print("I get up on the good foot.")
} else {
print("It's too funky in here.")
}
// It's too funky in here.
Dictionary(key, value를 가지고 있음)
빈 Dictionary 생성
var namesOfIntegers = [Int: String]()
사용법
// 1. 갯수 확인
print("The airports dictionary contains \(airports.count) items.")
// The airports dictionary contains 2 items.
// 2. 딕셔너리가 비었는지 확인
if airports.isEmpty {
print("The airports dictionary is empty.")
} else {
print("The airports dictionary is not empty.")
}
// The airports dictionary is not empty.
// 3. 딕셔너리 값 할당
airports["LHR"] = "London"
// the airports dictionary now contains 3 items
참고 자료
https://jusung.gitbook.io/the-swift-language-guide/
반응형
'iOS & Swift' 카테고리의 다른 글
[Swift] Swift 공식문서 정리 - 4(열거형) (0) | 2022.04.06 |
---|---|
[Swift] Swift 공식문서 정리 - 3(클래스 & 구조체) (0) | 2022.04.04 |
[Swift] Swift 공식문서 정리 - 2(제어문, 함수) (0) | 2022.03.29 |
[iOS] Key-Chain이란? (0) | 2022.03.26 |
[iOS] AppDelegate란 ? (0) | 2022.03.24 |