比较值和 return Swift 中的布尔值
Compare value and return a bool in Swift
我正在将我的代码从 Objective-C 转换为 Swift。我声明了一个函数来比较两个属性和 return 和 Bool
的值。
我很困惑为什么这段代码在 Swift.
中不起作用
private var currentLineRange: NSRange?
var location: UInt?
func atBeginningOfLine() -> Bool {
return self.location! == self.currentLineRange?.location ? true : false
}
编译器给我一个错误:
Could not find an overload for == that accepts the supplied arguments
谢谢。
Swift 有运算符重载,所以 == 是一个函数。你必须定义一个函数来接受你的两种类型。
如果删除 UInt,它会起作用:
class Document {
private var currentLineRange: NSRange?
var location: Int?
func atBeginningOfLine() -> Bool {
if let currentLocation = self.location, lineRange = self.currentLineRange {
return currentLocation=lineRange?.location
} else {
return false
}
}
}
修改为 null 安全。
你有两个可选值,你想检查它们是否相等。有一个版本的 ==
用于比较两个可选值——但它们必须是同一类型。
这里的主要问题是您正在比较 NSRange.location
,它是 Int
,location
,它是 UInt
。如果你尝试这样做,即使没有可选的复杂性,你也会得到一个错误:
let ui: UInt = 1
let i: Int = 1
// error: binary operator '==' cannot be applied to operands of
// type 'Int' and ‘UInt'
i == ui
有两种方法可供选择。将 location
更改为 Int
,您将能够使用可选的 ==
:
private var currentLineRange: NSRange?
var location: Int?
func atBeginningOfLine() -> Bool {
// both optionals contain Int, so you can use == on them:
return location == currentLineRange?.location
}
或者,如果 location
由于某些其他原因确实需要成为 UInt
,则 map
将其中一个选项与另一个类型进行比较:
private var currentLineRange: NSRange?
var location: UInt?
func atBeginningOfLine() -> Bool {
return location.map { Int([=12=]) } == currentLineRange?.location
}
有一点要注意——nil
等于nil
。所以如果你不想要这个(取决于你想要的逻辑),你需要明确地为它编码:
func atBeginningOfLine() -> Bool {
if let location = location, currentLineRange = currentLineRange {
// assuming you want to stick with the UInt
return Int(location) == currentLineRange.location
}
return false // if either or both are nil
}
我正在将我的代码从 Objective-C 转换为 Swift。我声明了一个函数来比较两个属性和 return 和 Bool
的值。
我很困惑为什么这段代码在 Swift.
private var currentLineRange: NSRange?
var location: UInt?
func atBeginningOfLine() -> Bool {
return self.location! == self.currentLineRange?.location ? true : false
}
编译器给我一个错误:
Could not find an overload for == that accepts the supplied arguments
谢谢。
Swift 有运算符重载,所以 == 是一个函数。你必须定义一个函数来接受你的两种类型。
如果删除 UInt,它会起作用:
class Document {
private var currentLineRange: NSRange?
var location: Int?
func atBeginningOfLine() -> Bool {
if let currentLocation = self.location, lineRange = self.currentLineRange {
return currentLocation=lineRange?.location
} else {
return false
}
}
}
修改为 null 安全。
你有两个可选值,你想检查它们是否相等。有一个版本的 ==
用于比较两个可选值——但它们必须是同一类型。
这里的主要问题是您正在比较 NSRange.location
,它是 Int
,location
,它是 UInt
。如果你尝试这样做,即使没有可选的复杂性,你也会得到一个错误:
let ui: UInt = 1
let i: Int = 1
// error: binary operator '==' cannot be applied to operands of
// type 'Int' and ‘UInt'
i == ui
有两种方法可供选择。将 location
更改为 Int
,您将能够使用可选的 ==
:
private var currentLineRange: NSRange?
var location: Int?
func atBeginningOfLine() -> Bool {
// both optionals contain Int, so you can use == on them:
return location == currentLineRange?.location
}
或者,如果 location
由于某些其他原因确实需要成为 UInt
,则 map
将其中一个选项与另一个类型进行比较:
private var currentLineRange: NSRange?
var location: UInt?
func atBeginningOfLine() -> Bool {
return location.map { Int([=12=]) } == currentLineRange?.location
}
有一点要注意——nil
等于nil
。所以如果你不想要这个(取决于你想要的逻辑),你需要明确地为它编码:
func atBeginningOfLine() -> Bool {
if let location = location, currentLineRange = currentLineRange {
// assuming you want to stick with the UInt
return Int(location) == currentLineRange.location
}
return false // if either or both are nil
}