{"id":2953,"date":"2025-11-29T12:46:29","date_gmt":"2025-11-29T03:46:29","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2953"},"modified":"2025-11-29T13:43:29","modified_gmt":"2025-11-29T04:43:29","slug":"swift-guard-and-if","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/11\/29\/swift-guard-and-if\/","title":{"rendered":"[Swift] Guard and If"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud2b8\ud504\uc758 guard\ubb38\ubc95\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.<br>This is an explanation of the guard syntax in Sweets.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb guard\ub300\uc2e0 if\ub97c \uc0ac\uc6a9\ud574\ub3c4\ub429\ub2c8\ub2e4.<br>You can use if instead of guard.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb gurard\ub97c \uc0ac\uc6a9\ud560 \uacbd\uc6b0 \ucf54\ub4dc \uc77d\uae30\uac00 \ub354 \uc26c\uc6cc\uc9d1\ub2c8\ub2e4.<br>Using gurard makes your code easier to read.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb guard\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \uad6c\uc870\ub97c \uac00\uc9d1\ub2c8\ub2e4.<br>The guard has the following structure:<\/p>\n\n\n\n<p>\u2714\ufe0f guard\uc758 \uc870\uac74\uacfc \uac19\uc73c\uba74 \ud1b5\uacfc\ud558\uace0 \uc870\uac74\uacfc \ub2e4\ub974\uba74 else \ube14\ub7ed\uc744 \uc2e4\ud589\ud569\ub2c8\ub2e4.<br>If it matches the guard condition, it passes, and if it doesn&#8217;t, the else block is executed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>guard \uc870\uac74 &#91; condition ] else {\n    \/\/ \uc870\uac74\uc774 false\uc77c \ub54c \uc2e4\ud589\ub418\ub294 \ubd80\ubd84\n    \/\/ The part that runs when the condition is false\n\n    \/\/ \ubcf4\ud1b5 return, break, continue, throw \ub4f1\uc73c\ub85c \ud0c8\ucd9c\n    \/\/ Usually escape with return, break, continue, throw, etc.\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \/ Code<\/p>\n\n\n\n<p>\u2714\ufe0f \uc544\ub798\ub294 guard\ub97c \uc0ac\uc6a9\ud558\ub294 \uc571\uc758 \ucf54\ub4dc \uc785\ub2c8\ub2e4.<br>Below is the code for an app that uses guard.<\/p>\n\n\n\n<p>\u2714\ufe0f UI\uc5d0 \uac12\uc744 \ud45c\uc2dc\ud558\ub824\uba74 .onAppear(){} \uc548\uc5d0 \ud568\uc218\ub97c \uc2e4\ud589\ud569\ub2c8\ub2e4.<br>To display a value in the UI, run a function inside .onAppear(){}.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport SwiftUI\n\nstruct ContentView: View {\n    @State var text:String = \"\"\n    \n    func process(_ value: Int?) {\n        \n        \/\/ \ud574\ub2f9 \uc870\uac74\uacfc \ub2e4\ub974\uba74 \ubaa8\ub450 \ud1b5\uacfc \ub429\ub2c8\ub2e4.\n        \/\/ If it is different from the condition, everything passes.\n        guard let value = value else { return }\n        guard value &gt; 0 else { return }\n        guard value != 999 else {  return }\n\n        \/\/ \uc704\uc758 guard\ub97c \ubaa8\ub450 \ud1b5\uacfc\uc2dc \ub2e4\uc74c \ubd80\ubd84\uc744 \ucc98\ub9ac \ud569\ub2c8\ub2e4.\n        \/\/ If all of the guards above are passed, the next part is processed.\n        print(\"Print Value:\", value)\n        text = \"\\(value)\"\n    }\n    \n    init(){\n        \n        \/\/ \uc81c\uc77c \ucc98\uc74c\uc2e4\ud589\ub418\uba70 \uc5ec\uae30\uc11c \ud568\uc218\uc2e4\ud589\uc2dc ui\uc5d0 \ud45c\uc2dc\ub418\uc9c0 \uc54a\uc74c\n        \/\/ It is executed first and is not displayed in the UI when the function is executed here.\n        \n        \/\/ process(Int(2));\n    }\n\n\n    var body: some View {\n        VStack {\n            Image(systemName: \"globe\")\n                .imageScale(.large)\n                .foregroundStyle(.tint)\n            \n            Text(\"Text1:\\(text)\")\n\n        }\n        .padding()\n        .onAppear(){\n            \n            \/\/ ui\uc5d0 \uac12\uc744 \uc815\uc0c1\uc801\uc73c\ub85c \ud45c\uc2dc\ud558\ub824\uba74 \uc5ec\uae30\uc5d0 \ud45c\uc2dc\ud560 \uac83\n            \/\/ If you want to display the value normally in the ui, display it here\n            \n            process(Int(2))\n        }\n    }\n}\n\n#Preview {\n    ContentView()\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\ud06c\ub9b0 \uc0f7 \/ ScreenShot<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"[Swift,Kotlin]Guard and If\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/YfTW_MC0YC4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud2b8\ud504\uc758 guard\ubb38\ubc95\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.This is an explanation of the guard syntax in Sweets. \ud83d\udc49\ud83c\udffb guard\ub300\uc2e0 if\ub97c \uc0ac\uc6a9\ud574\ub3c4\ub429\ub2c8\ub2e4.You can use if instead of guard. \ud83d\udc49\ud83c\udffb gurard\ub97c \uc0ac\uc6a9\ud560 \uacbd\uc6b0 \ucf54\ub4dc \uc77d\uae30\uac00 \ub354 \uc26c\uc6cc\uc9d1\ub2c8\ub2e4.Using gurard makes your code easier to read. \ud83d\udc49\ud83c\udffb guard\ub294 \ub2e4\uc74c\uacfc \uac19\uc740 \uad6c\uc870\ub97c \uac00\uc9d1\ub2c8\ub2e4.The guard has the following structure: \u2714\ufe0f guard\uc758 \uc870\uac74\uacfc \uac19\uc73c\uba74 \ud1b5\uacfc\ud558\uace0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,1],"tags":[],"class_list":["post-2953","post","type-post","status-publish","format-standard","hentry","category-swift","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=2953"}],"version-history":[{"count":9,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2953\/revisions"}],"predecessor-version":[{"id":2971,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2953\/revisions\/2971"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}