-> ์ฌ์ค ์ ์๋ ๋ด์ฉ๋ค์, ์ ์ฅ ํ๋กํผํฐ๋ฅผ ์ด๊ธฐํํ๋ ์์๋... ํ์ ์ ๋ฐ๋ผ ์ด๊ธฐํ ๋ฐฉ๋ฒ / ํ๋ก์ธ์ค์ ์ฐจ์ด๋ฅผ ๋ํ๋ด๋ ๊ฒ์ด๊ณ ํต์ฌ์ ์ฌ๊ธฐ.
init() {
// ๊ฐ์ฅ ๊ฐ๋จํ ํํ. ํ๋ผ๋ฏธํฐ X, init ํค์๋ ์ฌ์ฉ.
}
struct Fahrenheit {
var temperature: Double
init() {
temperature = 32.0
}
}
var f = Fahrenheit()
print("๊ธฐ๋ณธ ์จ๋๋ \(f.temperature)ยฐF ์
๋๋ค.")
// Prints "๊ธฐ๋ณธ ์จ๋๋ 32.0ยฐF ์
๋๋ค."
์์ ์์์ฒ๋ผ ํญ์ ๊ฐ์ ๊ฐ์ ๊ธฐ๋ณธ ๊ฐ์ผ๋ก ๊ฐ์ง๋ ๊ฒฝ์ฐ,
์ ์ธ๊ณผ ๋์์ ๊ฐ์ ํ ๋นํ๋ ๋ฐฉ์์ผ๋ก ์ด๊ธฐ๊ฐ์ ์ค์ ํ ์ ์๋ค.
struct Fahrenheit {
var temperature = 32.0
}
struct Celsius {
var temperatureInCelsius: Double
init(fromFahrenheit fahrenheit: Double) {
temperatureInCelsius = (fahrenheit - 32.0) / 1.8
}
init(fromKelvin kelvin: Double) {
temperatureInCelsius = kelvin - 273.15
}
init(_ celsius: Double) {
temperatureInCelsius = celsius
}
}
let boilingPointOfWater = Celsius(fromFahrenheit: 212.0) // 100.0
let freezingPointOfWater = Celsius(fromKelvin: 273.15) // 0.0
_
์ฌ์ฉํ์!struct Color {
let red, green, blue: Double
// ์ธ์ 3๊ฐ์ธ init
init(red: Double, green: Double, blue: Double) {
self.red = red
self.green = green
self.blue = blue
}
// ์ธ์ 1๊ฐ์ธ init
init(white: Double) {
red = white
green = white
blue = white
}
// ์ธ์ 1๊ฐ์ธ init ์ด๋ ์์ ๋ค๋ฆ.
// ์ธ์ ์ด๋ฆ์ด ๋ค๋ฅด๋๊น!!
init(red: Double) {
self.red = red
green = 0
blue = 0
}
}
class SurveyQuestion {
let text: String
var response: String?
init(text: String) {
self.text = text
}
func ask() {
print(text)
}
}
let beetsQuestion = SurveyQuestion(text: "How about beets?")
beetsQuestion.ask()
// Prints "How about beets?"
beetsQuestion.response = "I also like beets. (But not with cheese.)"
class Album {
let titleSong: String = "INVU"
var hotSong: String? // ๊ธฐ๋ณธ์ ์ผ๋ก nil ์ด ์ ๊ณต๋จ.
var top100: Bool = true
}
var third = Album()
third.hotSong = "๊ทธ๋ฐ ๋ฐค"
struct Size {
var width = 0.0, height = 0.0
}
// ์๋ 3 ๋ผ์ธ์ ๋ชจ๋ ๊ฐ์ ๋ป.
let twoByTwo = Size(width: 2.0, height: 2.0)
let twoByTwo = Size(width: 2.0)
let twoByTwo = Size()
self.init
์ผ๋ก ๋ค๋ฅธ ์ด๊ธฐํ ๊ตฌ๋ฌธ์์ ๋ถ๋ฅผ ์ ์๋ค.struct Size {
var width = 0.0, height = 0.0
}
struct Point {
var x = 0.0, y = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
init() {}
init(origin: Point, size: Size) {
self.origin = origin
self.size = size
}
init(center: Point, size: Size) {
let originX = center.x - (size.width / 2)
let originY = center.y - (size.height / 2)
// ํ๋์ init ์์ ๋ค๋ฅธ init ํธ์ถ ๊ฐ๋ฅ.
// initialiation delegate ๋ผ๊ณ ๋ถ๋ฆ.
self.init(origin: Point(x: originX, y: originY), size: size)
}
}
-> ํด๋์ค์ ๊ฑฐ์ ์กด์ฌํ์ง ์๊ณ (๊ธฐ๋ณธ init ์ด์ฉ), ์์ผ๋ฉด ํ๋๋ง ์๋ค.
// ์ ์ฅ ํ๋กํผํฐ์ ๋ํ ์ด๊ธฐํ๋ฅผ ๋ชจ๋ ํด๋๊ณ ,
class ViewModel {
var title: String = "title"
var content: String = "content"
var author: String?
}
// ๊ธฐ๋ณธ init ์ฌ์ฉ
let vm = ViewModel()
init(parameters) {
//statements
}
-> ๋์ฒด๋ก ์ ๊ณตํ์ง ์๊ณ , ์๊ฐ์ด ์ ์ฝ๋๊ฑฐ๋ ๋ ๋ช ํํ ์๋ฏธ๋ฅผ ์ง๋๋๋ง ๋ง๋ ๋ค.
convenience init(author: String) {
self.init() // ๊ฐ์ ๊ณ์ธต ๋ด์ desinated init ์ ๋ถ๋ฅธ๋ค.
self.author = author
}
let vm = ViewModel(author: "์ต๋ช
์ ์ฝ๋ผ๋ฆฌ")
convenience init(parameters) {
// statements
}
๊ฒฐ๋ก : desinated init ์ ์์ ํด๋์ค๋ก, convenience ๋ ๊ฐ์ ํด๋์ค๋ก init ์ ์์ํ๋ค.
NOTE
init() {
// ์์๋์ง ์์ ๋ชจ๋ ํ๋กํผํฐ ์ด๊ธฐํ
super.init() // ์์๋ ๊ฐ ์ค์ ๋จ
// ์์๋ฐ์ ๋ชจ๋ ํ๋กํผํฐ ์ด๊ธฐํ
}
convenience init() {
self.init() // designated init
// ์ด๊ธฐํ ํ๊ณ ์ถ์ ๊ฐ ์ด๊ธฐํ.
}
์ฌ๊ธฐ๋ถํฐ ๋ด์ผ!!
override init
์ผ๋ก ์ง์ ์์ ํด๋์ค์ designated init ์ ์ค๋ฒ๋ผ์ด๋ฉํ ์ ์๋ค.override
๋ฅผ ์ธ ํ์๊ฐ ์๋ค.class Vehicle {
var numberOfWheels = 0 // stored property ์ ๋ํ ๊ธฐ๋ณธ init ์ ๊ณต
var description: String {
return "\(numberOfWheels) wheel(s)"
}
}
class Bicycle: Vehicle {
// Vehicle ์ ๊ธฐ๋ณธ init ๊ณผ ๊ฐ๊ธฐ ๋๋ฌธ์,
// override ๋ฅผ ๋ถ์ฌ์ผ ํ๋ค.
override init() {
super.init() // ๋ถ๋ชจ๊บผ ๋จผ์ ์ธํ
ํ๊ณ ,
numberOfWheels = 2 // ์์๊ป ์ธํ
ํด์ผ ๋ฎ์ด์ฐ์ด์ง ์๋๋ค.
}
}
class Hoverboard: Vehicle {
var color: String
// ์ด๊ธฐํ ๊ตฌ๋ฌธ์์, color ๋ผ๋ ์ด ํด๋์ค์ ์กด์ฌํ๋ property ๋ง ์ด๊ธฐํํ๊ณ ,
// ์์ ๋ฐ์ numberOfWheels ๋ Vehicle ๊ฒ์ ๋ฐ๋ผ๊ฐ ๊ฒ์.
// ๊ทธ๋ ๋ค๋ฉด, super.init() ์ ์๋์ผ๋ก ํธ์ถ๋จ.
init(color: String) {
self.color = color
// super.init() implicitly called here
}
override var description: String {
return "\(super.description) in a beautiful \(color)"
}
}
์์ ํด๋์ค์ designated init ์ override ํ์ฌ ํ์ ํด๋์ค์ convenience init ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค. (์ด๋ ๊ฒ ๊ตฌํํด๋ ๋ถ๋ชจ์ designated init ์ ๋ชจ๋ ๊ตฌํํ ๊ฒ์ด๋ฏ๋ก ๊ท์น 2๋ฅผ ๋ง์กฑ)
Food > RecipeIngredient > ShoppingListItem ์ผ๋ก ์์๋๋ ํด๋์ค์ ์์๋ฅผ ๋ณผ ๊ฒ์.
class Food {
var name: String
// ํด๋์ค๋ memberwise init ์ด ๊ธฐ๋ณธ์ผ๋ก ์ ๊ณต๋์ง ์์ผ๋ฏ๋ก,
// designated init ์ ๊ตฌํํ๋ค.
// ๋ถ๋ชจ ํด๋์ค๊ฐ ์์ผ๋ฏ๋ก, super.init() ์ ๊ตฌํํ ํ์๊ฐ ์์.
init(name: String) {
self.name = name
}
// convenience init -> designated init
convenience init() {
self.init(name: "[Unnamed]")
}
}
let meat = Food(name: "Bacon")
let mystery = Food()
// Food ๋ฅผ ์์.
class RecipeIngredient: Food {
var quantity: Int
// designated init
init(name: String, quantity: Int) {
// ์์ ์ ๊ฒ 1 ์ถฉ์กฑ.
self.quantity = quantity // ์์์๋ง ์๋ ๊ฒ์ ๋จผ์ ์ด๊ธฐํํ๊ณ
super.init(name: name) // ๋ถ๋ชจ ์ด๊ธฐ์ ํธ์ถ.
}
// convenience init.
// Food ์ designated init ๊ณผ ๊ฐ์ ํ๋ผ๋ฏธํฐ. == override
override convenience init(name: String) {
self.init(name: name, quantity: 1)
}
}
// ๋ถ๋ชจ์ convenience init (๋ถ๋ชจ์ ๋ชจ๋ designated init ๊ตฌํํด์ ์๋ ์์)
let oneMysteryItem = RecipeIngredient()
// ๋ถ๋ชจ์ designated init ์ override ํ์ฌ convenience init ์ผ๋ก ๊ตฌํํจ.
let oneBacon = RecipeIngredient(name: "Bacon")
// ์์์ designated init
let sixEggs = RecipeIngredient(name: "Eggs", quantity: 6)
class ShoppingListItem: RecipeIngredient {
var purchased = false // ์ด๊ธฐํ ๊ตฌ๋ฌธ ๋์ ์ด๊ธฐ๊ฐ์ ๋ฃ์.
var description: String {
var output = "\(quantity) x \(name)"
output += purchased ? " โ" : " โ"
return output
}
// ์ด๊ธฐํ ๊ตฌ๋ฌธ์ด ์์ ์๊ธฐ ๋๋ฌธ์,
// ์์ ํด๋์ค์ ๋ชจ๋ designated init, convenience init ์ ์์.
}
// ์์๋ฐ์ 3๊ฐ์ง init ์ ๋ชจ๋ ์ฌ์ฉ๊ฐ๋ฅ.
var breakfastList = [
ShoppingListItem(),
ShoppingListItem(name: "Bacon"),
ShoppingListItem(name: "Eggs", quantity: 6),
]
breakfastList[0].name = "Orange juice"
breakfastList[0].purchased = true
for item in breakfastList {
print(item.description)
}
// 1 x Orange juice โ
// 1 x Bacon โ
// 6 x Eggs โ
init?
struct Animal {
let species: String
init?(species: String) {
// ์ด๊ธฐํ๊ฐ ์คํจํ๋ ์๊ฐ์ return nil ์ ํ๋ฉด ๋๋ค.
// ์ง์ง๋ก init ๊ตฌ๋ฌธ์ด nil ์ ๋ฐํํ๋ ๊ฒ์ ์๋๊ณ ,
// ์ด ๊ตฌ๋ฌธ์ด ์คํจํ๋ฉด ํด๋น ์ธ์คํด์ค๊ฐ nil ์ด ๋๋ค.
if species.isEmpty { return nil }
self.species = species
}
}
let anonymousCreature = Animal(species: "")
// anonymousCreature is of type Animal?, not Animal
if anonymousCreature == nil {
print("The anonymous creature could not be initialized")
}
// Prints "The anonymous creature could not be initialized"
enum ์์๋ failable init ์ ์ฌ์ฉํ ์ ์๋ค.
enum TemperatureUnit {
case kelvin, celsius, fahrenheit
init?(symbol: Character) {
switch symbol {
case "K":
self = .kelvin
case "C":
self = .celsius
case "F":
self = .fahrenheit
default:
return nil
}
}
}
rawValue
๋ผ๋ ํ๋ผ๋ฏธํฐ๋ก,enum TemperatureUnit: Character {
case kelvin = "K", celsius = "C", fahrenheit = "F"
}
let fahrenheitUnit = TemperatureUnit(rawValue: "F") // .kelvin
let unknownUnit = TemperatureUnit(rawValue: "X") // nil
class Product {
let name: String
init?(name: String) {
if name.isEmpty { return nil }
self.name = name
}
}
class CartItem: Product {
let quantity: Int
init?(name: String, quantity: Int) {
if quantity < 1 { return nil }
self.quantity = quantity
super.init(name: name)
}
}
// quantity ๊ฐ 1 ๋ฏธ๋ง์ด๊ฑฐ๋ name ์ด ๋น ๊ฒ์ ๋ฃ๊ณ ์ด๊ธฐํํ๋ฉด ์คํจ.
class Document {
var name: String?
// this initializer creates a document with a nil name value
init() {}
// this initializer creates a document with a nonempty name value
init?(name: String) {
if name.isEmpty { return nil }
self.name = name
}
}
class UntitledDocument: Document {
override init() {
// "" ์ผ๋ก ํธ์ถ๋์ง ์์ ๊ฒ์์ด ๋ณด์ฅ๋จ -> !
super.init(name: "[Untitled]")!
}
}
class SomeClass {
required init() {
// initializer implementation goes here
}
}
class SomeSubclass: SomeClass {
required init() {
// subclass implementation of the required initializer goes here
}
}
class VC: UIViewController {
let label: UILabel = {
let l = UILabel()
l.text= "text"
l.textColor = .blue
return l
}()
}
-> ๊ทธ๊ฒ์ ์ฌ์ฉ์๊ฐ ์ง์ ํ๊ธฐ ์ํด์ initializer๋ฅผ ์ ๊ณตํ๋๋ฐ...
init() {
// ์์๋์ง ์์ ๋ชจ๋ ํ๋กํผํฐ ์ด๊ธฐํ
super.init() // ์์๋ ๊ฐ ์ค์ ๋จ
// ์์๋ฐ์ ๋ชจ๋ ํ๋กํผํฐ ์ด๊ธฐํ
}
convenience init() {
self.init() // designated init
// ์ด๊ธฐํ ํ๊ณ ์ถ์ ๊ฐ ์ด๊ธฐํ.
}
์ฐธ๊ณ
https://bbiguduk.gitbook.io/swift/language-guide-1/initialization