【问题标题】:Expression of type '()-> is ambiguous without more context'()-> 类型的表达式在没有更多上下文的情况下是模棱两可的
【发布时间】:2017-11-23 21:48:01
【问题描述】:

let lockQueue = DispatchQueue(label: "com.test.LockQueue")
imapSession?.connectionLogger = { (UnsafeMutableRawPointer, MCOConnectionLogType, data) -> () in
    lockQueue.sync() {
        if (type != MCOConnectionLogType.SentPrivate) {  //error is coming in this line
            //TODO: better memory management
            if let data = data {
                let errorString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
                print("event logged \(String(describing: UnsafeMutableRawPointer)) withData: \(errorString!)")

我已从 swift 1.2 转换为 swift 3.2。我将 dispatch_queue_create 更改为最新的语法,但最终遇到了这个问题。谁能建议我如何解决这个问题?

【问题讨论】:

  • 什么是“type”?
  • (UnsafeMutableRawPointer, MCOConnectionLogType, data) => (unsafeMutableRawPointer, type, data) ?您需要命名 var,而不是放置它们的“类”。你是为(NS)Data 做的,但不是为其他两个?
  • 谢谢它的工作

标签: swift swift3


【解决方案1】:

你必须在闭包中传递值/变量而不是类型

   imapSession?.connectionLogger = { (pointer, type, data) in

并且不要在 Swift 中使用NSString,除非编译器告诉你这样做

let errorString = String(data: data, encoding: .utf8)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多