๊ณต๋ถ€ ์—ฐ์Šต์žฅ :-)

TIL_20171120 ~ 20171125

|

์ด๋ฒˆ ์ฃผ์— ๋ฐฐ์šด / ๋Š๋‚€ ๊ฒƒ

  • ์Šค์œ„ํ”„ํŠธ ๋ฌธ๋ฒ• ๊ณต๋ถ€
    • enum
    • protocol
    • pattern - ํŒจํ„ด๊ณผ ํŒจํ„ด๋งค์นญ
    • trimmingCharacter
      • ์ž…๋ ฅ๋ฐ›๋Š” ์œ ํšจํ•œ ๊ฐ’์„ CharacterSet์œผ๋กœ ์ •ํ•ด๋†“๊ณ , trimmingCharacter๋ฅผ ์“ฐ๋‹ˆ๊นŒ ํŽธ๋ฆฌํ–ˆ์Œ! (์›๋ž˜๋Š” rangeOfCharacter๋กœ ํ•˜๋ ค๊ณ  ํ•˜๋‹ค๊ฐ€ ๊ณ ์ƒ๊ณ ์ƒ..)
  • ์ขŒํ‘œ๊ณ„์‚ฐ๊ธฐ
  • <2๋‹จ๊ณ„ ๋ฏธ์…˜ ์ง„ํ–‰>
    • Data ๊ฐ์ฒด์™€ ๋กœ์ง ๊ฐ์ฒด๋ฅผ ๋ถ„๋ฆฌํ•˜๋Š”๋ฐ ๋„ˆ๋ฌด ์ค‘์ ์„ ๋‘๋‹ค๋ณด๋‹ˆ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๊ฐ์ฒด๊ฐ€ ๋งŽ์ด ๋งŒ๋“ค์–ด์ง„ ๋ถ€๋ถ„๋„ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹คโ€ฆ
    • ๊ฐ์ฒด๋ฅผ ๋ถ„๋ฆฌํ•ด์„œ ์„ค๊ณ„ํ•˜๋Š” ๊ฒƒ์€ ์ข‹์€๋ฐ ๊ฐ์ฒด์™€ ํ•จ์ˆ˜์˜ ์—ญํ• ์„ ์ •ํ™•ํžˆ ๋ถ„๋ฆฌํ•˜๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค. (์—๋Ÿฌ์ฒดํฌํ•˜๋Š” ํ•จ์ˆ˜์—์„œ ๋ฐ”๋กœ ์ขŒํ‘œ๊ฐ’์„ ๋ฆฌํ„ดํ•ด๋ฒ„๋ฆฌ๊ณ โ€ฆ.์ด์ƒํ•จ. ๋ฆฌํŒฉํ† ๋งํ•˜๊ธฐ!)
  • ๋‹ค์–‘ํ•œ substring : ๊ด„ํ˜ธ๋ฅผ ์ž˜๋ผ๋‚ด๋Š” ๋ถ€๋ถ„์—์„œ ๋‹คํ–‰ํžˆ ์•ž, ๋’ค ๋ฌธ์ž์—ด์„ trimming ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์•Œ๊ฒŒ๋˜์–ด์„œ ์ผ๋Š”๋ฐ ๋ฌธ์ž์—ด์„ ์ž๋ฅด๋Š” ํ•จ์ˆ˜๋ฅผ ์ •๋ฆฌํ•ด๋‘์–ด์•ผ๊ฒ ๋‹ค.
    • let noBlanks = userInput.trimmingCharacters(in: ["(", ")"])
  • <3๋‹จ๊ณ„ ๋ฏธ์…˜ ์ง„ํ–‰>
    • ํ”„๋กœํ† ์ฝœ ์‚ฌ์šฉ
      • ์  ์ž…๋ ฅ์—์„œ ์ง์„ ์ž…๋ ฅ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ–ˆ๋Š”๋ฐ, ํ”„๋กœํ† ์ฝœ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ๋‹ค ์ง  ํ›„, ์ค‘๋ณต๋˜๋Š” ๋ถ€๋ถ„์ด ๋ˆˆ์œผ๋กœ ๋ณด์ด๋ฉด ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•˜๋‹ค๋ณด๋‹ˆ ๋‹ต๋‹ตํ•œ ๋ถ€๋ถ„์ด ๋งŽ์•„์„œ ๊ฒฐ๊ตญ ๊ทธ๋ฆผ์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ณ  ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.
    • ํŒจํ„ด๋งค์นญ ์‚ฌ์šฉ
      • ํŒจํ……๋งค์นญ์—์„œ ๋ฐฐ์šด ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์—๋Ÿฌ ์บ์น˜ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ค„์˜€๋‹ค.(main.swift์—์„œ)

TIL_20171117

|

์˜ค๋Š˜ ๋ฐฐ์šด/๋Š๋‚€ ๊ฒƒ

  • ์ขŒํ‘œ๊ณ„์‚ฐ๊ธฐ ์„ค๊ณ„ : CheckingInput, AdjustPoint ๊ฐ์ฒด ์ถ”๊ฐ€.
    • outputView์— ์ถœ๋ ฅ๋˜๋„๋ก ์—ฐ๊ฒฐ๋˜๋Š” ๋ถ€๋ถ„์ด ์ดํ•ด๊ฐ€ ์•ˆ๊ฐ€์„œ ๋จผ์ € MyPoint๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”๊ฑธ ํ™•์ธํ•˜๊ณ  ์„ธ๋ถ€์ ์ธ ์‚ฌํ•ญ์„ ์งœ๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ๋จผ์ € ์ž…๋ ฅ - ์ถœ๋ ฅ ์ด๋ผ๋Š” ํฐ ํ‹€์„ ์—ฐ์Šตํ•ด๋†“๊ณ  ์‹œ์ž‘ํ•œ๊ฒŒ ๋„์›€์ด ๋˜์—ˆ๋‹ค.
    • โ€œ์ž…๋ ฅ - ์ž…๋ ฅ๊ฐ’์ฒดํฌ - ์ขŒํ‘œ๋งŒ๋“ค๊ธฐ - ์ขŒํ‘œ๊ฐ’ ์กฐ์ •ํ•˜๊ธฐ - ์กฐ์ •ํ•œ ์ขŒํ‘œ๋งŒ๋“ค๊ธฐ - ์กฐ์ •ํ•œ ์ขŒํ‘œ๋กœ ์ถœ๋ ฅํ•˜๊ธฐโ€ ์ˆœ์œผ๋กœ ์„ค๊ณ„ํ–ˆ๋‹ค. ๋ฆฌ๋ทฐ๋ฐ›์œผ๋ฉด์„œ ์ˆ˜์ •ํ•  ์˜ˆ์ •!
  • ์Šค์œ„ํ”„ํŠธ enum ๊ฐœ๋…์ •๋ฆฌ ์‹œ์ž‘. (๋‹ค ์™„๋ฃŒํ•˜์ง€ ๋ชปํ–ˆ๋‹คโ€ฆ.)

TIL_20171116

|

์˜ค๋Š˜ ๋ฐฐ์šด / ๋Š๋‚€ ๊ฒƒ

  • ์‚ฌ๋‹ค๋ฆฌ ๊ฒŒ์ž„ ๋ฏธ์…˜ ์™„๋ฃŒ
    • ์œ ๋‹› ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ๋ถ€์กฑํ•จ. ๋” ์—ฐ์Šตํ•˜๊ธฐ
  • ์ขŒํ‘œ๊ณ„์‚ฐ๊ธฐ ๋ฏธ์…˜ ์‹œ์ž‘!
  • ์—ฐ์‚ฐํ˜• ํ”„๋กœํผํ‹ฐ getter and setter
  • ์˜ต์…”๋„

์˜ต์…”๋„์˜ ์ •๋ฆฌ - ์˜ต์…”๋„์˜ ๊ฐœ๋…, ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ, ์•”๋ฌต์  ์–ธ๋ž˜ํ•‘

|

์˜ต์…”๋„ Optional

์Šค์œ„ํ”„ํŠธ์˜ ์˜ต์…”๋„์€ ์ผ์ข…์˜ ์•ˆ์ „์žฅ์น˜์ด๋‹ค. ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ณณ์—์„œ nil์ด ๋ฐœ์ƒํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์ด ์˜ˆ๊ธฐ์น˜ ์•Š๊ฒŒ ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค. nil์ด ๋  ์ˆ˜ ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋Š” ๋ฐ˜๋“œ์‹œ ์˜ต์…”๋„ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•ด์•ผํ•œ๋‹ค.

๋ฐ˜๋Œ€๋กœ, ์˜ต์…”๋„ํƒ€์ž…์œผ๋กœ ์„ ์–ธ๋˜์–ด์žˆ์ง€ ์•Š์€ ์ธ์Šคํ„ด์Šค๋Š” nil์ด ๋˜์ง€ ๋ชปํ•œ๋‹ค! (์ผ๋ฐ˜ ์ž๋ฃŒํ˜•์— nil์€ ์–ต์ง€๋กœ ๋Œ€์ž…ํ•  ์ˆ˜๋„ ์—†์Œ)

์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ

if let์—์„œ ์–ธ๋ž˜ํ•‘๊ณผ์ •์ด ์•”๋ฌต์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜์–ด ์˜ต์…”๋„์˜ ๊ฐ’์ด ์ž„์‹œ ์ƒ์ˆ˜๋กœ ์ง€์ •๋œ๋‹ค.(theError)

var errorCodeString: String?
errorCodeString = "404"

if let theError = errorCodeString {
    print(theError)
}
//errorCodeString์ด nil์ด ์•„๋‹ˆ๋ฉด theError์— ๊ฐ’์„ ๋Œ€์ž…ํ•˜๊ณ  ์ถœ๋ ฅํ•ด๋ผ.

/*
์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ
if let tempConstant = anOptional {
    //tempConstant๊ฐ€ nil์ด ์•„๋‹ˆ๋ฉด ์–ด๋–ค ์ผ์„ ํ•œ๋‹ค.
} else {
    //anOptional์—๋Š” ๊ฐ’์ด ์—†๋‹ค.(nil์ด๋‹ค.)
}
*/
์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ ์ค‘์ฒฉ
var errorCodeString: String?
errorCodeString = "404"

if let theError = errorCodeString {
    if let errorCodeInteger = Int(theError) {
        print("\(theError): \(errorCodeInteger)")
    }
}

์—ฌ๋Ÿฌ ์˜ต์…”๋„ ์–ธ๋ž˜ํ•‘ & ์ถ”๊ฐ€ํŒ๋‹จ

์˜ต์…”๋„๋ฐ”์ธ๋”ฉ์„ ์ค‘์ฒฉํ•˜์ง€์•Š๊ณ  ์—ฌ๋Ÿฌ ์˜ต์…”๋„ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

if let theError = errorCodeString, let errorCodeInteger = Int(theError) {
        print("\(theError): \(errorCodeInteger)")
    }

๋จผ์ € errorCodeString์„ ์–ธ๋ž˜ํ•‘ํ•˜์—ฌ ๊ทธ ๊ฐ’์„ theError์— ์ €์žฅํ•˜๊ณ , Int์ธ์Šคํ„ด์Šค๋กœ ๋ณ€ํ™˜ํ–ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋Š” ์˜ต์…”๋„์ด๊ธฐ๋•Œ๋ฌธ์—(?) ์–ธ๋ž˜ํ•‘ํ•˜์—ฌ ๊ทธ ๊ฐ’์„ errorCodeInteger๋กœ ๋ฐ”์ธ๋”ฉํ–ˆ๋‹ค.

  • ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ ์˜ต์…”๋„์„ ๋ฐ”์ธ๋”ฉ ํ•˜๋Š” ๊ตฌ๋ฌธ์—์„œ๋Š” ๋‘ ๋ฐ”์ธ๋”ฉ์—์„œ ํ•œ ๋ฒˆ์ด๋ผ๋„ nil์ด ๋ฆฌํ„ด๋˜๋ฉด ์กฐ๊ฑด๋ฌธ์˜ ์‹คํ–‰ ์ฝ”๋“œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ์ถ”๊ฐ€ ํŒ๋‹จ์€ errorCodeInteger == 404๋ถ€๋ถ„์ธ๋ฐ, ์ด๋Š” ์•ž์˜ ๋‘ ์˜ต์…”๋„์ด ์–ธ๋ž˜ํ•‘๋˜์–ด์„œ nil์ด ๋ฆฌํ„ด๋˜์ง€ ์•Š๋Š” ์กฐ๊ฑด, ์ดํ›„์— ์ถ”๊ฐ€๋กœ errorCodeInteger๊ฐ€ 404์ธ ์กฐ๊ฑด์„ ๋œปํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ if๋ฌธ์˜ ๋ชจ๋“  ์กฐ๊ฑด์ด ๋งŒ์กฑํ•  ์‹œ์— ๊ทธ ๋‹ค์Œ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ๊ณผ ์ถ”๊ฐ€ ํŒ๋‹จ์กฐ๊ฑด์„ ๊ฐ™์ด ,์™€ ํ•จ๊ป˜ ์จ์ฃผ์–ด๋„ ๋ฌธ๋ฒ•์— ๋งž๋Š”๋‹ค๋Š”๊ฒŒ ํŠน์ง•์ธ ๊ฒƒ ๊ฐ™๋‹ค.

์˜ต์…”๋„ ์ฒด์ด๋‹

var errorCodeString: String?
errorCodeString = "404"
var errorDescription: String?

if let theError = errorCodeString, let errorCodeInteger = Int(theError), errorCodeInteger == 404 {
    errorDescription = "\(errorCodeInteger + 200): resource was not found."
} //errorDescription์˜ ํƒ€์ž…์€ String? ์ด๋‹ค.

var upCaseErrorDescription = errorDescription?.uppercased()
//"604: RESOURCE WAS NOT FOUND."
errorDescription
//"604: resource was not found."

<์ฝ”๋“œ์„ค๋ช…> errorCodeString์€ String?ํƒ€์ž…์ด๋‹ค. theError์— errorCodeString์ด nil์ด ์•„๋‹ˆ๋ฉด ๊ฐ’์„ ๋Œ€์ž…ํžŒ๋‹ค. theError์˜ ํƒ€์ž…์€ String์ด๋‹ค. ์ด๋ฅผ Int๋กœ ๋ณ€ํ™˜ํ•ด์„œ errorCodeInteger์— ๋Œ€์ž…ํ•œ๋‹ค. (errorCodeInteger์˜ ํƒ€์ž…์€ Int์ด๋‹ค.) ๋งŒ์•ฝ ๊ทธ errorCodeInteger์˜ ๊ฐ’์ด 404์ด๋ฉด ๊ทธ ๋‹ค์Œ์˜ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

upCaseErrorDescription๋Š” ์—๋Ÿฌ๋ฉ”์‹œ์ง€(errorDescription , String?ํƒ€์ž…)์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ํ‘œํ˜„ํ•˜๋Š” ์ธ์Šคํ„ด์Šค์ด๋‹ค. errorDescription์— ๋ถ™์€ ?๋Š” ์˜ต์…”๋„ ์ฒด์ด๋‹์ด๋‹ค.

errorDescription์— ๊ฐ’์ด ์—†์„๋•Œ๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•  ๋ฌธ์ž๊ฐ€ ์—†์œผ๋ฏ€๋กœ upCaseErrorDescription์ด nil๋กœ ์„ค์ •๋œ๋‹ค. ์ด ์‹œ์ ์—์„œ ์˜ต์…”๋„ ์ฒด์ด๋‹์— ๋”ฐ๋ผ ์˜ต์…”๋„์ด ๋ฆฌํ„ด๋œ๋‹ค. ํ•˜์ง€๋งŒ errorDescription์— ๊ฐ’์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„ค๋ช… ํ…์ŠคํŠธ๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜ํ–ˆ๊ณ , ๋ณ€ํ™˜๋œ ๊ฒฐ๊ณผ๋ฅผ upCaseErrorDescription์— ์ง€์ •ํ–ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒฐ๊ณผ๋Š” ๋Œ€๋ฌธ์ž๋กœ ๋ฐ˜ํ™˜๋œ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

nil ๊ฒฐํ•ฉ ์—ฐ์‚ฐ์ž

์˜ต์…”๋„์—์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ ์˜ต์…”๋„ ๊ฐ’์ด nil์ด๋ฉด ์–ด๋–ค ๋™์ž‘์„ ํ•ด๋ผ ๋ผ๋Š” ๋‹จ์ˆœํ•œ ์—ฐ์‚ฐ (ex. print(โ€œErrorโ€)๊ฐ™์€ ๋‹จ์ˆœํ•œ ๋™์ž‘์ด else๋ฌธ ๋’ค์— ์˜ค๋Š” ๊ฒƒ)์ผ๋•Œ ??๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํžˆ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.

let description: String
if let errorDescription = errorDescription {
  description = errorDescription
} else {
  description = "No Error"
}
// ๋‹จ์ˆœํ•œ ์—ฐ์‚ฐ์ธ๋ฐ๋„ ์ฝ”๋“œ๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๋‹ค.

์ด ์ฝ”๋“œ๋Š” ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•œ๋‹ค!

let description = errorDescription ?? "No Error"

??์˜ ์™ผ์ชฝ์—๋Š” ์˜ต์…”๋„์ด ์™€์•ผํ•˜๊ณ , ์˜ค๋ฅธ์ชฝ์€ ๋น„์˜ต์…”๋„ํƒ€์ž…์ด ์™€์•ผํ•œ๋‹ค. errorDescription์€ ์˜ต์…”๋„ํƒ€์ž…. ์™ผ์ชฝ ์˜ต์…”๋„์ด nil์ด๋ฉด ??๋Š” ์˜ค๋ฅธ์ชฝ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. ์™ผ์ชฝ ์˜ต์…”๋„์ด nil์ด ์•„๋‹ˆ๋ฉด ์˜ต์…”๋„์— ํฌํ•จ๋œ ๊ฐ’์ด ๋ฆฌํ„ด๋œ๋‹ค!

errorDescription = nil
let description = errorDescription ?? "No Error"
//๊ฒฐ๊ณผ ์‚ฌ์ด๋“œ๋ฐ”์—๋Š” "No Error"๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค.

ํ”„๋กœํผํ‹ฐ-์—ฐ์‚ฐํ˜• ํ”„๋กœํผํ‹ฐ-getter, setter

|

ํ”„๋กœํผํ‹ฐ์˜ ์‚ฌ์šฉ

์—ฐ์‚ฐํ˜• ํ”„๋กœํผํ‹ฐ

์—ฐ์‚ฐํ˜• ํ”„๋กœํผํ‹ฐ๋Š” ์ง€๊ธˆ๊นŒ์ง€ ๋‹ค๋ฃฌ ํ”„๋กœํผํ‹ฐ์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๊ฐ’์„ ์ €์žฅํ•˜์ง€ ์•Š๊ณ , getter์™€ setter๋ผ๋Š” ๊ฒƒ์„ ์ œ๊ณตํ•œ๋‹ค. ์ด ๋•Œ๋ฌธ์— ๊ณ„์‚ฐํ˜• ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ๋  ์ˆ˜ ์žˆ๋‹ค.

  • getter : ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ด
  • setter : ๊ฐ’์„ ์„ค์ •ํ•จ (๋‹จ, setter๋Š” ํ•„์ˆ˜ ์š”์†Œ๊ฐ€ ์•„๋‹ˆ๋‹ค.)

๋จผ์ € ์ „์ฒด ์˜ˆ์‹œ์ฝ”๋“œ

//main.swift

struct Point {
    var x = 0.0, y = 0.0
}

struct Size {
    var width = 0.0, height = 0.0
}

struct Rect {
    var origin = Point()
    var size = Size()
    var center : Point {
        get {
            let centerX = origin.x + (size.width / 2)
            let centerY = origin.y + (size.height / 2)
            return Point(x: centerX, y: centerY)
            //center๋Š” Pointํƒ€์ž…์ด๊ณ  get์•ˆ์˜ ๋กœ์ง์— ๋”ฐ๋ผ ์—ฐ์‚ฐ๋œ ๊ฐ’์˜ x,y
        } set(newCenter) {
            origin.x = newCenter.x - (size.width / 2)
            origin.y = newCenter.y - (size.height / 2)
        } //newCenter๋Š” setter์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ™์€ ๊ฒƒ์ด๋‹ค.
    }
}

var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width:10.0, height: 10.0))
let initialSquareCenter = square.center 
square.center = Point(x: 15.0, y: 15.0) 

print ("initialSquareCenter is now at (\(initialSquareCenter.x), \(initialSquareCenter.y))")
print("square.origin is now at (\(square.origin.x),\(square.origin.y))")


/*
๊ฒฐ๊ณผ : 
initialSquareCenter is now at (5.0, 5.0)
square.origin is now at (10.0,10.0)
*/


getter

var center : Point {
get {
            let centerX = origin.x + (size.width / 2)
            let centerY = origin.y + (size.height / 2)
            return Point(x: centerX, y: centerY)
            //center๋Š” Pointํƒ€์ž…์ด๊ณ  get์•ˆ์˜ ๋กœ์ง์— ๋”ฐ๋ผ ์—ฐ์‚ฐ๋œ ๊ฐ’์˜ x,y
        }
   ...
   
var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width:10.0, height: 10.0))
let initialSquareCenter = square.center
  • var center : Point {}์ฒ˜๋Ÿผ ๊ณ„์‚ฐํ˜• ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ• ๋•Œ๋Š” ํƒ€์ž…์„ ๊ผญ ๋ช…์‹œํ•ด์•ผ ํ•จ. ์—ฌ๊ธฐ์„œ center๋ผ๋Š” ๊ณ„์‚ฐํ˜•ํ”„๋กœํผํ‹ฐ๋Š” Pointํƒ€์ž…์ด๋ผ๋Š”๊ฒƒ์„ ๋ช…์‹œ.

  • getter : Pointํƒ€์ž…์˜ value๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ , getter๋งŒ์œผ๋กœ ๊ฐ’์ด ๋ณต์‚ฌ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹˜! (์—ฐ์‚ฐ๋งŒ ํ•˜๋Š” ๊ฒƒ)

  • square ์˜ ์ด๋ฆ„์œผ๋กœ Rectํƒ€์ž… ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŒ๋“ค์–ด์กŒ๋‹ค. ์ด๋•Œ ์ดˆ๊ธฐํ™”๋œ ๊ฐ’์€ origin (0,0), size(10,10)

  • square๋ผ๋Š” ์ธ์Šคํ„ด์Šค์˜ count๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๋Š” ์—ฐ์‚ฐํ˜• ํ”„๋กœํผํ‹ฐ์ด๋‹ค. ๋Œ€์ž…์—ฐ์‚ฐ์ž = ์˜†์— ์—ฐ์‚ฐํ˜•ํ”„๋กœํผํ‹ฐ๋ฅผ ํ˜ธ์ถœํ•˜๋Š”๊ฒŒ ์žˆ์œผ๋ฉด getter๋ฅผ ๋ถ€๋ฅด๋Š” ๊ฒƒ์ธ๋ฐ, let initialSquareCenter = square.center ๋Š” center์˜ get์•ˆ์˜ ๋กœ์ง์— ๋”ฐ๋ผ ์—ฐ์‚ฐ๋œ ๊ฐ’์ด initialSquareCenter์— ๊ฐ’์„ ๋ณต์‚ฌํ•œ๋‹ค๋Š” ๋œป์ž„

    โ€‹

setter

var center : Point {
        get {
            let centerX = origin.x + (size.width / 2)
            let centerY = origin.y + (size.height / 2)
            return Point(x: centerX, y: centerY)
        } set(newCenter) {
            origin.x = newCenter.x - (size.width / 2)
            origin.y = newCenter.y - (size.width / 2)
        }
        //setter์—์„œ, set(newCenter) = func setCenter(newCenter: Point) ์™€ ๊ฐ™์€๋œป. newCenter๋Š” set์˜ ๋งค๊ฐœ๋ณ€์ˆ˜
    }
}
...

var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width:10.0, height: 10.0))

...

square.center = Point(x: 15.0, y: 15.0)


  • set(newCenter) ๋Š” set(newCenter) = func setCenter(newCenter: Point)์ฒ˜๋Ÿผ ์ž‘๋™ํ•จ. (newCenter๋Š” setter์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์ž‘๋™)
  • square.center = Point(x: 15.0, y: 15.0) ์ฒ˜๋Ÿผ ์™ผ์ชฝ์— square.center๊ฐ€ ์žˆ์œผ๋ฉด set์˜ ๋กœ์ง์— ๋”ฐ๋ผ newCenter์— (x: 15.0, y: 15.0)์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋“ค์–ด๊ฐ€์„œ ๊ณ„์‚ฐ๋œ ๊ฐ’์ด origin.x / y์— ๋ณต์‚ฌ