var someString: String? = "a"
someString = "b"
// or someString = nil
条件:someString不是nil和“a”
例:
if someString != nil && someString != "a" {
}
是否可以调整一个?
我认为你的意思是你想在一个逻辑表达式中测试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
,如果你想要条件“双方都是零”评估为真)
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
尝试使用where子句:
if let forcedString = someString where forcedString != "a" {
print("Yea")
} else {
print("Err")
}
您可以使用可选的展开运算符折叠整个事物:
if someString? == "a" {
swiz 1.2 for var a:int?; var b:int?
有矩阵
nil == nil; // true
nil > nil; // false
nil > 2; // false
2 > nil; // true
2 > 1; // true
一招:
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")
派对有点晚了但是这个怎么样(假设你想要比较字符串):
if someName.compare(displayName) == .OrderedSame{
//true
}
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
}
}
}