7. Java ☕
Null Pointer Exception
Getter / Setter
Anonymous Class
복잡한 계산로직을 자유자재로, 가독성이 좋게 짜기
힘듦
8. Activity (== ViewController)
너무나 많은 역할을 하는 Activity
View, Life Cycle, Navigation, Networking, DB,
Business Logic, …
어떤 코드가 어디에 있는지 찾기 힘듦
(== 뭣이 중헌지 모름)
이러한 구조로는 테스트도 불가능
16. Optional Type이란?
타입 선언 시 변수에 nil이 저장될 수 있는지 여부를 명시
var nonOptional: Int = 0
nonOptional = nil // Compile Error!
var optional: Int? = 0
optional = nil // OK!
18. Optional Binding - if let
if let viewController = viewController as? UITableViewController {
viewController.tableView.reloadData()
}
19. Optional Binding - if let
if let viewController = viewController as? UITableViewController,
refreshControl = viewController.refreshControl {
refreshControl.beginRefreshing()
}
20. Optional Binding - if let
if let viewController = viewController as? UITableViewController,
refreshControl = viewController.refreshControl
where refreshControl.refreshing {
refreshControl.beginRefreshing()
}
24. Optional Binding - switch
let passwordText: String? = ""
switch(passwordText) {
case .Some(let password) where 8 <= password.characters.count:
print(password)
case .Some:
print("8자리 이상 입력해주세요")
case .None:
print("password is nil")
}
32. Closure 사용과 타입 추론
Closure는 이름이 없는 함수
var sum = [1, 23, 4, 32].reduce(0, combine: { (sum: Int, element: Int) -> Int in
return sum + element
})
33. Closure 사용과 타입 추론
타입이 명확한 경우 생략 가능
extension SequenceType {
/// Returns the result of repeatedly calling `combine` with an
/// accumulated value initialized to `initial` and each element of
/// `self`, in turn, i.e. return
/// `combine(combine(...combine(combine(initial, self[0]),
/// self[1]),...self[count-2]), self[count-1])`.
@warn_unused_result
public func reduce<T>(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows -> T
}
var sum = [1, 23, 4, 32].reduce(0, combine: { (sum: Int, element: Int) -> Int in
return sum + element
})
34. Closure 사용과 타입 추론
sum = [1, 23, 4, 32].reduce(0, combine: { sum, element in
return sum + element
})
sum = [1, 23, 4, 32].reduce(0, combine: {
return $0 + $1
})
sum = [1, 23, 4, 32].reduce(0, combine: { $0 + $1 })
sum = [1, 23, 4, 32].reduce(0) { $0 + $1 }
sum = [1, 23, 4, 32].reduce(0, combine: +)
35. 투데잇에서의 Functional
통계 계산 로직에서 적극적으로 사용
대용량 데이터를 다루는 것이 아닌 유저 한사람의 데이
터 그 중에서도 특정 날짜의 데이터를 주로 다룸
스레드 처리만 잘 해주면 사용자 체감 상 크게 문제될 상
황은 없음
퍼포먼스도 물론 중요하지만 오류 없고, 가독성이 높고,
수정이 용이한 코드가 필요
map, flatMap, filter, reduce, contains ...
37. Property&Method Requirements
Property & Method 에 대한 명세
protocol Flyable {
var distance: Int { get set }
func fly()
}
struct SwiftBird: Flyable {
var distance: Int
func fly() {
print("flying!")
}
}
38. Protocol with an Extension
Protocol은 Property를 가질 수 없지만 Method는
가질 수 있음
protocol ViewControllerProtocol: class {
var loadingView: LoadingView { get set }
}
extension ViewControllerProtocol where Self: UIViewController {
func showLoadingView() {
loadingView.showLoadingView(self)
}
func dismissLoadingView() {
loadingView.dismissView()
}
}
44. View
UIViewController에 해당
View Life Cycle
ex) viewDidLoad 함수가 호출되었음을 Presenter에 알림
View Event
ex) signUpButton이 클릭되었음을 Presenter에 알림
View Control
ex) titleLabel의 텍스트를 바꾸는 방법을 Presenter에 제공
67. VIPER with Swift - Protocol
protocol ViewControllerProtocol: class {
var loadingAlert: LoadingAlert { get set }
}
extension ViewControllerProtocol where Self: UIViewController {
func showLoadingAlert() {
loadingView.showLoadingAlert(self)
}
func dismissLoadingAlert() {
loadingView.dismissAlert()
}
}
68. VIPER with Swift - Computed Property
protocol SignUpViewControllerProtocolForPresenter: class {
var email: String? { get set }
var password: String? { get set }
}
extension SignUpViewController: SignUpViewControllerProtocolForPresenter {
var email: String? {
get { return emailTextField.text }
set { emailTextField.text = newValue }
}
var password: String? {
get { return passwordTextField.text }
set { passwordTextField.text = newValue }
}
}
69. 장점
하나의 화면에서 기능 단위로 코드를 명확하게 분리
(물론 모든 컴포넌트에 대해 테스트를 하고 있지 않
지만) 모든 컴포넌트에 대해 테스트 가능해지는 구조
가됨
(물론 혼자 개발하고 있지만) 분업이 훨씬 수월해짐
72. Swift로 iOS 앱 개발을 하면서 느낀 점
Swift 정말 최고!
UI가 있는 어플리케이션 만든다는 관점에서 큰 동작
원리는 Android나 iOS나 비슷한 것 같음
iOS에도 문제가 많다고 하지만 안드로이드에 비할바
는 아닌 것 같음
훨씬 평화로운 iOS! 피쓰~ 🕊