“运营商的结果”

问题描述 投票:-3回答:1

我想知道这是否是XCode中的错误或者我是否做错了什么:

我有这个实现Comparable的类:

class Span: Comparable {
    var start: Int
    var end: Int

    // Some stuff skipped 

    static func < (lhs: Span, rhs: Span) -> Bool {
        lhs.start < rhs.start
    }

    static func == (lhs: Span, rhs: Span) -> Bool {
        lhs.start == rhs.start
    }
}

XCode抱怨func <func ==声明为

Result of operator '<' is unusedResult of operator '==' is unused

确实从未在我的代码中明确调用它们,但它们被用作

var spans: [Span] = []
spans.append(Span(....))
...
spans.append(Span(....))
spans.sort() // sort() calls operator '<' for a Comparable type Span

我做错了什么或是XCode中的错误?

编辑:没有return不缺少。这是隐含的。代码编译得很好。这是我要问的警告。没有更多关于“失踪回归”的答案。谢谢。

swift swift4.2
1个回答
2
投票

你需要return一个bool值

static func < (lhs: Span, rhs: Span) -> Bool {
    return lhs.start < rhs.start
}

static func == (lhs: Span, rhs: Span) -> Bool {
    return lhs.start == rhs.start
}
© www.soinside.com 2019 - 2024. All rights reserved.