전체 글 211

[Jetpack Compose] Column, Row, Text

Text 말 그대로 텍스트를 화면에 출력시켜준다. class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { InflearnProjectTheme { Surface(color = MaterialTheme.colors.background) { Text("Hello") } } } } } 이때 한줄이 아닌 여러줄을 출력시키고자 할 경우 Text를 여러번 호출시킬 수 있다. class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceSt..

[Jetpack Compose] 요즘 떠오르는 Jetpack Compose가 뭘까?

Jetpack Compose Native UI를 코드레벨로 구현할 수 있는 최신 툴킷이다. 기존의 뷰를 업데이트하는 방식과 달리 Compose를 사용하면 필요한 영역의 뷰를 다시 그려주는 방식으로 작업할 수 있다. 지금까지 Android 어플리케이션을 개발하기 위해서는 대부분 XML을 활용해야만 했다. 하지만, Jetpack Compose를 활용하게 된다면 코틀린 단 하나로 Android 어플리케이션을 개발할 수 있다. 아래는 간단한 예제이다. class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { Inf..

[Udacity] Kotlin Bootcamp for Programmers - Functional Manipulation

1번 문제 Practice Time In this practice, you are going to write the the first part of a higher-order functions game. You will implement everything, except the higher-order functions. Let’s get started. Create a new file. Create an enum class, Directions, that has the directions NORTH, SOUTH, EAST and WEST, as well as START, and END. Create a class Game. Inside Game, declare a var, path, that is a m..

[Kotlin] 람다 lambda

람다는 익명함수 라고도 부른다. 이름은 많이 들어봤는데 람다가 뭘까? "익명함수"라는 이름이 힌트다. 이름 없이 함수 역할을 하는 형태를 "람다"라고 부른다. 람다는 화살표 표기법을 사용한다. 그럼 어떻게 쓸까? val lambdaExample: (Int, Int) -> Int = {x: Int, y: Int -> x + y} 자료형 변수명: (선언자료형) {람다식의 매개변수 -> 처리내용} 이러한 형식으로 사용한다. 이때 코틀린은 자료형을 추론할 수 있다는 성질로 인해 몇몇가지를 생략할 수 있다. val lambdaExample: (Int, Int) -> Int = {x: Int, y: Int -> x + y} // 기본형 val lambdaExample = {x: Int, y: Int -> x + y..

[Udacity] Kotlin Bootcamp for Programmers - Kotlin Essentials: Beyond the Basics

1번문제 Practice Time Let's go through an example of getting information about a book in the format of a Pair. Generally, you want information about both the title and the author, and perhaps also the year. Let’s create a basic book class, with a title, author, and year. Of course, you could get each of the properties separately. Create a method that returns both the title and the author as a Pair...

[Kotlin] 엘비스 연산자 (Elvis Operation)

코틀린에는 엘비스 연산자가 있다. null을 체크하고, 결과를 리턴해준다. var birthday = readLine()?.toIntOrNull()?:1 이렇게 ?: 를 활용해서 값을 리턴하는 방식이다. ?:의 왼쪽 값이 null일 경우 ?:의 오른쪽에 있는 값을 리턴해준다. 위의 코드를 예시로 들면 readLine()?.toIntOrNull의 값, 즉 입력받은 값이 null일 경우 1을 리턴하고 null이 아닐경우 원래 값을 리턴한다. 아래와 같은 예시가 있다. fun main(){ var nullTest: Int? = 3 if (nullTest == null){ println(0) }else{ println(nullTest+1) } } if와 else를 사용하여 입력값이 null일 경우를 체크해준다...

[Kotlin] Loop : while / for / repeat

코틀린에서의 반복은 크게 while, for, repeat가 있다. 상황에 맞는 함수를 선택할 수 있도록 각각 함수에 대한 특징을 정리해 보자. while while은 java와 완전히 동일한 형식으로 사용한다. while(조건){ 실행할 내용 } 조건이 true일 경우 while문은 계속해서 반복을 하게 되고, 조건이 false가 될 경우 반복을 종료한다. while의 경우 조건을 검사한 후 실행할 내용을 실행한다. 그렇기 때문에 처음 조건이 false일 경우, while문은 작동하지 않는다. 우선 한번 실행하고, 조건을 검사하고자 한다면 do-while을 사용하면 된다. do-while 또한 java와 완전히 동일하다. do{ 실행할 내용 }while(조건) for kotlin에서의 for문은 in ..