본문 바로가기

전체 글126

class class User { let id: Int init(id: Int) { self.id = id print("User \(id): I'm alive!") } deinit { print("User \(id): I'm dead!") } } id : 멤버변수 init : 생성자 deinit : 종결자 계승 inherit class Vehicle { let isElectric: Bool init(isElectric: Bool) { self.isElectric = isElectric } func description() { print("I'm a vehicle.") } } base 클래스 기본적인 기능만 구현 class Car: Vehicle { let isConvertible: Bool init(isElectr.. 2024. 2. 15.
struct 내부 변수를 변경하는 메서드를 포함하지 않는 struct struct Song { let title: String let singer: String let year: Int func printLyrics() { print("bla bla blar ...") } let song1 = Song(title: "Friend", artist: "Taylor Swift", year: 2012) let wings = Song(title: "Wings", artist: "BTS", year: 2016) print(song1.title) print(wings.artist) song1.printLyrics() wings.printLyrics() 내부 변수를 변경하는 메서드를 포함하는 struct struct Song { .. 2024. 2. 15.
예외처리 do { let result = try someRiskyWork() } catch { print("Handle errors here") } 2024. 2. 14.
function func printHello(name : String) { print("Hello \(name)") } printHello( name : "Kitty" ) or printHello("Kitty") func printHello(nick name : String) { print("Hello \(name)") } printHello( nick : "Kitty" ) ⭐️ 외부용 파라미터로 nick 을 내부용으로 name 을 정의 func printHello(name: String) -> Bool { print("Hello\(name)") return true } let b = printHello( name : "Kitty" ) func printHello(name : String) -> String { "Hel.. 2024. 2. 14.
Loops for in let platforms = ["iOS", "macOS", "tvOS", "watchOS"] for os in platforms { print("Swift works great on \(os).") } Swift works great on iOS. Swift works great on macOS. Swift works great on tvOS. Swift works great on watchOS. // 5를 포함 for i in 1...5 { print("Counting from 1 through 5: \(i)") } // 5를 미포함 for i in 1.. 2024. 2. 14.
switch enum enum Weather { case sun, rain, wind, snow, unknown } let forecast = Weather.sun switch forecast { case .sun: print("It should be a nice day.") case .rain: print("Pack an umbrella.") case .wind: print("Wear something warm") case .snow: print("School is cancelled.") case .unknown: print("Our forecast generator is broken!") } String let place = "Metropolis" switch place { case "Gotham": print(.. 2024. 2. 14.