我怎么能说,如果 label.text == 任何 Int
how i can say, if label.text == any Int
我怎么说:if label.text == any Int
if label.text == (Any, Int) {
label1.text = "Might be another Integer"
}
如果您有足够的时间再回答一个问题,那就太好了:
我怎么说:if label.text == any Int except 42
请把问题分开,谢谢帮助
您是否正在寻找这样的东西:
class Label {
var text: String? = "43"
}
let label = Label()
if let text = label.text, let int = Int(text) where int != 42 {
print("I'm \(int), not 42!")
}
// prints: I'm 43, not 42!
如果这看起来有点乱,你可以这样包装它:
if
let text = label.text,
let int = Int(text)
where int != 42
{
print("I'm \(int), not 42!")
}
欢迎来到 Whosebug,您的问题不清楚,但我会尝试回答,label
的 属性 .text
将始终是 String 类型的对象,因此它永远不会是一个 Int,但是通过阅读你的第二个问题,我假设你想检查是否是一个整数而不是一个对象 Int,所以你可以创建一个函数来检查一个字符串是否包含一个数字:
function checkIfAStringIsANumber(str:String)->Bool{
let decimalCharacters = NSCharacterSet.decimalDigitCharacterSet()
let decimalRange = str.rangeOfCharacterFromSet(decimalCharacters)
if decimalRange != nil {
return true;
}
return false;
}
并这样使用:
if (checkIfAStringIsANumber(label.text)) {
label1.text = "Label contains an integer value!"
}
第二个问题:
为了避免一些数字,你可以修改我们之前创建的函数来做这样的事情:
function checkIfAStringIsANumberWithoutSomeValues(str:String,excludedValues:[Int])->Bool{
let decimalCharacters = NSCharacterSet.decimalDigitCharacterSet()
let decimalRange = str.rangeOfCharacterFromSet(decimalCharacters)
if decimalRange != nil {
let intObj = Int(str);
if excludedValues.contains(intObj) {
return false;
}
return true;
}
return false;
}
你可以这样使用它:
let excludedValues:[Int] = [42,31,89,101] //Values that you want to exclude, if you want to exclude only 42 simply write [42]
if(checkIfAStringIsANumberWithoutSomeValues(label.text,excludedValues){
label1.text = "integer found"
}
我怎么说:if label.text == any Int
if label.text == (Any, Int) {
label1.text = "Might be another Integer"
}
如果您有足够的时间再回答一个问题,那就太好了: 我怎么说:if label.text == any Int except 42
请把问题分开,谢谢帮助
您是否正在寻找这样的东西:
class Label {
var text: String? = "43"
}
let label = Label()
if let text = label.text, let int = Int(text) where int != 42 {
print("I'm \(int), not 42!")
}
// prints: I'm 43, not 42!
如果这看起来有点乱,你可以这样包装它:
if
let text = label.text,
let int = Int(text)
where int != 42
{
print("I'm \(int), not 42!")
}
欢迎来到 Whosebug,您的问题不清楚,但我会尝试回答,label
的 属性 .text
将始终是 String 类型的对象,因此它永远不会是一个 Int,但是通过阅读你的第二个问题,我假设你想检查是否是一个整数而不是一个对象 Int,所以你可以创建一个函数来检查一个字符串是否包含一个数字:
function checkIfAStringIsANumber(str:String)->Bool{
let decimalCharacters = NSCharacterSet.decimalDigitCharacterSet()
let decimalRange = str.rangeOfCharacterFromSet(decimalCharacters)
if decimalRange != nil {
return true;
}
return false;
}
并这样使用:
if (checkIfAStringIsANumber(label.text)) {
label1.text = "Label contains an integer value!"
}
第二个问题: 为了避免一些数字,你可以修改我们之前创建的函数来做这样的事情:
function checkIfAStringIsANumberWithoutSomeValues(str:String,excludedValues:[Int])->Bool{
let decimalCharacters = NSCharacterSet.decimalDigitCharacterSet()
let decimalRange = str.rangeOfCharacterFromSet(decimalCharacters)
if decimalRange != nil {
let intObj = Int(str);
if excludedValues.contains(intObj) {
return false;
}
return true;
}
return false;
}
你可以这样使用它:
let excludedValues:[Int] = [42,31,89,101] //Values that you want to exclude, if you want to exclude only 42 simply write [42]
if(checkIfAStringIsANumberWithoutSomeValues(label.text,excludedValues){
label1.text = "integer found"
}