简体   繁体   English

无法分配类型为'String'的值? 输入“ Int”

[英]Cannot assign value of type 'String?' to type 'Int'

I am getting the error message Cannot assign value of type 'String?' 我收到错误消息无法分配类型为'String'的值? to type 'Int' 输入“ Int”

I have browsed through other questions like this but it still shows the error. 我已经浏览了其他类似问题,但仍然显示错误。

 if sunscreenName.text != nil && reapplyTime.text != nil {
     sunscreen = sunscreenName.text!
     reApplyTime = reapplyTime.text

     //Some sort of message such as Progress hud
 }

Thanks in advance! 提前致谢!

I got your problem, actually what happens here Swift is is type safe langauge 我有您的问题,实际上Swift在这里发生的是类型安全的语言

So what you are doing is is to store a String value in Int which will not happen automatically you need to convert it to Int 因此,您要做的是将一个String值存储在Int ,这不会自动发生,您需要将其转换为Int

like this Int(sunscreenName.text) 像这样Int(sunscreenName.text)

But there is a catch there not all string are convertible to Int type, fo eg 但是有一个问题,并不是所有的字符串都可以转换为Int类型,例如

let name = "roshan"

if you try to convert it to Int it will give you a nil 如果您尝试将其转换为Int ,它将给您零

let a = Int(name)

So its better you do a optional Binding here provided by Swift 所以最好在这里由Swift提供一个可选的Binding

if let sunValue = Int(sunscreenName.text),let reApplyValue = Int(reapplyTime.text) {
 sunscreen = sunValue
 reApplyTime = reApplyValue
}

I recommend reading through The Swift Programming Language to get a better understanding of Swift and its fundamental concepts, since this question is fairly basic. 我建议您通读《 Swift编程语言》以更好地理解Swift及其基本概念,因为这个问题是相当基本的。

You make several mistakes: 您犯了几个错误:

if sunscreenName.text != nil && reapplyTime.text != nil {

This is wrong. 错了 In Swift, if you plan to use the value later, you should use if let rather than comparing to nil . 在Swift中,如果您打算以后使用该值,则应使用if let而不是与nil进行比较。 Comparing to nil leaves the values optional, but if let unwraps them. nil比较,这些值是可选的,但是if let它们解开,则将它们取消包装。 So, do this instead: 因此,改为这样做:

if let sunscreenText = sunscreenName.text, let reapplyText = reapplyTime.text {

Now you have the sunscreenText and reapplyText variables, which are typed String , not String? 现在,您有了sunscreenTextreapplyText变量,它们的类型为String ,而不是String? (ie they are not optional). (即它们不是可选的)。

Now, there's these two lines. 现在,有这两行。

sunscreen = sunscreenName.text!
reApplyTime = reapplyTime.text

You don't say which one is giving the error, but the issue is the same in either case. 您没有说出是谁在发出错误,但是在两种情况下,问题都是相同的。 First, use our unwrapped sunscreenText and reapplyText variables instead of sunscreenName.text! 首先,使用展开后的sunscreenTextreapplyText变量代替sunscreenName.text! and reapplyTime.text . reapplyTime.text Next, if one of these is meant to be an Int instead of a String , cast it. 接下来,如果其中之一是Int而不是String ,则将其强制转换。 Swift is not like JavaScript, in that it won't automatically convert values from one type to another, so if something is a string and we need an integer, we have to convert it ourselves. Swift与JavaScript不一样,它不会自动将值从一种类型转换为另一种类型,因此,如果某物是字符串并且我们需要一个整数,则必须自己将其转换。

(assuming reapplyTime was the line that was giving the error:) (假设reapplyTime是给出错误的行:)

if let reapplyInt = Int(reapplyText) {
    reapplyTime = reapplyInt
}

The reason we have to unwrap is because Int(String) can return nil if the string is something that can't be converted to an integer. 我们必须解包的原因是,如果字符串不能被转换为整数,则Int(String)可以返回nil Alternately, we could just provide a default value: 或者,我们可以提供一个默认值:

reapplyTime = Int(reapplyText) ?? 0 // sets to 0 if it can't parse the string as an integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM