UNUserNotificationCenter.current().getPendingNotificationRequests {
DispatchQueue.main.async{//Contextual closure type '() -> Void' expects 0 arguments, but 1 was used in closure body
let str:String = ""
self.finalresulter.text = str
self.finalresulter.text = "\($0.map{$0.content.title})"
}
}
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You are using $0
inside async { }
closure. This closure expects no arguments, which means using $0
argument shortcut is invalid.
You are evidently attempting to refer to requests
array from getPendingNotificationRequests
callback. The reason you can't by using $0
is that it's screened by DispatchQueue.main.async{ ... }
closure with no arguments:
Try this:
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
DispatchQueue.main.async{
let str:String = ""
self.finalresulter.text = str
self.finalresulter.text = "\(requests.map{$0.content.title})"
}
}
The rule for $0
claims that $0
always refers to current scope. Thus, to access closure argument from nested closure, that argument must be named (requests
in the above code).