열거형 UITableViewCell.CellStyle
https://developer.apple.com/documentation/uikit/uitableviewcell/cellstyle
UITableViewCell.CellStyle | Apple Developer Documentation
An enumeration for the various styles of cells.
developer.apple.com
열거형(enum)를 사용하는 프로그램 언어
열거형 정의
enum 열거형명{
열거형 정의
}
enum Planet {
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
} //하나의 case문에 멤버들 나열하는 것도 가능
예시
enum Compass {
case North
case South
case East
case West
}
//var x : Compass // Compass형 인스턴스 x
print(Compass.North) // North
var x = Compass.West
print(type(of:x)) // Compass
x = .East
print(x) // East
enum Compass {
case North, South, East, West
}
//var x : Compass // Compass형 인스턴스 x
print(Compass.North) // North
var x = Compass.West
print(type(of:x)) // Compass
x = .East
print(x) // East
열거형 멤버별 기능 정의
enum Compass {
case North
case South
case East
case West
}
var direction : Compass
direction = .South
switch direction { //switch의 비교값이 열거형 Compass
case .North: //direction이 .North이면 "북" 출력
print("북")
case .South:
print("남")
case .East:
print("동")
case .West:
print("서") //모든 열거형 case를 포함하면 default 없어도 됨
}
열거형 멤버에는 메서드도 가능
enum Week {
case Mon,Tue,Wed,Thur,Fri,Sat,Sun
func printWeek() { //메서드도 가능
switch self {
case .Mon, .Tue, .Wed, .Thur, .Fri:
print("주중")
case .Sat, .Sun:
print("주말")
}
}
}
Week.Sun.printWeek() //주말
열거형의 rawValue
enum Color : Int { //원시값(rawValue) 지정
case red
case green = 2
case blue
}
print(Color.red) //red
print(Color.blue) //blue
print(Color.red.rawValue) //0
print(Color.blue.rawValue) //3
String형 값을 갖는 열거형의 rawValue
enum Week : String {
case Monday = "월"
case Tuesday = "화"
case Wednesday = "수"
case Thursday = "목"
case Friday = "금"
case Saturday // 값이 지정되지 않으면 case 이름이 할당됨
case Sunday // = "Sunday"
}
print(Week.Monday) //Monday
print(Week.Monday.rawValue) //월
print(Week.Sunday) //Sunday
print(Week.Sunday.rawValue) //Sunday
연관 값(associated value)을 갖는 enum
enum Date {
case intDate(Int,Int,Int) // (int,Int,Int)형 연관값을 갖는 intDate
case stringDate(String) // String형 연관값을 값는 stringDate
}
var todayDate = Date.intDate(2025,4,30)
todayDate = Date.stringDate("2025년 5월 20일") // 주석처리하면? 2025년 4월 30일 이 출력됨
switch todayDate {
case .intDate(let year, let month, let day):
print("\(year)년 \(month)월 \(day)일")
case .stringDate(let date):
print(date) // 2025년 5월 20일
}
옵셔널은 연관 값(associated value)을 갖는 enum
let age : Int? = 30 //Optional(30)
switch age {
case .none: // nil인 경우
print("나이 정보가 없습니다.")
case .some(let a) where a < 20:
print("\(a)살 미성년자입니다")
case .some(let a) where a < 71:
print("\(a)살 성인입니다")
default:
print("경로우대입니다")
} //30살 성인입니다.
var x : Int? = 20 //.some(20)
var y : Int? = Optional.some(10)
var z : Int? = Optional.none
var x1 : Optional<Int> = 30
print(x, y, z, x1) // Optional(20) Optional(10) nil Optional(30)
구조체
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/classesandstructures/
Memberwise Initializer가 자동으로 만들어짐
Int, Double, String 등 기본 자료형은 구조체
https://developer.apple.com/documentation/swift/int
@frozen struct Int
@frozen attribute: 저장 property 추가, 삭제 등 변경 불가
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/attributes/
Array, Dictionary, Set은 Generic Structure
https://developer.apple.com/documentation/swift/array
@frozen struct Array
구조체/enum의 인스턴스는 값(value) 타입, 클래스의 인스턴스는 참조(reference) 타입
구조체는 상속 불가
구조체 : Memberwise Initializer 자동 생성
struct Resolution { //구조체 정의
var width = 1024 //프로퍼티
var height = 768
}
let myComputer = Resolution() //인스턴스 생성
print(myComputer.width) //프로퍼티 접근 1024
class Resolution {
var width = 1024 /
var height = 768
init(width:Int,heigt:Int){
self.width = width
self.height = heigt
}
}
let myComputer = Resolution(width: 1000, heigt: 2000)
print(myComputer.width) 1000
struct Resolution { //구조체 정의
var width : Int //프로퍼티 초기값이 없어요!!
var height : Int
} //init()메서드 없어요, 그런데!
let myComputer = Resolution(width:1920,height:1080) //Memberwise Initializer
print(myComputer.width) //1920
클래스 내에 구조체
struct Resolution {
var width = 1024
var height = 768
}
class VideoMode {
var resolution = Resolution()
var frameRate = 0.0
}
let myVideo = VideoMode()
print(myVideo.resolution.width) // 1024
구조체는 값 타입(value type) 클래스는 참조 타입(reference type)
var x = 1
var y = x
print(x,y) // 1 1
x = 2
print(x,y) // 2 1
y = 3
print(x,y) // 2 3
struct Human {
var age : Int = 1
}
var kim = Human()
var lee = kim //값 타입
print(kim.age, lee.age) // 1 1
lee.age = 20
print(kim.age, lee.age) // 1 20
kim.age = 30
print(kim.age, lee.age) // 30 20
class Human {
var age : Int = 1
}
var kim = Human()
var lee = kim //값 타입
print(kim.age, lee.age) // 1 1
lee.age = 20
print(kim.age, lee.age) // 20 20
kim.age = 30
print(kim.age, lee.age) // 30 30
struct Resolution {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolution()
var frameRate = 0
var name: String?
}
var hd = Resolution(width: 1920, height: 1080)
//자동 Memberwise Initializer
var highDef = hd
//구조체는 값타입(value type)
print(hd.width, highDef.width) // 1920 1920
hd.width = 1024
print(hd.width, highDef.width) // 1024 1920
var xMonitor = VideoMode()
xMonitor.resolution = hd
xMonitor.name = "LG"
xMonitor.frameRate = 30
print(xMonitor.frameRate) // 30
var yMonitor = xMonitor
//클래스는 참조타입(reference type)
yMonitor.frameRate = 25
print(yMonitor.frameRate) // 25
print(xMonitor.frameRate) // 25