代码之家  ›  专栏  ›  技术社区  ›  Final Fantassy Remix

如何将这些打印文本放入文本字段?

  •  -4
  • Final Fantassy Remix  · 技术社区  · 7 年前
    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 回复  |  直到 7 年前
        1
  •  0
  •   Hexfire    7 年前

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

    你显然是想提到 requests 阵列自 getPendingNotificationRequests 回调。您无法使用的原因 $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 始终引用当前范围 . 因此,要从嵌套闭包访问闭包参数,必须命名该参数( 请求 在上述代码中)。