๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

์ดˆ๋ฐ˜์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์•„๋Š” ์ง€์‹์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๊ฑฐ๋‚˜ ํŒจ์Šคํ•˜๊ณ  ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ์ง€์‹ ์œ„์ฃผ๋กœ ์ •๋ฆฌํ•ด ๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค!

 

 

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜ (Constants and Variables)

let maximumNumberOfLoginAttempts = 10 // ์ƒ์ˆ˜ -> ๊ฐ’ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ
var currentLoginAttempt = 0 // ๋ณ€์ˆ˜ -> ๊ฐ’ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
var x = 0.0, y = 0.0, z = 0.0

์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ, ํ•œ ์ค„๋กœ ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

let input = readLine()!.split(separator: " ").map { Int(String($0))! }
let height = input[0], width = input[1], k = input[2]

๊ฐœ์ธ์ ์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ input๊ฐ’์„ split๋กœ ์ชผ๊ฐ  ํ›„ ์„ ์–ธํ•  ๋•Œ ํ•œ ์ค„ ์„ ์–ธ์„ ๋งŽ์ด ์“ฐ๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

 

ํƒ€์ž… ๋ช…์‹œ (Type Annotations)

var welcomeMessage: String
  • ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์˜ ์ข…๋ฅ˜๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด ํƒ€์ž… ๋ช…์‹œ (Type Annotation) ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

โ“ ํƒ€์ž… ์–ด๋…ธํ…Œ์ด์…˜์„ ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ข‹์„๊นŒ?

  • ๋‹น์—ฐํ•œ๊ฑฐ์ง€๋งŒ ์„ ์–ธ๊ณผ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ตฌ๋ฌธ์ด ๋ถ„๋ฆฌ๋œ ๊ฒฝ์šฐ ํƒ€์ž… ์ถ”๋ก ์„ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž… ๋ช…์‹œ๋ฅผ ํ•ด์ค€๋‹ค.
  • ํƒ€์ž… ์ถ”๋ก ์œผ๋กœ ์–ป์–ด์ง€๋Š” ํƒ€์ž…์ด ์•„๋‹Œ, ๋‹ค๋ฅธ ํƒ€์ž…์„ ์ง์ ‘ ์ง€์ •ํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ
    • Int ํƒ€์ž… ๋Œ€์‹  Double์ด๋‚˜ Float ํƒ€์ž…
    • String ๋Œ€์‹  Character ํƒ€์ž…
  • ํ”„๋กœ์ ํŠธ ๊ทœ๋ชจ๊ฐ€ ์ปค์ง€๋ฉด → ์„ฑ๋Šฅ์ƒ์—์„œ ์ด์ ์ด ์žˆ๋‹ค..! ๋นŒ๋“œ ์†๋„ ์ฐจ์ด -> ํƒ€์ž… ๋ช…์‹œ๋ฅผ ์ง€ํ–ฅํ•˜์ž

 

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„ (Naming Constants and Variables)

let n = 3.14159
let ไฝ ๅฅฝ = "ไฝ ๅฅฝไธ–็•Œ"
let ๐Ÿถ๐Ÿฎ = "dogcow"
  • ์œ ๋‹ˆ์ฝ”๋“œ (Unicode) ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜์—ฌ ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์„ธ๋ฏธ์ฝœ๋ก  (Semicolons)

let cat = "๐Ÿฑ"; print(cat)
  • ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋‹ค๋ฅด๊ฒŒ Swift๋Š” ์ฝ”๋“œ์˜ ๊ฐ ๊ตฌ๋ฌธ ํ›„์— ์„ธ๋ฏธ์ฝœ๋ก  (;)์€ ํ•„์ˆ˜์กฐ๊ฑด์ด ์•„๋‹ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ์—ฌ๋Ÿฌ ๊ตฌ๋ฌธ์„ ํ•œ์ค„๋กœ ์ž‘์„ฑํ•  ๊ฒฝ์šฐ ์„ธ๋ฏธ์ฝœ๋ก ์€ ํ•„์ˆ˜

 

ํƒ€์ž… ์„ธ์ดํ”„ํ‹ฐ์™€ ํƒ€์ž… ์ถ”๋ก  (Type Safety and Type Inference)

  • Swift๋Š” *ํƒ€์ž…-์„ธ์ดํ”„ (type-safe)* ์–ธ์–ด → ๊ทธ๊ฒŒ ๋ญ”๋ฐ?
    • ์ฝ”๋“œ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์˜ ํƒ€์ž…์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
    • ์ปดํŒŒ์ผ ํ•  ๋•Œ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ํƒ€์ž…์„ ์˜ค๋ฅ˜๋กœ ํ‘œ์‹œํ•œ๋‹ค.
let meaningOfLife = 42 // -> Int๋ผ๊ณ  ํƒ€์ž… ์ถ”๋ก 

 

ํƒ€์ž… ๋ณ„์นญ (Type Aliases)

typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min // 0
  • ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํƒ€์ž…์„ ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์ •์˜

โ“ ์‹ค์ œ๋กœ ์–ด๋–ป๊ฒŒ ์“ฐ์ผ๊นŒ?

// Alamofire ๋‚ด๋ถ€ ์ฝ”๋“œ
public typealias Session = Alamofire.Session
internal typealias Request = Alamofire.Request
internal typealias DownloadRequest = Alamofire.DownloadRequest
internal typealias UploadRequest = Alamofire.UploadRequest
internal typealias DataRequest = Alamofire.DataRequest
  • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋งŒ๋“ค ๋•Œ ๋งŽ์ด ์“ด๋‹ค.

 

ํŠœํ”Œ (Tuples)

let http404Error = (404, "Not Found") // ํŠœํ”Œ -> (Int, String)ํƒ€์ž…
  • ์—ฌ๋Ÿฌ๊ฐ’์„ ๋‹จ์ผ ๋ณตํ•ฉ ๊ฐ’์œผ๋กœ ๊ทธ๋ฃนํ™”
let (statusCode, statusMessage) = http404Error
print("The status code is \\(statusCode)")
// Prints "The status code is 404"
print("The status message is \\(statusMessage)")
// Prints "The status message is Not Found"
  • ํ”Œ์˜ ๋‚ด์šฉ์„ ๋ณ„๋„์˜ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋กœ ๋ถ„ํ•ด (decompose) ํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์จ๋ณธ ์ ์ด ์—†๋Š” ๊ฒƒ ๊ฐ™๋‹ค..!

 

์˜ต์…”๋„ (Optionals)

  • ๊ฐ’์ด ์žˆ์–ด์„œ ์˜ต์…”๋„์„ ํ’€์–ด์„œ ๊ฐ’์„ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ or ๊ฐ’์ด ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค.
  • ์˜ต์…”๋„์ด ์•„๋‹Œ ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜์—๋Š” nil ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

 

๊ฐ•์ œ ์–ธ๋ž˜ํ•‘ Forced Unwrapping) & ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ (Optional Binding)

// ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘
if convertedNumber != nil {
    print("convertedNumber has an integer value of \\(convertedNumber!).")
}

// ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ
if let actualNumber = Int(possibleNumber) {
    print("The string \\"\\(possibleNumber)\\" has an integer value of \\(actualNumber)")
} else {
    print("The string \\"\\(possibleNumber)\\" could not be converted to an integer")
}
  • ! ํ‚ค์›Œ๋“œ๋กœ ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ตœ๋Œ€ํ•œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
    • ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ์“ฐ์ž!

 

if let ๊ตฌ๋ฌธ์—์„œ =๋ฅผ ์ƒ๋žตํ•˜๊ธฐ

et myNumber: Int? = 123
if let myNumber {
    print("My number is \\(myNumber)")
}

// Xcode 14 ์ด์ƒ๋ถ€ํ„ฐ๋งŒ error ์—†์ด ์ปดํŒŒ์ผ ๊ฐ€๋Šฅ

 

์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘ ๋œ ์˜ต์…”๋„ (Implicitly Unwrapped Optionals)

let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // requires an exclamation point

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // no need for an exclamation point
  • ์˜ต์…”๋„์„ ๋งŒ๋“ค๊ธฐ์œ„ํ•ด ํƒ€์ž…๋’ค์— ๋ฌผ์Œํ‘œ (String?)๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋Œ€์‹ ์— ๋Š๋‚Œํ‘œ (String!) ๋กœ ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘ ๋œ ์˜ต์…”๋„์„ ์ž‘์„ฑํ•œ๋‹ค.
  • ์™œ ์“ฐ๋Š”๊ฑฐ์•ผ..?
    • ์˜ต์…”๋„์ด ์ฒ˜์Œ ์ •์˜๋œ ์งํ›„์— ์˜ต์…”๋„์˜ ๊ฐ’์ด ์กด์žฌํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ™•์ธ๋˜๊ณ  ๊ทธ ์ดํ›„ ๋ชจ๋“  ์‹œ์ ์— ์กด์žฌํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉ

 

์—๋Ÿฌ ์ฒ˜๋ฆฌ (Error Handling)

func makeASandwich() throws {
    // ...
}

do {
    try makeASandwich()
    eatASandwich() // ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ
} catch SandwichError.outOfCleanDishes { // ๊นจ๋—ํ•œ ๊ทธ๋ฆ‡์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ ๋ฐœ์ƒ
    washDishes()
} catch SandwichError.missingIngredients(let ingredients) { // ์žฌ๋ฃŒ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ
    buyGroceries(ingredients)
}
  • ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ ์›์ธ์„ ํŒ๋ณ„ํ•˜๊ณ  ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ ํ”„๋กœ๊ทธ๋žจ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์œผ๋กœ ์ „ํŒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์—ญ์„ค๊ณผ ์ „์ œ์กฐ๊ฑด (Assertions and Preconditions)

  • ๋Ÿฐํƒ€์ž„์‹œ ๋ฐœ์ƒํ•˜๋Š” ์กฐ๊ฑด์ด๋‹ค.
  • ๊ฐ€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์ˆ˜์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์—ญ์„ค์„ ํ†ตํ•œ ๋””๋ฒ„๊น… (Debugging with Assertions)

let age = -3
// ์กฐ๊ฑด์ด true์ด๋ฉด ๊ณ„์† ์ง„ํ–‰, false๋ฉด ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
assert(age >= 0, "A person's age can't be less than zero.")
  • [assert(_:_:file:line:)] ํ•จ์ˆ˜๋กœ ์—ญ์„ค์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
if age > 10 {
    print("You can ride the roller-coaster or the ferris wheel.")
} else if age >= 0 {
    print("You can ride the ferris wheel.")
} else {
    assertionFailure("A person's age can't be less than zero.")
}
  • ์—ญ์„ค์ด ์‹คํŒจ๋˜์—ˆ๋Š”์ง€๋ฅผ ์•Œ๋ ค๋ฉด? → [assertionFailure(_:file:line:)] ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ

 

๊ฐ•์ œ ์ „์ œ์กฐ๊ฑด (Enforcing Preconditions)

// index > 0์ด false๋ฉด
precondition(index > 0, "Index must be greater than zero.")
  • [precondition(_:_:file:line:)] ํ•จ์ˆ˜๋กœ ์ „์ œ์กฐ๊ฑด์„ ์ž‘์„ฑ
  • ์กฐ๊ฑด์ด false ์ผ ๊ฒฝ์šฐ ์ถœ๋ ฅ๋  ๋ฉ”์„ธ์ง€๋ฅผ ์ „๋‹ฌ
profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...