Ios 为什么我的标签上会显示;您的猫是可选的(35)岁”;它应该在什么时候显示";你的猫35岁了?

Ios 为什么我的标签上会显示;您的猫是可选的(35)岁”;它应该在什么时候显示";你的猫35岁了?,ios,swift,type-conversion,label,Ios,Swift,Type Conversion,Label,Swift的新手,我遇到了一个令人沮丧的问题。程序编译正确,运行时不会崩溃。该程序应根据用户输入的人类年数,以猫年为单位计算猫的年龄。但是,按下按钮后,结果会显示“操作员”一词,并附加以括号分隔的cat年份,即,可选(35)。这是我的密码: @IBOutlet weak var getHumanYears: UITextField! @IBOutlet weak var displayCatYears: UILabel! @IBAction func calculateCatYears(_ se

Swift的新手,我遇到了一个令人沮丧的问题。程序编译正确,运行时不会崩溃。该程序应根据用户输入的人类年数,以猫年为单位计算猫的年龄。但是,按下按钮后,结果会显示“操作员”一词,并附加以括号分隔的cat年份,即,
可选(35)
。这是我的密码:

@IBOutlet weak var getHumanYears: UITextField!
@IBOutlet weak var displayCatYears: UILabel!
@IBAction func calculateCatYears(_ sender: Any) 
{
    if let humanYears = getHumanYears.text
    {
        var catYears: Int? = Int(humanYears)
        catYears = catYears! * 7
        let catYearsString: String = String(describing: catYears)

        displayCatYears.text = "Your cat is " + catYearsString + " years old"
    }
}

有人知道我做错了什么吗?感谢您的宝贵意见

展开
catYearsString
。 用这个 让CATYEARS字符串:字符串=字符串(描述:catYear!) displayCatYears.text=“您的猫是”+catYearsString+“岁”

输出:

测试代码

var catYears: Int? = Int(7)
catYears = catYears! * 7
let catYearsString: String = String(describing: catYears!)

print("Your cat is " + catYearsString + " years old")

展开
Catyears字符串
。 用这个 让CATYEARS字符串:字符串=字符串(描述:catYear!) displayCatYears.text=“您的猫是”+catYearsString+“岁”

输出:

测试代码

var catYears: Int? = Int(7)
catYears = catYears! * 7
let catYearsString: String = String(describing: catYears!)

print("Your cat is " + catYearsString + " years old")
问题在于:

String(describing: catYears)
catYears
是一个
可选
,描述
可选
的字符串将采用
Optional()
nil
的格式。这就是为什么您会得到
可选(35)

您需要打开
catYears

String(describing: catYears!)
或者,将
字符串(描述:)
一起删除,然后执行以下操作:

if let humanYearsText = getHumanYears.text, let humanYears = Int(humanYearsText)
{
    let catYears = humanYears * 7
    displayCatYears.text = "Your cat is \(catYears) years old"
}
问题在于:

String(describing: catYears)
catYears
是一个
可选
,描述
可选
的字符串将采用
Optional()
nil
的格式。这就是为什么您会得到
可选(35)

您需要打开
catYears

String(describing: catYears!)
或者,将
字符串(描述:)
一起删除,然后执行以下操作:

if let humanYearsText = getHumanYears.text, let humanYears = Int(humanYearsText)
{
    let catYears = humanYears * 7
    displayCatYears.text = "Your cat is \(catYears) years old"
}

正如其他人提到的,这是因为
var catYears:Int?=Int(人年)
,因此
catYears
是可选的。可选的
字符串(描述:…)
会打印
optional(rawValue)

您需要的是确保在打印时具有该值,而不是可选值。如果100%确定字符串中确实有
Int
值,则可以使用
执行此操作

但是,我建议您不要使用
运算符,因为如果文本字段中有字符,则会使应用程序崩溃

if let text = getHumanYears.text, let humanYears = Int(text)
{
    let catYears = humanYears * 7
    displayCatYears.text = "Your cat is \(catYears) years old"
} else {
    displayCatYears.text = "I don't know!" 
}

正如其他人提到的,这是因为
var catYears:Int?=Int(人年)
,因此
catYears
是可选的。可选的
字符串(描述:…)
会打印
optional(rawValue)

您需要的是确保在打印时具有该值,而不是可选值。如果100%确定字符串中确实有
Int
值,则可以使用
执行此操作

但是,我建议您不要使用
运算符,因为如果文本字段中有字符,则会使应用程序崩溃

if let text = getHumanYears.text, let humanYears = Int(text)
{
    let catYears = humanYears * 7
    displayCatYears.text = "Your cat is \(catYears) years old"
} else {
    displayCatYears.text = "I don't know!" 
}

您不应该使用字符串描述方法。只需打开可选文件并使用
字符串(CATYERS)
您不应该使用字符串描述方法。只需打开可选文件并使用
String(catYears)
@LeoDabus-Edited,我只是指出OP的代码哪里出了问题。@LeoDabus-Edited,我只是指出OP的代码哪里出了问题。
catYears-String
无法打开。。。这不是一个问题optional@LeoDabus请检查编辑答案。
catYearsString
无法展开。。。这不是一个问题optional@LeoDabus请检查编辑答案。