{"id":2963,"date":"2025-11-29T13:02:20","date_gmt":"2025-11-29T04:02:20","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=2963"},"modified":"2025-11-29T13:43:19","modified_gmt":"2025-11-29T04:43:19","slug":"kotlin-guard-and-if","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/11\/29\/kotlin-guard-and-if\/","title":{"rendered":"[Kotlin]Guard and If"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud504\ud2b8\uc758 Guard\uc640 \ucf54\ud2c0\ub9b0\uc758 If\ub97c \ube44\uad50\ud558\uae30 \uc704\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.<br>Here&#8217;s a comparison between Swift&#8217;s Guard and Kotlin&#8217;s If.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ud2c0\ub9b0\uc5d0\ub294 Guard\ubb38\ubc95\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.<br>There is no guard syntax in Kotlin.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud504\ud2b8\uc758 Guard\ubb38\ubc95\uacfc IF\uac00 \uc5b4\ub5bb\uac8c \ub2e4\ub978\uc9c0 \ube44\uad50\ud574 \ubcf4\uc138\uc694<br>Compare how Guard and IF statements differ in Swift.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ud2c0\ub9b0\uc5d0\uc11c\ub294 \uc2a4\uc704\ud504\ud2b8\uc758 Guard\ub97c \ub2e4\uc74c\uacfc \uac19\uc774 If\ubb38\uc73c\ub85c \ud45c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>In Kotlin, Swift&#8217;s Guard can be expressed as an If statement as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        val v = value ?: return      \/\/ guard let\n        if (v &lt;= 0) return           \/\/ guard value &gt; 0\n        if (v == 999) return         \/\/ guard value != 999<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\ucf54\ub4dc \/ Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package com.freelifemakers.guardif\n\nimport android.os.Bundle\nimport androidx.activity.ComponentActivity\nimport androidx.activity.compose.setContent\nimport androidx.activity.enableEdgeToEdge\nimport androidx.compose.foundation.layout.*\nimport androidx.compose.material.icons.Icons\nimport androidx.compose.material.icons.filled.Build\nimport androidx.compose.material3.*\nimport androidx.compose.runtime.*\nimport androidx.compose.ui.Alignment\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.tooling.preview.Preview\nimport androidx.compose.ui.unit.dp\nimport com.freelifemakers.guardif.ui.theme.GuardIfTheme\n\nclass MainActivity : ComponentActivity() {\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        enableEdgeToEdge()\n        setContent {\n            GuardIfTheme {\n                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -&gt;\n                    ContentView(modifier = Modifier.padding(innerPadding))\n                }\n            }\n        }\n    }\n}\n\n@Composable\nfun ContentView(modifier: Modifier = Modifier) {\n\n    \/\/ SwiftUI\uc758 @State\n    var text by remember { mutableStateOf(\"\") }\n\n    \/\/ Swift guard \ud328\ud134 \u2192 Kotlin if + early return\n    fun process(value: Int?) {\n        val v = value ?: return      \/\/ guard let\n        if (v &lt;= 0) return           \/\/ guard value &gt; 0\n        if (v == 999) return         \/\/ guard value != 999\n\n        println(\"Print Value: $v\")\n        text = \"$v\"\n    }\n\n    \/\/ \uc544\ub798\ucc98\ub7fc \uc2e4\ud589\ud574\ub3c4 \ub3d9\uc791\ud569\ub2c8\ub2e4.\n    \/\/ It also works if you run it like below.\n\n    \/\/ process(1)\n\n    \/\/ SwiftUI onAppear \u2192 Compose LaunchedEffect\n    LaunchedEffect(Unit) {\n        process(2)\n    }\n\n    \/\/ UI\n    Column(\n        modifier = modifier\n            .fillMaxSize()\n            .padding(16.dp),\n        verticalArrangement = Arrangement.Center, \/\/ \uc218\uc9c1 \uc911\uc559 \uc815\ub82c \/ vertical center alignment\n        horizontalAlignment = Alignment.CenterHorizontally  \/\/ \uc218\ud3c9 \uc911\uc559 \uc815\ub82c \/ horizontal center alignment\n    ) {\n        Icon(\n            imageVector = Icons.Default.Build,\n            contentDescription = \"Globe\"\n        )\n        Spacer(modifier = Modifier.height(16.dp))\n        Text(text = \"Text1: $text\")\n    }\n}\n\n@Preview(showBackground = true)\n@Composable\nfun ContentViewPreview() {\n    GuardIfTheme {\n        ContentView()\n    }\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\ud504\ud2b8\uc758 Guard\uc640 \ucf54\ud2c0\ub9b0\uc758 If\ub97c \ube44\uad50\ud558\uae30 \uc704\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.Here&#8217;s a comparison between Swift&#8217;s Guard and Kotlin&#8217;s If. \ud83d\udc49\ud83c\udffb \ucf54\ud2c0\ub9b0\uc5d0\ub294 Guard\ubb38\ubc95\uc774 \uc5c6\uc2b5\ub2c8\ub2e4.There is no guard syntax in Kotlin. \ud83d\udc49\ud83c\udffb \uc2a4\uc704\ud504\ud2b8\uc758 Guard\ubb38\ubc95\uacfc IF\uac00 \uc5b4\ub5bb\uac8c \ub2e4\ub978\uc9c0 \ube44\uad50\ud574 \ubcf4\uc138\uc694Compare how Guard and IF statements differ in Swift. \ud83d\udc49\ud83c\udffb \ucf54\ud2c0\ub9b0\uc5d0\uc11c\ub294 \uc2a4\uc704\ud504\ud2b8\uc758 Guard\ub97c \ub2e4\uc74c\uacfc \uac19\uc774 If\ubb38\uc73c\ub85c \ud45c\ud604\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.In Kotlin, Swift&#8217;s Guard [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,1],"tags":[],"class_list":["post-2963","post","type-post","status-publish","format-standard","hentry","category-kotlin","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2963","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=2963"}],"version-history":[{"count":6,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2963\/revisions"}],"predecessor-version":[{"id":2970,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/2963\/revisions\/2970"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=2963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=2963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=2963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}