일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- weatherkit
- designpattern
- state
- isolate
- OpenAI
- uikit
- Architecture
- factory
- WWDC24
- network
- 문법
- Adapter
- iot
- flutter
- SampleApp
- EventLoop
- singleton
- dartz
- builder
- philipshue
- AppleDeveloper
- GIT
- WiFi
- swift
- dart
- Xcode
- concurrency
- tuist
- LifeCycle
- SwiftUI
Archives
- Today
- Total
Jaebi의 Binary는 호남선
Swift - 문법 (Values, Control Flow, Functions and Closures) 본문
목차
Values
- var → variable
- let → constant
- type 명시 → `let explicitDouble: Double = 70`
- type conversion → wrap with `Int()`, `Double()`, `String()`
- String 표기 (string interpolation)
- 기존 Flutter의 `'${value}'` -> Swift에서는 `"\(value)"`
- three double quotation marks(`"""`) → multiple line strings
- arrays and dictionaries → use `[]`
- array → var fruits = `["limes", "pears", "apples"]`
- dictionary → var jobs = `["Jae": "Student", "Kai": "Actor"]`
Control Flow
- conditionals → `if` and `switch`
- loops → `for-in`, `while`, `repeat-while`
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
}
}
Functions and Closures
- function → `func`
- use `->` to separate the parameter names and types from the function's return type
func greet(person: String, day: String) -> String {
return "Hello \(person), today is \(day)."
}
- use underscore instead of an explicit argument label to omit the parameter
func someFunction(_ firstParameterName: Int, secondParameterName: Int) {}
someFunction(1, secondParameterName: 2)
- default parameter values
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12) {}
someFunction(parameterWithoutDefault: 3, parameterWithDefault: 6) // parameterWithDefault is 6
someFunction(parameterWithoutDefault: 4) // parameterWithDefault is 12
- function types
var mathFunction: (Int, Int) -> Int = addTwoInts
Define a variable called mathFunction, which has a type of ‘a function that takes two Int values, and returns an Int value.’ Set this new variable to refer to the function called addTwoInts.
- nested functions
func returnFifteen() -> Int {
var y = 10
func add() {
y += 5
}
add()
return y
}
- function can return another function as its value
func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
func lessThanTen(number: Int) -> Bool {
return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(list: numbers, condition: lessThanTen)
- closure → `({})`
- use in to separate the arguments and return type from the body in closure
numbers.map({ (number: Int) -> Int in
let result = 3 * number
return result
})
- if closure’s type is already known, can omit the type of its parameters, its return type, or both
let mappedNumbers = numbers.map({ number in 3 * number })
print(mappedNumbers)
// Prints "[60, 57, 21, 36]"
- when a closure is the only argument to a function, can omit the parentheses
let sortedNumbers = numbers.sorted { $0 > $1 }
print(sortedNumbers)
// Prints "[20, 19, 12, 7]"
Reference
'Swift' 카테고리의 다른 글
Swift - 문법 (Property Wrappers) (0) | 2024.06.12 |
---|---|
Swift - 문법 (Protocols and Extensions, Error Handling, Generics) (0) | 2024.06.12 |
Swift - 문법 (Object and Classes, Enumerations and Structures, Concurrency) (0) | 2024.06.12 |
Philips Hue 연동 프로젝트 (0) | 2024.06.08 |
SwiftUI에 UIKit 넣기 (0) | 2024.05.06 |