openlang

Swift 2.0에서 | 연산자로 알림 플래그를 합치면 실패해요

아래 코드로 제 앱의 로컬 알림을 등록하려고 해요:

  UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

Swift 2.0과 Xcode 7에서 오류는 Binary Operator "|" cannot be applied to two UIUserNotificationType operands예요. 도와주실 분 있나요?

영어에서 번역원문 보기

이어지는 대화 4

서로의 근거를 따라
  1. 고유 링크

    Swift 2에서는 이런 식으로 자주 쓰던 타입 중 다수가 OptionSetType 프로토콜을 구현하도록 바뀌었습니다. 그래서 배열과 비슷한 문법을 지원하며, 질문의 예제는 이렇게 작성할 수 있습니다.

      let notificationOptions = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
      UIApplication.sharedApplication().registerUserNotificationSettings(notificationOptions)

    관련해서, 옵션 집합에 어떤 옵션이 있는지 확인할 때도 더는 비트 AND 뒤에 nil 검사를 할 필요가 없습니다. 대신 배열에 원소가 있는지 확인하듯 그 집합에 원하는 값이 들어 있는지 물으면 됩니다.

      let notificationOptions = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
    
      if notificationOptions.types.contains(.Alert) {
          // handle this option
      }

    Swift 3에서는 예제를 이렇게 쓰세요:

      let notificationOptions = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
      UIApplication.shared.registerUserNotificationSettings(notificationOptions)

    그리고

      let notificationOptions = UIUserNotificationSettings(types: [.alert, .badge], categories: nil)
    
      if notificationOptions.types.contains(.alert) {
          // handle this option
      }
    영어에서 번역원문 보기
  2. 고유 링크

    저는 이 버전으로 됐어요

      //Successful version
      var notificationOptions = UIUserNotificationSettings(forTypes: UIUserNotificationType([.Alert, .Badge, .Sound]), categories: nil)
    영어에서 번역원문 보기
  3. 고유 링크

    Swift 3에서 이 부분의 문법이 바뀌었어요.

              let notificationOptions = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
              UIApplication.shared.registerUserNotificationSettings(notificationOptions)
    영어에서 번역원문 보기

같은 질문의 다른 갈래

  1. Java Mail에서 이메일 주소 대신 발신자 이름을 표시하기

    Java Mail API로 메시지를 보내고 있어요. 도착한 메일의 받은편지함에는 보낸 주소가 표시돼요. 그 발신자 주소 대신 텍스트를 보여 주고 싶어요. facebook에서 오는 메일은 받은편지함에 "mailbox@faceb…

    답글 2최근 활동 java
  2. TypeScript에서 Dynamics CRM에 접근하기

    CRM 코드가 웹 리소스 안에서 실행되다 보니 엔터티 데이터를 가져오려면 이렇게 작성해야 해요. 그런데 typescript로 작성하면 이 표현식이 컴파일되지 않아요. 지금은 Xrm.Page 앞에 window.parent를..…

    답글 2최근 활동 typescript
  3. Gleam HTTP 응답 헤더에는 어떤 날짜 형식이 필요한가요?

    Date 헤더를 보내고 싶어요. 필요한 형식은 다음과 같아요: 지금까지 시도한 코드는 이거예요:

    답글 3최근 활동 gleam