Ios 什么是;无法调用类型为';Int';具有类型为';UITextField'&引用;什么意思?

Ios 什么是;无法调用类型为';Int';具有类型为';UITextField'&引用;什么意思?,ios,swift,Ios,Swift,在我的应用程序中,我有一个字符串,它是lrs24=(30*Int(权重)!+70)*Int(因子)。看起来一切正常,但编译器说它“无法使用'UITextField'类型的参数列表调用'Int'类型的初始值设定项”。这意味着什么?我如何修复它?谢谢 PS-这是我所有的代码- 错误正是它所说的:初始化Int时不能使用UITextField作为参数 以下是您在textfieldsidendediting函数中使用的代码 lrs24 = (30 * Int(weight)! + 70) * Int(fa

在我的应用程序中,我有一个字符串,它是
lrs24=(30*Int(权重)!+70)*Int(因子)。看起来一切正常,但编译器说它“无法使用'UITextField'类型的参数列表调用'Int'类型的初始值设定项”。这意味着什么?我如何修复它?谢谢

PS-这是我所有的代码-


错误正是它所说的:初始化Int时不能使用UITextField作为参数

以下是您在
textfieldsidendediting
函数中使用的代码

lrs24 = (30 * Int(weight)! + 70) * Int(factor)!
                 ^ ^ ^ ^              ^ ^ ^ ^
                 | | | |              | | | |
         Defined as a UITextField!    | | | |
                               Defined as a [UITextField]!
您试图告诉Swift创建一个新的整数,
Int()
。要创建整数,需要给Swift一个文本字段,
weight
factor
。Swift应该从哪里获取整数值?
UITextField
不是整数

从这个角度来看,这就像试图把一个苹果变成一个橘子——这是不可能做到的(除非你有黑魔法,但那是另一个主题)。同样的想法也适用于这一点——你不能把一个用户可以输入文本的字段变成一个数字。它就是不起作用

但是,您可以将字符串转换为整数,例如,因为它包含可以初始化整数的数据。例如,使用
Int(“7901”)
将字符串
“7901”
转换为数字非常简单,因为您提供的是可以转换为整数的Swift数据

如果要获取在文本字段中输入的文本,必须使用变量。例如,要获取在
weight
字段中输入的数字,可以使用

//This should only be used if you are 100% sure that the text will be a number
//If it isn't, using this code, the app will crash
var weightInteger: Int = Int(weight.text)!

//If you aren't 100% sure the input will be a number, you should use
var weightInteger: Int = Int(weight.text) ?? 0
由于使用
字符串进行了上述
Int
初始化,您将能够执行此操作

我还假设
因子
变量不应该是
[UITextField]
,它是
UITextFields
的数组(或列表)。相反,它应该是一个
UITextField
(当然,我可能错了,您可能实际上正在该变量中存储
UITextFields
的列表,在这种情况下,您必须使用
for
循环来获取列表中的值

如果要设置标签的文本,必须使用-不能将
UILabel
设置为字符串

因此,最后,假设
因子
应该是
UITextField!
而不是
[UITextField]!
,您应该使用

let value: Int = (30 * (Int(weight.text) ?? 0) + 70) * (Int(factor.text) ?? 0)
//You may want to change the last bit
//(Int(factor.text) ?? 0) to (Int(factor.text) ?? 1)
//Which would set the factor to "1" instead of "0"
//if a non-integer or nothing is inputed

lrs24.text = "\(value)"
//this sets the text of the label to
//the value above. It has to be in the format
//"\(x)" because we have to turn x into a String.
//If you prefer, you could also use String(x)
//which would be String(value) in this example
//that's personal preference, though
此外,虽然这与原始问题无关,但您应该对代码做一些改进

  • 行尾不需要
    ,Swift会自动为您结束行尾
  • 除非确实确保值不会为零,否则应避免强制展开(
    运算符)。如果初始化整数为
    nil
    ,则应使用
    Int(x)!
    ,而不是使用
    Int(x)??0
    ,这将代替崩溃,给出一个值“0”

我明白,但是我应该用什么方法来收集用户在这些文本字段中输入的数字呢?@George我刚刚更新了这个问题,这个问题假设
因子
实际上应该是
UITextField!
而不是
[UITextField]!
(这一点我可能是错的,取决于你在做什么)哦,我刚刚注意到你关于
[UITextField]
的更新,我对它进行了更改。但是,编译器现在说它“不能将类型'Int'分配给类型'String'”。@George Yep,这是我犯的错误-你必须在将值放入标签之前将其转换为字符串(因为标签的文本必须是字符串).我更新了答案对不起,我只是按照你在答案中说的做了,我犯了一个致命的错误…也许我输入的代码错了?
let value: Int = (30 * (Int(weight.text) ?? 0) + 70) * (Int(factor.text) ?? 0)
//You may want to change the last bit
//(Int(factor.text) ?? 0) to (Int(factor.text) ?? 1)
//Which would set the factor to "1" instead of "0"
//if a non-integer or nothing is inputed

lrs24.text = "\(value)"
//this sets the text of the label to
//the value above. It has to be in the format
//"\(x)" because we have to turn x into a String.
//If you prefer, you could also use String(x)
//which would be String(value) in this example
//that's personal preference, though