| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 31 |
- 알고리즘 자바
- 안드로이드
- Android WebView
- 코틀린 코루틴
- scope function
- 카카오 알고리즘
- OkHttp Interceptor
- 66챌린지
- coroutine
- Android 12 대응
- 안드로이드 갤러리 접근
- Kotlin
- 영어공부
- Kotlin FCM
- DataBinding
- 안드로이드 카카오 로그인
- android recyclerview
- Android Interceptor
- Android ViewPager2
- 영어독립365
- Android Navigation
- Java
- Android ProgressBar
- Android
- Android 12
- Android Jetpack
- MVP Architecture
- 습관만들기
- WebView
- 프로그래머스 알고리즘
- Today
- Total
나미래 Android 개발자
[Android] Kotlin apply이란 본문
apply 범위함수
범위함수란
범위함수란 영어로 Scope Funtion으로 불린다.
함수명 Scope Funtion에서 알 수 있듯이 이 범위 내에 함수를 만드는 것이다.
범위(Scope)를 만들어서 그 안에서 람다식을 이용해 로직을 구현할 수 있다.
apply 범위함수란
fun <T> T.apply(block: T.() -> Unit): T
apply 범위함수에 대해서 Kotlin 공식 문서에서는 다음과 같이 설명하고 있다.
The context object is available as a receiver(this). The return value is the object itself.
Use
applyfor code blocks that don't return a value and mainly operate on the members of the receiver object.
The common case forapplyis the object configuration. Such call can be read as "apply the following assignments to the object."
val adam = Person("Adam").apply {
age = 32
city = "London"
}
println(adam) // Person(name=Adam, age=32, city=London)
Having the receiver as the return value, you can easily include apply into call chains for more complex processing.
공식 문서에 설명이 정말 잘 되어있는 것 같다.
간단하게 요약한다면 자기 자신을 receiver(this)로 받는 코드 블록을 생성하고 자기 자신을 반환한다는 것이 apply다.
사용법
공식문서에서도 사용법에 대해 좋은 예시가 있지만, 내가 apply를 사용하면서 경험상 좋았던 예시와 안좋았던 예시에 대해 다루어보겠다.
GOOD
val intent = Intent(this@MainActivity, ProductInfoActivity::class.java).apply{
putExtra("userId", userId)
putExtra("productId", productId)
/** do something with this intent */
}
startActivity(intent)
Intent를 생성해서 해당 Intent에 데이터를 담거나 특별한 작업들이 필요할 때,Intent를 생성한 후 intent.[-]() 형태와 같이 변수를 반복적으로 쓰기보다는 직관적으로 선언과 동시에 apply를 이용해서 정의를 해주면 반복된 코드도 줄이게 되고 코드를 읽기도 편했다.
BAD
binding.apply{
imageView.load(url)
textView.text = "test text"
button.setOnClickListener{
/** do something */
}
}
ViewBinding 또는 DataBinding을 사용할 때, ViewController에서 각가의 View들을 초기화할 때 binding을 반복적인 사용을 줄이기 위해서 apply를 이용해서 했다.
하지만 apply 사용의 목적은 "The common case for apply is the object configuration."이고 반환되는 객체를 사용하지도 않기 때문에 좋은 사용은 아니였던 것 같다.
대신 with 또는 run을 사용하면 괜찮은 것 같다.
'안드로이드' 카테고리의 다른 글
| Dynamic Link - 파이어베이스 프로젝트 셋팅 (0) | 2023.03.20 |
|---|---|
| [Android] Android Studio Project 빌드 안되는 이유 찾기 (0) | 2022.10.25 |
| [Android] PendingIntent 란 (0) | 2022.10.01 |
| JUnit Annotations with Example in Kotlin (0) | 2022.06.24 |
| Android Unit Testing 기본 구현 in Kotlin (0) | 2022.06.23 |