Type Annotation & Type Inference & Literals
Type Annotation
- 변수 선언 시 타입을 명확하게 지정하는 것
1 | let year: Int = 2019 |
Type Inference
- 변수 선언 시 값을 통해 변수의 타입을 추론하는 것
1 | let name: String = "Babo" |
Literals & Types
- 리터럴: 고정된 값으로 표현되는 문자 그 자체
- 정수 / 실수 / 문자 / 문자열 / 불리언 리터럴 등
Operator
Ternary Operator 삼항연산자
- 결과: ? 앞의 식이 참일때 True : False
1 | a > 0 ? "positive" : "zero or negative" |
Assignment Operators 할당연산자
1 | value += 1 |
Arithmetic Operators 산술 연산자
1 | // 실수에서의 나눗셈 |
Question
1 | // 범위 연산의 순서를 반대로(내림차순) 적용하려면? |
Function
Input 과 Output 이 모두 있는 것 (Function)
Input 이 없고 Output 만 있는 것 (Generator)
Input 이 있고 Output 은 없는 것 (Consumer)
Input 과 Output 이 모두 없는 것
1 | // 기본유형 |
Input이 없는 경우
1 | func hello1() { |
Output이 없는 경우
1 | func say(number: Int) { |
Input 과 Output 이 모두 있는 것
1 | func greet(person: String) -> String { |
Input 과 Output 이 모두 없는 것
1 | let outside = "outside" |
Argument Label
- argumentName: 외부에서 호출
- parameterName: 함수내에서사용
1 | func functionName(argumentName <parameterName>: <Type>) { |
Variadic Parameters 가변 파라미터
1 | func average(_ numbers: Double...) -> Double { |
Nested Functions 중첩함수
1 | /* |