how can i put these print text into Textfield?

2019-03-07 07:20发布

问题:

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).