2016-11-30 4 views
0

에서 비동기 함수의 결과를 내 코드에 관한 질문이 기다립니다아이폰 OS

func isNotificationsEnabled()->Bool{ 
    var isNotificationEnabled = false 
    center.getNotificationSettings() { (settings) in 
     switch settings.soundSetting{ 
     case .enabled: 
      isNotificationEnabled = true 
      break 
     case .disabled: 
      isNotificationEnabled = false 
      break 

     case .notSupported: 
      isNotificationEnabled = false 
      break 
     } 
    } 

    return isNotificationEnabled 
} 

이 함수의 반환 결과 center.getNotificationSettings() 전에 결과를 반환합니다. center.getNotificationSettings()의 결과를 기다리고이 기능을 동기화 할 수있는 방법이 있습니까?

+1

가능한 복제본 [Swift 함수에서 비동기 호출에서 데이터 반환] (http://stackoverflow.com/questions/25203556/returning-data-from-async-call-in-swift- 기능) –

+1

묻지 말고, 말해! 비동기 완료 핸들러를 사용하십시오. – vadian

답변

2

당신이, 이 시도, 아이폰 OS에서 완료 블록이라고를 위해

func isNotificationsEnabled(completion:@escaping (Bool)->Swift.Void){ 
     var isNotificationEnabled = false 
     center.getNotificationSettings() { (settings) in 
      switch settings.soundSetting{ 
      case .enabled: 
       isNotificationEnabled = true 
       completion(isNotificationEnabled) 
       break 
      case .disabled: 
       isNotificationEnabled = false 
       completion(isNotificationEnabled) 
       break 

      case .notSupported: 
       isNotificationEnabled = false 
       completion(isNotificationEnabled) 
       break 
      } 
     } 
    } 

사용,

isNotificationsEnabled { (isNotificationEnabled) in 
    debugPrint(isNotificationEnabled)   
} 
-1

가 완료 처리기를 추가를 찾고 있습니다!

func isNotificationsEnabled(completion: (Bool) ->())->Bool{ 
    var isNotificationEnabled = false 
    center.getNotificationSettings() { (settings) in 
     switch settings.soundSetting{ 
     case .enabled: 
      isNotificationEnabled = true 
      break 
     case .disabled: 
      isNotificationEnabled = false 
      break 

     case .notSupported: 
      isNotificationEnabled = false 
      break 
     } 
    } 

    return isNotificationEnabled 
    completion(isNotificationEnabled) 
} 

그리고

이 완료 블록을 사용하는 예입니다
isNotificationEnabled() { isNotificationsEnabled in 
    print(isNotificationsEnabled) 
} 
+1

아니요, 당신은'return' 문 다음에'completion'을 호출합니다 (즉, 결코 거기에 가지 않을 것입니다). (a) 메소드 정의를 변경하여 더 이상 아무것도 반환하지 않게해야합니다 (이제 완료 핸들러가 있음). (b)'return' 문을 제거하십시오. – Rob

+0

좋은 합의점, 죄송합니다. –

2

, 그것은 감소의 IT를 호출하지만 코드와 같은 기능이 있습니다

func isNotificationsEnabled(completion:@escaping (Bool)->()) { 
    center.getNotificationSettings() { (settings) in 
     switch settings.soundSetting { 
     case .enabled: 
      completion(true) 

     default: 
      completion(false) 
     } 
    } 
} 

아직 더 할 수 있습니다 필수로 환원 :

func isNotificationsEnabled(completion:@escaping (Bool)->()) { 
    center.getNotificationSettings() { (settings) in 
     completion (settings.soundSetting == .enabled) 
    } 
} 

.enabled 경우에만 true을 반환하므로 default을 사용하면 다른 모든 경우에 false을 반환합니다. 그건 그렇고 : 스위프트에서 break 성명은 필요하지 않습니다.

그리고 전화 :

func isNotificationsEnabled(completionHandler: @escaping (Bool) -> Void) { 
    center.getNotificationSettings { settings in 
     completionHandler(settings.soundSetting == .enabled) 
    } 
} 

을 그리고 다른 사람들이 지적했듯이, 그것은 다음과 같이 호출 :

isNotificationsEnabled { success in 
    if success { 
     print("is enabled") 
    } else { 
     print("is disabled") 
    } 
} 
0

를 참고로, 당신은 아마 훨씬 더이 문제를 단순화 할 수

isNotificationsEnabled { enabled in 
    if enabled { 
     print("is enabled") 
    } else { 
     print("is not enabled") 
    } 
}