Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Property Wrapper 사용한 UserDefaults 리펙토링 #629

Open
hyun99999 opened this issue May 21, 2022 · 2 comments
Open

[Refactor] Property Wrapper 사용한 UserDefaults 리펙토링 #629

hyun99999 opened this issue May 21, 2022 · 2 comments
Assignees
Labels
👼타락pOwEr천사현규 크로아서버 환영요 👷Refactor👷‍♂️ 동작변화없이 내부구조 변경

Comments

@hyun99999
Copy link
Member

프로퍼티 래퍼 공부 후 다시금 리펙토링을 목표로 함

Originally posted by @hyun99999 in #628 (comment)

@hyun99999
Copy link
Member Author

@propertyWrapper
struct UserDefault<T> {
  let key: String
  let defaultValue: T

  // ✅ Userdefaults 로 값을 찾을 때 없다면, defaultValue 를 반환.
  // 그렇기 때문에 wrappedValue 는 옵셔널이 아님.
  var wrappedValue: T {
    get {
      return UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
    }
    set {
      UserDefaults.standard.set(newValue, forKey: key)
    }
  }
}

enum GlobalSettings {
  @UserDefault(key: "FOO_FEATURE_ENABLED", defaultValue: false)
  static var isFooFeatureEnabled: Bool

  @UserDefault<Bool>(key: "BAR_FEATURE_ENABLED", defaultValue: false)
  static var isBarFeatureEnabled
}

@hyun99999
Copy link
Member Author

swift-evolution/0258-property-wrappers.md at main · apple/swift-evolution

의 코드를 참고하여 프로젝트에 적용

@hyun99999 hyun99999 self-assigned this Sep 4, 2022
@hyun99999 hyun99999 added 👷Refactor👷‍♂️ 동작변화없이 내부구조 변경 👼타락pOwEr천사현규 크로아서버 환영요 labels Sep 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
👼타락pOwEr천사현규 크로아서버 환영요 👷Refactor👷‍♂️ 동작변화없이 내부구조 변경
Projects
None yet
Development

No branches or pull requests

1 participant