【问题标题】:Swift Combine: Unable to infer complex closure return type errorSwift Combine:无法推断复杂的闭包返回类型错误
【发布时间】:2021-10-16 03:27:04
【问题描述】:

我有以下功能:

func generateIn() -> Future<Int,Never> {
    return Future{ promise in
        promise(.success(Int.random(in: 1...10)))
    }
}

我正在从这个变量中调用该函数:

let sub2 = generateIn()
    .map { value in
        print(value)
        return Int(value)
    }

但是我收到了这个错误:

Unable to infer complex closure return type; add explicit type to disambiguate

你们中的任何人都知道我为什么会收到此错误或如何解决它?

非常感谢您的帮助

【问题讨论】:

  • 您的print 可能会让编译器感到困惑。你可以试试.map({ Int($0) })。如果这不起作用,您可以按照下面的示例显式定义类型。 swift 编译器很聪明,但如果你在地图中添加太多东西,它也很愚蠢。

标签: ios swift xcode swiftui combine


【解决方案1】:

做它要求你做的事;告诉它闭包是什么类型。

    let sub2 = generateIn()
        .map { (value:Int) -> Int in
            print(value)
            return Int(value)
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2020-07-06
    相关资源
    最近更新 更多