【问题标题】:~= operator in Swift~= Swift 中的运算符
【发布时间】:2016-07-14 10:32:01
【问题描述】:

我最近从 Apple 下载了 Advanced NSOperations 示例应用程序并找到了这段代码...

// Operators to use in the switch statement.
private func ~=(lhs: (String, Int, String?), rhs: (String, Int, String?)) -> Bool {
    return lhs.0 ~= rhs.0 && lhs.1 ~= rhs.1 && lhs.2 == rhs.2
}

private func ~=(lhs: (String, OperationErrorCode, String), rhs: (String, Int, String?)) -> Bool {
    return lhs.0 ~= rhs.0 && lhs.1.rawValue ~= rhs.1 && lhs.2 == rhs.2
}

它似乎对Strings 和Ints 使用~= 运算符,但我以前从未见过。

这是什么?

【问题讨论】:

    标签: ios swift operators


    【解决方案1】:

    只需使用“范围”的快捷方式:您可以构造一个范围,“~=”表示“包含”。 (其他可以添加更多的理论细节,但意义是这样的)。读作“包含”

    let n: Int = 100
    
    // verify if n is in a range, say: 10 to 100 (included)
    
    if n>=10 && n<=100 {
        print("inside!")
    }
    
    // using "patterns"
    if 10...100 ~= n {
        print("inside! (using patterns)")
    
    }
    

    尝试使用一些 n 值。

    广泛用于例如 HTTP 响应:

    if let response = response as? HTTPURLResponse , 200...299 ~= response.statusCode {
                    let contentLength : Int64 = response.expectedContentLength
                    completionHandler(contentLength)
                } else {
                    completionHandler(nil)
    

    【讨论】:

    • 很好的解释!
    【解决方案2】:

    它是用于case 语句中的模式匹配的运算符。

    您可以在这里查看以了解如何使用和利用它来提供您自己的实现:

    这是一个定义自定义并使用它的简单示例:

    struct Person {
        let name : String
    }
    
    // Function that should return true if value matches against pattern
    func ~=(pattern: String, value: Person) -> Bool {
        return value.name == pattern
    }
    
    let p = Person(name: "Alessandro")
    
    switch p {
    // This will call our custom ~= implementation, all done through type inference
    case "Alessandro":
        print("Hey it's me!")
    default:
        print("Not me")
    }
    // Output: "Hey it's me!"
    
    if case "Alessandro" = p {
        print("It's still me!")
    }
    // Output: "It's still me!"
    

    【讨论】:

    • switch语句是唯一使用这个操作符的地方吗? for case let item in items {...} 等呢?
    • @RoboRobok 你是完全正确的,它适用于任何case 语句。我更新了我的帖子。
    • 注意:switch 的第二个参数必须是~= 过载的第二个参数。意思是func ~=(value: Person, pattern: String) -&gt; Bool{ return value.name == pattern } 会产生这个错误:'String' 类型的表达式模式不能匹配'Person' 类型的值
    • @Honey 是的,这有点违反直觉。模式是第一个参数,而要匹配的值是第二个。不过,我的例子是正确的。
    • 但是 cmets 真的不谈论模式匹配的问题。他们谈到了一个问题,他们错误地认为可以对参数进行重新排序,当然事实并非如此。这是许多其他人共享的 Swift 语言限制。关于我上面的评论,我的观点很简单,每当您创建一个模式匹配 ~= 函数时,最好进行相反的操作,因为您可以在任何您想要的方向上进行匹配。
    【解决方案3】:

    您可以查看Define Swift

    func ~=<I : IntervalType>(pattern: I, value: I.Bound) -> Bool
    func ~=<T>(lhs: _OptionalNilComparisonType, rhs: T?) -> Bool
    func ~=<T : Equatable>(a: T, b: T) -> Bool
    func ~=<I : ForwardIndexType where I : Comparable>(pattern: Range<I>, value: I) -> Bool
    

    【讨论】:

    • 很高兴知道,但实际上并不能解释它是什么。是什么使它等同于true 或false?从这里我可以看到它可以针对相同类型的Equatable 变量运行,但这并没有告诉我实现。
    • 如果项目是范围范围则返回true,否则返回false。例如,0...10 ~= 5 为真,因为 5 在 0 到 10 的范围内。0...10 ~= 50 为假,因为 50 不在范围内。
    猜你喜欢
    • 2015-08-26
    • 1970-01-01
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多