Lecture 2: MVVM and the Swift Type System

인생노잼시기·2021년 2월 21일
0

🏛Stanford

목록 보기
2/2

유튜브에서 Stanford CS193p iPhone Application Development Spring 2020을 들으며 기록했습니다.

Architecture

MVVM: design pattern

Model-View-VewModel

  • "code organizing" 설계 디자인 패턴
  • "reactive" user-interfaces와 함께 사용된다
  • SwiftUI와 함께 사용해야 한다
  • UIKit가 사용하는 MVC(Model View Controller)와 다르다


  • "The Truth": 다른 곳에 데이터를 저장하거나, 다른 버전이 있는 것이 아니다

  • Declared: UI에 어떤 함수가 선언되어있는지 알고 아무 때나 시간에 독립적으로 호출할 수 있다. localize code. UI를 보여주기 위한 다른 코드가 흩어져 있지않고 read-only라서 예측가능하다.

  • imperative: 함수간 의존성이 있어서(어떤 함수가 실행되고 다른 함수가 실행) 사용자가 함수를 호출하고 UI를 아무 때나 변경하려고 하면, 관리가 어렵다

  • Reactive: 모델이 바뀌면 자동으로 뷰도 바뀌게 된다.

  • Model -> View
    ViewModel은 View가 Model에 사용하기 위한 portal통로
    View가 공유해야할 일이 많아서 class로 되어 있다
    ViewModel은 View를 가리키는 포인터가 없다. (View는 그냥 subscribes the publication(observe), 모델까지 가지 않는다)
    Model의 변화를 알아챈다(struct가 복사되거나 HTTP 요청하거나 SQL database에 접근하거나)
    might "interpret": 중간에 해석할 수도 있고 안할 수도 있다
    publish "something changed": 변화한 것을 보여준다

  • 반대방향? View -> Model
    Model View Intent
    사용자가 화면을 카드를 클릭

Varieties of Types

struct, class, protocol, generics(Don't care), enum, functions

struct, class

stored var: 메모리에 저장됨
computed var
constant let
func
init

structclass
value typeReferenc type(heap에 저장)
copiedpointers
Copy on writeAutomatically reference counted
Functional programmingObject-oriented programming
No inheritanceInheritance (single)
"Free" init initializes ALL vars"Free" init initializes NO vars
Mutability must be explicitly stated(let일 때 변경 불가능)Always mutable(힙으로 접근 가능)
Your "go to " data structureUsed in specific circumstances
View는 프로토콜MVVM모델에서 ViewModel, UIKit

Generics

don't care 타입 -> Type parameter
예시: Array 안의 코드

strct Array<Element> {
	...
    func append(_ element: Element) {...}
}

var a = Array<Int>()
a.append(5)
a.append(22)
// 누군가 Array를 '사용'할 때, Element의 타입이 Int로 정해짐

Functions as Types

(Int, Int) -> Bool // takes two Ints and returns a Bool

var foo: (Double) -> Void // foo's type: function that takes a Double, returns nothing

func doSomething(what: () -> Bool)  // what's type: function, takes nothing, returns Bool



var operation: (Double) -> Double  // function that takes a Double and returns a Double

func squre(operand: Double) -> Double {
	return operand * operand
}

operation = squre
let result1 = operation(4)	// 16

operation = sqrt	// 내장함수
let result2 = operation(4)	// 2
  • Closure 클로저
    inlined function
    함수를 파라미터로 넘길 때 사용

위는 View 만들기
37분 27초
아래는 Model 만들기 시작

https://cs193p.sites.stanford.edu/

profile
인생노잼

0개의 댓글