iBooks 의 SWIFT 4 를 통해 언어 공부 중입니다. 이때 사용된 책 속의 코드 및 연습문제 등의 코드 로그입니다.
아래 코드들은 Github 에서도 다운받을 수 있습니다.
SWIFT_Playground_Excersice: https://github.com/youp-han/SWIFT_Playground_Excersice
//------------------------------------------------------------
// 코드 로그 입니다.
// Day 2
//Function in Function (Closure)
func makeIncrementer() -> ((Int)-> Int) {
func addOne (number: Int) -> Int {
return number + 1
}
return addOne
}
var increment = makeIncrementer()
increment(8)
//a function receives a return value from a function
//as a parameter
// Blocks of codes that call be called later
func hasAnyMatches(list:[Int], condition:(Int)->Bool)->Bool{
for item in list {
if condition(item){
print("conditon true \(item)")
return true
}
print("condition false, and item = \(item)")
}
return false
}
func lessThanTen(number: Int)-> Bool{
return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(list: numbers, condition: lessThanTen)
// closure sample with no name
numbers.map({ (number:Int) -> Int in
let result = 3 * number
return result
})
let threeTimes = numbers.map({ (number:Int) -> Int in
let result = 3 * number
return result
})
print(threeTimes)
//experiment : the closure that returns 0 for all odd numbers in list
numbers.map({ (number:Int) -> Int in
let result = number % 2
if result == 0{
return number
}
return 0
})
// when the type of closure is known
let mappedNumbers = numbers.map({ number in 3 * number})
print (mappedNumbers)
// parameters as numbers
let sortedNumbers = numbers.sorted { $0 > $1 }
let sortedNumbers2 = numbers.sorted { $0 < $1 }
print(sortedNumbers)
'초짜 IT보이 서바이벌 스토리 > iOS - swift' 카테고리의 다른 글
[swift] code log, day 4 (0) | 2017.09.26 |
---|---|
[swift] code log, day 3 (0) | 2017.09.25 |
[swift] code log, day 1 (0) | 2017.09.22 |
[swift] #SWIFT #4 언어 가이드 (0) | 2017.09.22 |
[swift] push notification (0) | 2014.06.25 |