21. 10. 26 Notification, 네이밍

Allie·2021년 11월 24일
1

TIL

목록 보기
8/15

Notification

등록된 Notification에 NotificationCenter를 통해 정보를 전달하기 위한 구조체

  • 주요 프로퍼티로는 name, object, userInfo가 있다.
var name: Notification.Name
// 보내는 방송의 이름 같은 것

var object: Any?
// 발송자가 Obsever에게 보내는 객체.

var userInfo: [AnyHashable : Any]?
// 방송과 관련된 값 또는 객체의 저장소. Notification이 발송될 때, 필요한 데이터를 같이 넘겨준다.
  • userInfo에는 필요한 데이터를 dictionary 형식으로 보내줄 수 있는데, 아래와같이 열거형을 적용해서 깔끔하게 만들어 줄 수 있다!
let notificationCenter = NotificationCenter.default

extension Notification.Name {
    static let stockInformation = Notification.Name("stockInformation")
    static let stockError = Notification.Name("stockError")
}

enum NotificationKey {
    case fruit
    case stock
}

private func postNotification(for fruit: Fruit, stock: Int) {
        notificationCenter.post(name: Notification.Name.stockInformation,
                                object: nil,
                                userInfo: [NotificationKey.fruit: fruit,
                                           NotificationKey.stock: stock]
                                           )

Bool type의 메서드 네이밍 하는 법

네이밍 꿀팁이다~

profile
게발자🦀 되는 중.. 궁김하다.. 궁김해..

0개의 댓글