【问题标题】:how can i put these print text into Textfield?如何将这些打印文本放入 Textfield?
【发布时间】:2018-01-13 10:26:13
【问题描述】:
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})"
     }
    }

【问题讨论】:

  • print() 在 Xcode 中有效,但在尝试放入文本字​​段时无效 *************print($0.map { $0.content.title },",",$0.map { $0.content.subtitle},",",$0.map { $0.content.body},",",$0.map { $0.trigger!})

标签: ios11 swift4 xcode9


【解决方案1】:

您在 async { } 闭包中使用 $0。此闭包不需要任何参数,这意味着使用 $0 参数快捷方式无效。

您显然试图从getPendingNotificationRequests 回调中引用requests 数组。不能使用 $0 的原因是它被 DispatchQueue.main.async{ ... } 闭包屏蔽,没有参数:

试试这个:

    UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
        DispatchQueue.main.async{
            let str:String = ""
            self.finalresulter.text = str
            self.finalresulter.text = "\(requests.map{$0.content.title})"
        }
    }

$0 的规则声称 $0 始终引用当前范围。因此,要从嵌套闭包访问闭包参数,该参数必须命名(上述代码中的requests)。

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 2022-01-04
    • 2013-05-11
    • 2016-10-20
    相关资源
    最近更新 更多