let sum : Int
let name : String
sum = 100 // 이후 변경 불가능
name = "woonsik" //이후 변경 불가능
var id : Int
id = 100 //변경 가능
id = 200
변수로 선언 후 변경을 하지 않는다면 let으로 선언하라는 에러가 나온다
var someAny: Any = 100;
someAny = "any"
someAny = 10.222
let someDouble: Double = someAny //error
Double != Any (자료형이 다르다)
class someClass{}
var someAnyObject: AnyObject = someClass();
someAnyObject = 클래스의 인스턴스
var someAny: Any = 100
var someAnyObject: AnyObject = someClass()
someAny = nill // error
someAnyObject = nill // error
각각 Any, AnyObject타입으로 할당 불가능
var integerArr: Array<Int> = Array<Int>()
var integerArr: Array<Int> = [Int]()
var integerArr: Array<Int> = []
var integerArr: [Int] = Array<Int>()
var integerArr: [Int] = [Int]()
var integerArr: [Int] = []
var integerArr = [Int]()
intergerArr.append() //추가
intergerArr.contains() //유무
intergerArr.remove() //삭제
intergerArr.removeLast() //마지막 삭제
intergerArr.removeAll() //전부 삭제
intergerArr.count //갯수
var anyDictionary: Dictionary <String : Any> = [String:Any]()
var anyDictionary: Dictionary <String, Any> = Dictionary<String, Any>()
var anyDictionary: Dictionary <String, Any> = [:]
var anyDictionary: [String: Any] = Dictionary<String, Any>()
var anyDictionary: [String: Any] = [String: Any]()
var anyDictionary: [String: Any] = [:]
var anyDictionary = [String: Any]()
//[키:값] 추가 및 변경
anyDictionary["someKey"] = "value"
anyDictionary["anotherKey"] = 100
anyDictionary["someKey"] = "changeValue"
//키 제거 방법(동일)
anyDictionary.removeValue(forKey: "anotherKey")
anyDictionary["anotherKey"] = nill
var IntegerSet: Set<Int> = Set<Int>()
var IntegerSet: Set<Int> = []
var IntegerSet: Set<Int> = [100, 200, 300]
integerSet.insert() //삽입
integerSet.contains() //유무
integerSet.remove() //삭제
integerSet.count //갯수
//집합 연산
let union: Set<Int> = setA.union(setB)
let sortedUnion: Array<Int> = union.sorted()
let intersection: Set<Int> = setA.intersection(SetB)
let subtracting: Set<Int> = setA.subtracting(SetB)
요소의 추가 및 삭제는 insert(_:)
, remove(_:)
를 이용하며 삭제연산 후 결과값을 반환한다
var IntSet: Set<Int> = [100, 200]
IntSet.insert(300)
IntSet.insert(400)
IntSet.insert(400)
IntSet.remove(400)
var count: Int = IntSet.count
for num in IntSet {
print(num)
}
선언 방식
enum school { case 대학교 case 고등학교 case 중학교 case 초등학교 } enum school { case 대학교, 고등학교, 중학교, 초등학교 }
선언 방식
enum school : String { case elementary = "초등학교" case middel = "중학교" case high = "고등학교: case college = "대학교" } 이때 raw value(원시값)은 rawValue메서드를 통해서 취할수 있다 var getvalue: school = school.college print(getvalue.rawValue) //대학교
물론 일부항목만 원시값을 주고 나머지에 대해서 부여하지 않을수 있으며 Int의 경우 0부터 1씩 늘어난 값을 자동으로 할당 받으며 String의 경우 항목 그자체가 원시값이 된다
열거형 항목 각각은 연관값을 가질수 있고 다른 항목이 연관값을 가지는 것과는 별개이다
enum MainDish {
case pasta (taste: String)
case pizza (dough: String, topping: String)
case chicken (withSouce: Bool)
case rice
}
var dinner: MainDish = MainDish.pasta(taste: "크림")
dinner = .pizza(dough: "치즈크러스트", topping: "소고기")
dinner = .chicken(withSouce: true)
dinner = .rice
enum pastaTaste {
case cream, toato
}
enum pizzaDough {
case cheeseCrust, thin, original
}
enum pizzaTopping {
case pepperoni, cheese, bacon, meet
}
enum MainDish2 {
case pasta(taste: pastaTaste)
case pizza(dough: pizzaDough, topping: pizzaTopping)
case chicken(withSauce: Bool)
case rice
}
var dinner2: MainDish2 = MainDish2.pasta(taste: pastaTaste.cream)
dinner2 = .pizza(dough: pizzaDough.thin, topping: pizzaTopping.meet)
dinner2 = .chicken(withSauce: true)
dinner2 = .rice
열거형에 포함된 모든 케이스를 알고자 할때 열거형의 이름뒤에 : CaseIterable
을 채택하면 allCases
라는 타입 프로퍼티를 통해 모든 케이스의 컬렉션을 생성해 준다
>원시값이 있는경우
enum books: CaseIterable {
case abook
case bbook
case cbook
case dbook
case ebook
}
let all_book: [books] = books.allCases
print(all_book)
dump(all_book)
>원시값이 없는경우
enum books2: String, CaseIterable {
case abook = "c1"
case bbook = "c2"
case cbook = "java"
case dbook = "pytho"
case ebook
}
let all_book2: [books2] = books2.allCases
dump(all_book2)
- 출력예시 -
- __lldb_expr_153.books.abook
- __lldb_expr_153.books.bbook
- __lldb_expr_153.books.cbook
- __lldb_expr_153.books.dbook
- __lldb_expr_153.books.ebook
연관값을 갖는 경우 단순 선언 만으로는 사용할 수 없으며 직접 allCases를 구현해줘야 한다
열거형 항목의 연관값이 열거형 자신의 값이고자 할때(재귀적) 사용한다
특정 case 또는 enum앞에 indirect
키워드를 사용한다
enum ArithmeticExpression {
case number(Int)
indirect case addition(ArithmeticExpression, ArithmeticExpression)
indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
}
indirect enum ArithmeticExpression {
case number(Int)
case addition(ArithmeticExpression, ArithmeticExpression)
case multiplication(ArithmeticExpression, ArithmeticExpression)
}