简体   繁体   English

但在 Xcode 10.3 中失败,但在 Xcode 11.3 中工作

[英]But failed in Xcode 10.3 but work in Xcode 11.3

I have a piece of code that builds without a problem in Xcode 11.3 but fails in Xcode 10.3.我有一段代码在 Xcode 11.3 中构建没有问题,但在 Xcode 10.3 中失败。 I think that's because Xcode 10.3 doesn't have the required compiler.我认为这是因为 Xcode 10.3 没有所需的编译器。 The code implements push notifications that don't work in Xcode 11.3 but work in Xcode 10.3 (something to do with this - that's why I want to build in Xcode 10.3).该代码实现了在 Xcode 11.3 中不起作用但在 Xcode 10.3 中起作用的推送通知(与此有关- 这就是我想在 Xcode 10.3 中构建的原因)。 Is there anything I can do to build in Xcode 10.3?有什么我可以在 Xcode 10.3 中构建的吗?

Update with code:用代码更新:

func signalRecipients(transaction: SDSAnyReadTransaction) -> [SignalRecipient] {
    e164sForIntersection.compactMap { e164Number in
        let address = SignalServiceAddress(phoneNumber: e164Number)
        return SignalRecipient.registeredRecipient(for: address, mustHaveDevices: true, transaction: transaction)
    }
}

In Xcode 11.3 no errors.在 Xcode 11.3 中没有错误。 In Xcode 10.3: "Unable to infer complex closure return type; add explicit type to disambiguate"在 Xcode 10.3 中:“无法推断复杂的闭包返回类型;添加显式类型以消除歧义”

    var hasViewed: Bool { firstViewedTimestamp > 0 }

In Xcode 11.3 no errors.在 Xcode 11.3 中没有错误。 In Xcode 10.3: "Missing return in a function expected to return 'Bool'".在 Xcode 10.3 中:“预期返回‘Bool’的函数中缺少返回值”。

I think the behavior is due to the compiler from Xcode 10.3.我认为这种行为是由于 Xcode 10.3 的编译器造成的。 Could I somehow use the compiler from Xcode 11.3 to Xcode 10.3?我可以以某种方式使用从 Xcode 11.3 到 Xcode 10.3 的编译器吗?

The error messages tell you what you need to know.错误消息告诉您需要了解的内容。

  • The first one is an expression that is too complex for type interference for 10.3, so give it some types.第一个是对于 10.3 的类型干扰来说太复杂的表达式,所以给它一些类型。 Like e164Number in -> whatever type.像 e164Number in -> 任何类型。

  • The second one tells you clearly that you are not returning a bool expression.第二个清楚地告诉您,您没有返回 bool 表达式。 Looking at the code, true, there is no return statement.看代码,真的,没有return语句。 So add a return statement.所以添加一个return语句。

I think the behavior is due to the compiler from Xcode 10.3.我认为这种行为是由于 Xcode 10.3 的编译器造成的。

Of course it does.当然可以。

Could I somehow use the compiler from Xcode 11.3 to Xcode 10.3?我可以以某种方式使用从 Xcode 11.3 到 Xcode 10.3 的编译器吗?

No: what you do is write code that works with both compilers.不:您所做的是编写适用于两种编译器的代码。

Let's be clearer.让我们更清楚。 This has to do only indirectly with the version of Xcode.这仅与 Xcode 的版本间接相关。 What's really important is the version of Swift .真正重要的是Swift的版本。 Different versions of Swift have different compilers and different language rules, as the language evolves over time.随着语言的发展,不同版本的 Swift 有不同的编译器和不同的语言规则。

Let's take the second one:我们来看第二个:

var hasViewed: Bool { firstViewedTimestamp > 0 }

In Xcode 11.3 no errors.在 Xcode 11.3 中没有错误。 In Xcode 10.3: "Missing return in a function expected to return 'Bool'".在 Xcode 10.3 中:“预期返回‘Bool’的函数中缺少返回值”。

Correct.正确的。 In Swift 5.1 a new rule was introduced that it is legal to omit the keyword return in a one-line function body.在 Swift 5.1 中引入了一条新规则,即在一行函数体中省略关键字return是合法的。 But the rule is new, so for an earlier version of Swift you still have to say that the old way:但规则是新的,所以对于早期版本的 Swift,你仍然必须说旧的方式:

var hasViewed: Bool { return firstViewedTimestamp > 0 }

That will work for both older and newer versions of Swift, so just use that and all will be well.这对旧版本和新版本的 Swift 都有效,所以只要使用它,一切都会好起来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM