如何在swift可选中比较值

时间:2014-08-25 15:25:44

标签: swift optional

var someString: String? = "a"    
someString = "b" 
// or someString = nil

条件: someString不是nil和“a”

示例:

if someString != nil && someString != "a" {

}

是否可以调整单个?

8 个答案:

答案 0 :(得分:7)

Swift 3 中的正确方法是使用多子句条件:

if let bString = someString, bString != "a" {
    print("bString: '\(bString)' is not nil and is different from 'a'")
}

https://github.com/apple/swift-evolution/blob/master/proposals/0099-conditionclauses.md

答案 1 :(得分:4)

尝试使用where子句:

if let forcedString = someString where forcedString != "a" {
    print("Yea")
} else {
    print("Err")
}

答案 2 :(得分:3)

我认为你的意思是你想在一个逻辑表达式中测试someString != nil someString != "a"(而不是两个in和。)

不,我不认为使用内置运算符是可能的,但是可以实现像这样的String扩展:

extension String {
    func isDifferentThan(value: String?) -> Bool {
        return value != nil && self != value?
    }
}

您可以按如下方式使用:

someString = nil
"a".isDifferentThan(someString) // Return false

someString = "b"
"a".isDifferentThan(someString) // Return true

someString = "a"
"a".isDifferentThan(someString) // Return false

附录:更优雅的解决方案是定义自己的逻辑运算符。我使用了!~=,但可以自由使用。

infix operator !~= { associativity left }
func !~= (a: String?, b: String?) -> Bool {
    if a == nil || b == nil {
        return false
    }

    return a != b
}

测试如下:

someString = nil
someString !~= "a" // Returns false

someString = "b"
someString !~= "a" // Returns true

someString = "a"
someString !~= "a" // Returns false

someString = nil
someString !~= nil // Returns false

您可以在处理nil值时对其进行微调(例如为nil添加支票并返回true,以防您想要条件“双方都是零“评估为真”

答案 3 :(得分:2)

您可以使用可选的展开运算符折叠整个事物:

if someString? == "a" {

答案 4 :(得分:2)

swift 1.2 for var a:int?; var b:int?有矩阵

nil == nil; // true
nil > nil;  // false
nil > 2;    // false
2 > nil;    // true
2 > 1;      // true

答案 5 :(得分:1)

一招:

func existsAnd<T: Equatable>(x:T?, _ it:T ) -> Bool {
    return x.flatMap { $0 == it } ?? false
}

var str : String? = "hello"
existsAnd(str, "hello")
str = nil
existsAnd(str, "hello")

答案 6 :(得分:0)

派对有点晚了但是这个怎么样(假设你想要比较字符串):

if someName.compare(displayName) == .OrderedSame{
  //true
}

答案 7 :(得分:0)

此Optional扩展名将比较Optionals及其包装值。

extension Optional: Comparable where Wrapped: Comparable {

    // Optional <=> Optional

    public static func < (lhs: Optional<Wrapped>, rhs: Optional<Wrapped>) -> Bool {
        switch lhs {
        case .some(let v): return v < rhs
        case .none: return false
        }
    }

    public static func <= (lhs: Optional<Wrapped>, rhs: Optional<Wrapped>) -> Bool {
        switch lhs {
        case .some(let v): return v <= rhs
        case .none: return false
        }
    }

    public static func >= (lhs: Optional<Wrapped>, rhs: Optional<Wrapped>) -> Bool {
        switch lhs {
        case .some(let v): return v >= rhs
        case .none: return false
        }
    }

    public static func > (lhs: Optional<Wrapped>, rhs: Optional<Wrapped>) -> Bool {
        switch lhs {
        case .some(let v): return v > rhs
        case .none: return false
        }
    }


    // Optional <=> Wrapped

    public static func < (lhs: Optional<Wrapped>, rhs: Wrapped) -> Bool {
        switch lhs {
        case .some(let v): return v < rhs
        case .none: return false
        }
    }

    public static func <= (lhs: Optional<Wrapped>, rhs: Wrapped) -> Bool {
        switch lhs {
        case .some(let v): return v <= rhs
        case .none: return false
        }
    }

    public static func >= (lhs: Optional<Wrapped>, rhs: Wrapped) -> Bool {
        switch lhs {
        case .some(let v): return v >= rhs
        case .none: return false
        }
    }

    public static func > (lhs: Optional<Wrapped>, rhs: Wrapped) -> Bool {
        switch lhs {
        case .some(let v): return v > rhs
        case .none: return false
        }
    }


    // Wrapped <=> Optional

    public static func < (lhs: Wrapped, rhs: Optional<Wrapped>) -> Bool {
        switch rhs {
        case .some(let v): return lhs < v
        case .none: return false
        }
    }

    public static func <= (lhs: Wrapped, rhs: Optional<Wrapped>) -> Bool {
        switch rhs {
        case .some(let v): return lhs <= v
        case .none: return false
        }
    }

    public static func >= (lhs: Wrapped, rhs: Optional<Wrapped>) -> Bool {
        switch rhs {
        case .some(let v): return lhs >= v
        case .none: return false
        }
    }

    public static func > (lhs: Wrapped, rhs: Optional<Wrapped>) -> Bool {
        switch rhs {
        case .some(let v): return lhs > v
        case .none: return false
        }
    }
}