{"id":13,"date":"2025-09-25T11:05:55","date_gmt":"2025-09-25T02:05:55","guid":{"rendered":"https:\/\/devserver.kr\/blog\/?p=13"},"modified":"2025-09-25T11:28:55","modified_gmt":"2025-09-25T02:28:55","slug":"swift-%eb%ac%b8%eb%b2%95-%ec%a0%95%eb%a6%ac","status":"publish","type":"post","link":"https:\/\/devserver.kr\/blog\/swift\/swift-%eb%ac%b8%eb%b2%95-%ec%a0%95%eb%a6%ac\/","title":{"rendered":"Swift \ubb38\ubc95 \uc815\ub9ac"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><h2>\ubcc0\uc218<\/h2>\n<ul>\n<li>let, var<\/li>\n<li>Int, Double, Bool, String, Array, Dictionary<\/li>\n<\/ul>\n<h2>Converting \uc740 \uba85\uc2dc\uc801\uc73c\ub85c!<\/h2>\n<pre><code class=\"language-swift\">let width = 94\nString(width)\n<\/code><\/pre>\n<h2>\ubb38\uc790\uc5f4 \uc548\uc5d0 \ubcc0\uc218\uac12<\/h2>\n<pre><code class=\"language-swift\">&quot;\\(width + height)&quot;\n<\/code><\/pre>\n<p>\uc5f0\uc0b0 \ud6c4, \ubb38\uc790\uc5f4\ub85c \ubcc0\ud658<\/p>\n<h2>\uc5ec\ub7ec\uc904\uc5d0 \uac78\uccd0 \ubb38\uc790\uc5f4 \uc815\uc758\ud560 \ub54c<\/h2>\n<pre><code class=\"language-swift\">let g = &quot;&quot;&quot;\nI said &quot;I have&quot;\n&quot;&quot;&quot;\n<\/code><\/pre>\n<p>\ud070\ub530\uc634\ud45c 3\uac1c<\/p>\n<h2>\ubc30\uc5f4<\/h2>\n<pre><code class=\"language-swift\">var arr = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]\nvar arr2 = [String]() \/\/ \ube48 \ubc30\uc5f4\nvar arr3 = [] \/\/ \ube48 \ubc30\uc5f4\narr.append(&quot;d&quot;) \/\/ \uc694\uc18c \ucd94\uac00\n<\/code><\/pre>\n<h2>\ub515\uc154\ub108\ub9ac<\/h2>\n<pre><code class=\"language-swift\">var dict = [\n  &quot;key1&quot; : &quot;value1&quot;,\n  &quot;key2&quot; : &quot;value2&quot;,\n]\ndict[&quot;key2&quot;] = &quot;newValue&quot;\nvar dict2 = [String:String]() \/\/ \ube48 \ub515\uc154\ub108\ub9ac\n<\/code><\/pre>\n<h2>Optional variables<\/h2>\n<p>\uac12\uc774 \uc788\uac70\ub098 nil \uc774\uac70\ub098\u2026<\/p>\n<pre><code class=\"language-swift\">var str: String? = nil\nvar str2:String = &quot;Hello&quot;\nprint(&quot;\\(str ?? str2)&quot;) \/\/ ?? \ub4a4 str2\ub294 str\uc774 nil \uc77c \uacbd\uc6b0 \uae30\ubcf8\uac12\n\nvar optionalString : String? = &quot;hello&quot;\nprint(optionalString == nil) \/\/ false \uac00 \ucc0d\ud78c\ub2e4.\n<\/code><\/pre>\n<h1>Control Flow<\/h1>\n<h2>for ~ in<\/h2>\n<pre><code class=\"language-swift\">\/\/ \ubc30\uc5f4\nlet arr = [1,2,3,4]\nfor score in arr {\n  ...\n}\n\n\/\/ \ub515\uc154\ub108\ub9ac\nlet dict = [\n  &quot;prime&quot; : [2,3,5,7,11,13],\n  &quot;Fibonacci&quot; : [1,1,2,3,5,8],\n]\nfor (kind, numbers) in dict {\n  for number in numbers {\n    ...\n  }\n}\n\/\/ \ub515\uc154\ub108\ub9ac\ub294 \uc21c\uc11c\uac00 \uc5c6\ub2e4. \uc21c\uc11c\ub300\ub85c \ub098\uc628\ub2e4\ub294 \ubcf4\uc7a5\uc774 \uc5c6\uc74c!\n\nfor i in 0..&lt;4 {\n  ...\n}\n\/\/ range 0,1,2,3\n<\/code><\/pre>\n<h2>while, repeat ~ while<\/h2>\n<pre><code class=\"language-swift\">while n &lt; 100 {\n  n *= 2\n}\n\nrepeat {\n  m *= 2\n} while m &lt; 100\n\n<\/code><\/pre>\n<h2>if<\/h2>\n<pre><code class=\"language-swift\">if \uc870\uac74 {\n  ...\n} else {\n  ...\n}\n<\/code><\/pre>\n<p>if optional \ubcc0\uc218<\/p>\n<pre><code class=\"language-swift\">if let name = optionalName {\n  ...\n}\n<\/code><\/pre>\n<p>optionalName \uc774 nil \uc774\uba74 \uc870\uac74\uc774 false\uac00 \ub41c\ub2e4. let\uc740 optionalName\uc744 unwrap \ud574\uc11c name\uc5d0 \ub123\ub294\ub2e4.<\/p>\n<h2>switch<\/h2>\n<pre><code class=\"language-swift\">switch vegetable {\n  case &quot;celery&quot; :\n    print(...)\n  case &quot;cucumber&quot; :\n    print(...)\n  case let x where x.hasSuffix(&quot;pepper&quot;) \/\/ \uc774\ub7f0 \uac83\ub3c4 \ub428!\n    print(...)\n  default : \/\/ default \uc5c6\uc73c\uba74 \uc5d0\ub7ec\n    print(...)\n}\n<\/code><\/pre>\n<p>break \uc5c6\uc5b4\ub3c4 \ub428!<\/p>\n<h1>\ud568\uc218<\/h1>\n<pre><code class=\"language-swift\">\/\/ \uc120\uc5b8\nfunc greet(person:String, day:String) -&gt; String {\n  return &quot;Hello \\(person), today is \\(day)&quot;\n}\ngreet(person:&quot;Bob&quot;, day:&quot;Tuesday&quot;)\n\nfunc greet(_ person:string, on day:String) -&gt; { \n\/\/ _ \ub97c \uc4f0\uba74 person \uc774\ub77c\ub294 \uc778\uc790\uc5c6\uc774 \ud638\ucd9c\uac00\ub2a5\n\/\/ on \uc740 \ud638\ucd9c\ud560 \ub54c \uc778\uc790\uac00 \ub428. day\ub294 \ud568\uc218 \ub0b4\ubd80 \ubcc0\uc218\n\ngreet(&quot;Bob&quot;, on:&quot;Tuesday&quot;)\n\n\/\/ \ud29c\ud50c\nfunc calc(scores:[Int]) -&gt; (min:Int, max:Int,sum:Int) {\n\nlet statistic = calc(score:[5,3,100,3,9])\nstatistic.sum\n\/\/ sum \uc740 \uc544\ub798\ucc98\ub7fc \uc811\uadfc\uac00\ub2a5\nstatistic.2\n\/\/ min \uc774 0, max \uac00 1, sum \uc774 2\n\n<\/code><\/pre>\n<h2>\ud568\uc218 \uc548\uc5d0 \ud568\uc218 \uc120\uc5b8 (nested)<\/h2>\n<pre><code class=\"language-swift\">func A() -&gt; Int {\n  var y = 10\n  func add() {\n    y += 5\n    }\n\n    add()\n    return y\n}\n<\/code><\/pre>\n<h2>\ud568\uc218\ub294 \ud568\uc218\ub97c \ub9ac\ud134\ud560 \uc218 \uc788\ub2e4.<\/h2>\n<pre><code class=\"language-swift\">func makeIncrementer() -&gt; ((Int)-&gt;Int) {\n  func addOne(number:Int) -&gt; Int {\n    return 1 + number\n  }\n}\nvar incrementer = makeIncrementer()\n\nincrementer(7)\n<\/code><\/pre>\n<h2>\ud568\uc218\ub97c \uc778\uc790\ub85c \ubc1b\uc744 \uc218 \uc788\ub2e4.<\/h2>\n<pre><code class=\"language-swift\">func hasAnyMatches(list:[Int], condition:(Int)-&gt;Bool) -&gt; Bool {\n\/\/ Int \ub97c \ubc1b\uc544 Bool \uc744 \ub9ac\ud134\ud558\ub294 \ud568\uc218\ub97c condition \uc5d0 \uc778\uc790\ub85c \uc904 \uc218 \uc788\ub2e4.\n<\/code><\/pre>\n<h1>Closure<\/h1>\n<p>closure : block of code<\/p>\n<pre><code class=\"language-swift\">numbers.map( { (number:Int) -&gt; Int in \n  let result = 3 * number\n  return result\n})\n\nnumbers.map({ number in 3 * number})\n\/\/ \uc778\uc790,\ub9ac\ud134\ud0c0\uc785 \uc0dd\ub7b5, return \uc0dd\ub7b5\n\/\/ \uc778\uc790\ub97c \uc774\ub984\uc774 \uc544\ub2cc \uc22b\uc790\ub85c \uc811\uadfc\uac00\ub2a5\n\/\/ numbers.sorted{ $0 &gt; $1 }\n<\/code><\/pre>\n<h1>Class<\/h1>\n<pre><code class=\"language-swift\">class Shape {\n  var a = 10\n  func aaa() -&gt; String {\n    return ...\n  }\n\n  \/\/ \uc0dd\uc131\uc790\n  init(name:String) {\n    self.name = name\n  }\n}\n\nvar s = Shape()\n\/\/ \uc811\uadfc\n\/\/ s.a\n\/\/ s.aaa()\n\n\/\/ deinit \ud568\uc218 : some clean-up \ubaa9\uc801\n\n<\/code><\/pre>\n<ul>\n<li>\uc0c1\uc18d<\/li>\n<\/ul>\n<p>super class\uc758 \ud568\uc218\ub97c override \ud558\ub824\uba74 \ud0a4\uc6cc\ub4dc override \uc0ac\uc6a9<\/p>\n<pre><code class=\"language-swift\">super.init(name:name) {\n\n<\/code><\/pre>\n<ul>\n<li>get, set<\/li>\n<\/ul>\n<pre><code class=\"language-swift\">class B {\n  var perimeter : Double {\n    get {\n      return 3.0 * sideLength\n    }\n\n    set {\n      sideLength = newValue \/ 3.0\n    }\n\n    \/\/ didSet, willSet \ub3c4 \uc788\uc74c\n  }\n\n}\n<\/code><\/pre>\n<h1>enum<\/h1>\n<pre><code class=\"language-swift\">enum Rank : Int {\n  case ace = 1\n  case two, three, four, five\n  case jack, queen, king\n\n  func simpleDescription() -&gt; String {\n    switch self {\n      case .ace : \n        return &quot;Ace&quot;\n      ...\n\n      default : \n        return String(self.rawValue)\n\n    }\n\n  }\n\n}\n<\/code><\/pre>\n<p>enum \uc758 \uc0dd\uc131\uc790<\/p>\n<pre><code class=\"language-swift\">if let convertedRank = Rank(rawValue: 3) {\n\n\n<\/code><\/pre>\n<h1>Struct<\/h1>\n<p>struct \ub294 copied, class \ub294 referenced<\/p>\n<h1>Protocol<\/h1>\n<pre><code class=\"language-swift\">protocol ExampleProtocol {\n  var simpleDescription: String { get }\n\n  mutating func adjust()\n\n}\n<\/code><\/pre>\n<p>struct \uc548\uc5d0\uc11c\ub294 mutating \uc73c\ub85c \ubc1b\uc544 \uc7ac\uc815\uc758, class \ub294 \uc548 \uadf8\ub798\ub3c4 \ub428.<\/p>\n<h1>Extension<\/h1>\n<p>\uae30\uc874 \ud0c0\uc785\uc5d0 \ud568\uc218 \ucd94\uac00\ud558\ub294 \ubc29\ubc95<\/p>\n<pre><code class=\"language-swift\">extension Int: ExampleProtocol {\n  var simpleDescription: String {\n    return &quot;number&quot;\n  }\n  mutating func adjust() {\n    ...\n  }\n\n}\n\n\/\/ Int \uc5d0 \ud568\uc218\uc640 \ubcc0\uc218\uac00 \ucd94\uac00\ub428\n\/\/ 7.simpleDescription \uc73c\ub85c &quot;number&quot; \uac12 \ubc1b\uc744 \uc218 \uc788\ub2e4.\n<\/code><\/pre>\n<h2>Optional Binding<\/h2>\n<pre><code class=\"language-swift\">if let actualNumber = Int(possibleNumber) {\n  print(&quot;The string \\&quot;\\(possibleNumber)\\&quot; has an integer value of \\(actualNumber)&quot;)\n} else {\n  print(&quot;The string \\&quot;\\(possibleNumber)\\&quot; could not be converted to an integer&quot;)\n}\n<\/code><\/pre>\n<pre><code class=\"language-swift\">if let firstNumber = Int(&quot;4&quot;), let secondNumber = Int(&quot;42&quot;), firstNumber &lt; secondNumber &amp;&amp; secondNumber &lt; 100 {\n    print(&quot;\\(firstNumber) &lt; \\(secondNumber) &lt; 100&quot;)\n}\n\/\/ Prints &quot;4 &lt; 42 &lt; 100&quot;\n\nif let firstNumber = Int(&quot;4&quot;) {\n    if let secondNumber = Int(&quot;42&quot;) {\n        if firstNumber &lt; secondNumber &amp;&amp; secondNumber &lt; 100 {\n            print(&quot;\\(firstNumber) &lt; \\(secondNumber) &lt; 100&quot;)\n        }\n    }\n}\n\/\/ Prints &quot;4 &lt; 42 &lt; 100&quot;\n<\/code><\/pre>\n<h2>Error Handling<\/h2>\n<pre><code class=\"language-swift\">enum PrintedError : Error {\n  case outOfPaper\n  case noToner\n}\n<\/code><\/pre>\n<pre><code class=\"language-swift\">func send(job:Int, name:String) throw -&gt; String {\n  if name == &quot;Never&quot; {\n    throw PrintedError.noToner\n  }\n  return &quot;Job sent&quot;\n}\n<\/code><\/pre>\n<p>do ~ catch \ub85c try \ud574\uc11c throw \ub97c \ubc1b\uc744 \uc218 \uc788\ub2e4.<\/p>\n<pre><code class=\"language-swift\">do {\n  let printerResp = try send(job:, name: &quot;Shen&quot;)\n  print(printResp)\n} catch {\n  print(error)\n}\n<\/code><\/pre>\n<p>case\ucc98\ub7fc \uc5d0\ub7ec\ub9c8\ub2e4 catch \uc0ac\uc6a9 \uac00\ub2a5<\/p>\n<pre><code class=\"language-swift\">catch PrintError.outOfPaper {\n  ...\n}\ncatch let printerError as PrinterError {\n ...\n}\n<\/code><\/pre>\n<pre><code class=\"language-swift\">try? send(...\n<\/code><\/pre>\n<p>\uacb0\uacfc\uac00 optional \uc774 \ub41c\ub2e4. throw \ubc1b\uc73c\uba74 \ubb34\uc2dc\ub418\uace0 \ub9ac\ud134\uac12\uc774 nil<\/p>\n<h3>defer<\/h3>\n<p>\ud568\uc218 \uc548 \ub2e4\ub978 \ucf54\ub4dc \ub2e4 \uc2e4\ud589\ub418\uace0 \ub09c \ud6c4(return \uc9c1\uc804) \uc2e4\ud589\ud558\ub77c\uace0<\/p>\n<h3>Generic<\/h3>\n<pre><code class=\"language-swift\">func makeArray&lt;Item&gt;(repeating item:Item, numberOfTimes:Int) -&gt;[Item] {\n...\n}\n<\/code><\/pre>\n<h3>public<\/h3>\n<h3>weak<\/h3>\n<h2>convenience int()<\/h2>\n<p>\uae30\ubcf8\uc801\uc73c\ub85c \ubc18\ub4dc\uc2dc \uc874\uc7ac\ud574\uc57c\ud558\ub294 init()\uc5d0 \ubcf4\uc870\uc801 \uc5ed\ud560\uc744 \ud558\ub294 \ucd08\uae30\ud654\ud568\uc218\n\ud074\ub798\uc2a4 \ub0b4\uc758 \ubcc0\uc218\ub97c \ucd08\uae30\ud654\ud558\uba74\uc11c \ub2e4\uc2dc init()\uc744 \ud638\ucd9c\ud560 \ub54c \uc0ac\uc6a9\ud55c\ub2e4.<\/p>\n<pre><code class=\"language-swift\">class Person {\n    var name: String\n    var age: Int\n    var gender: String\n\n    init(name: String, age: Int, gender: String) {\n        self.name = name\n        self.age = age\n        self.gender = gender\n    }\n\n    convenience init(age: Int, gender: String) {\n        self.init(name: &quot;zedd&quot;, age: age, gender: gender)\n    }\n\n}\n\n<\/code><\/pre>\n<h2>if let, guard let<\/h2>\n<pre><code class=\"language-swift\">\/\/if let\nfunc printName(){\n  var value:String?\n  value = &quot;Lena&quot;\n  print(value) \/\/ Optional(&quot;Lena&quot;)\n  if let name = value {\n    print(name) \/\/ &quot;Lena&quot;\n  } \n  \/\/if\ubb38 \uc548\uc5d0\uc11c\ub9cc name \ubcc0\uc218\ub97c \uc0ac\uc6a9 \uac00\ub2a5.\n}\n\n\/\/guard let\nfunc printName(){\n  var value:String?\n  value = &quot;Lena&quot;\n  print(value) \/\/ Optional(&quot;Lena&quot;)\n  guard let name = value else { return }\n  print(name) \/\/ &quot;Lena&quot;\n  \/\/name\ubcc0\uc218\ub294 \uba54\uc18c\ub4dc \ub0b4\uc5d0\uc11c \uc9c0\uc5ed\uc0c1\uc218\ucc98\ub7fc \uc0ac\uc6a9 \uac00\ub2a5.\n}\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-13","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/13","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/comments?post=13"}],"version-history":[{"count":4,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":22,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/22"}],"wp:attachment":[{"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devserver.kr\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}