2016-11-30 59 views
0

異步函數的結果我有關於我的代碼的問題:返回結果等待iOS中

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

你所尋找被稱爲iOS中完成塊, 試試這個,

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

這是使用完畢塊爲例,它的減少,但具有相同的功能代碼:

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。順便說一句:在Swift break聲明是不需要的。

,並稱之爲:

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

僅供參考,你也許可以進一步簡化這個:

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

正如有人指出,它就會被調用爲:

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