티스토리 뷰
저는 지금은 주로 iOS만 개발하기 때문에 guard를 잘 사용합니다.
그럼 안드로이드에서는 guard가 없는데 어떻게 사용하면 guard처럼 사용할 수 있을까요?
1. if
그냥 손쉽게 사용하는 방법입니다.
Android Studio 안에서는 smart기능이라고 하죠...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (person == null) return | |
Log.d("test", person) // person is String? type |
2. let
null이 아닐 경우에 접근하여서 값을 가져오는 방법입니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person: String? = null | |
val good = person?.let { it } ?: return | |
// Look Like Swift guard. -> let good = person else { return } |
3. apply
let의 경우와 비슷합니다.
거의 동일하다고 봐도 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var person: String? = null | |
val good = person?.apply { print(this) } ?: return | |
// apply block in set Class values |
4. guard처럼 씁니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val number = number ?: return |
guard처럼 inline fun을 만들어 보았지만 리턴 타입을 제대로 만들지 못하여서 그렇게는 되지 않는것 같습니다.
각 언어에 맞는 방법으로 null을 잘 바인딩해서 사용하여 봅시다.
'Mobile > Android' 카테고리의 다른 글
Android. gradle build fail. 'No matching client found for package name' (0) | 2019.06.17 |
---|---|
Android. kotlin. static을 어떻게 쓰면 좋을까? (0) | 2019.06.15 |
Android. minSdkVersion, targetSdkVersion 이란? (0) | 2019.06.15 |
Android. Android Studio 배포 기준별 설정 따로 하기 (0) | 2019.06.15 |
Kotlin. Backing Field (0) | 2019.05.28 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- golang
- intellij
- war
- android
- php
- nodejs
- SWIFT
- go
- cocoapods
- github
- Xcode
- Python
- Spring
- windows10
- Kotlin
- git
- ubuntu
- CentOS
- tomcat
- MySQL
- Linux
- ios
- enum
- Windows
- docker
- centos8
- Codable
- rxswift
- Java
- Gradle
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
글 보관함