swift - 无法将类型 'Int' 的值转换为预期参数类型 'Bool'

标签 swift

我是 Swift 新手,不明白为什么会出现以下错误:

exit status 1
main.swift:17:13: error: cannot convert value of type 'Int' to expected argument type 'Bool'
else if remainder2 && remainder == 0{
        ^~~~~~~~~~
main.swift:21:12: error: cannot convert value of type 'Int' to expected argument type 'Bool'
else if remainder && remainder3 == 0 {
       ^~~~~~~~~

对于以下代码:

var aYear =  Int(readLine()!)! 

func isLeap(year: Int) {
    let remainder = year % 4
    let remainder2 = year % 400
    let remainder3 = year % 100

    var whatLeap = false

    if remainder == 0 {
        whatLeap = true
    } else if remainder2 && remainder == 0 {
        whatLeap = true
    } else if remainder && remainder3 == 0 {
        whatLeap = false
    } else {
        whatLeap = false
    }

    if whatLeap == true {
        print("YES")
    } else  {
        print("NO")
    }
}

isLeap(year: aYear)

我尝试将变量“whatLeap”更改为字符串,但出现类似的错误。

任何帮助将不胜感激!

最佳答案

您需要完整地陈述这两个陈述:

remainder2 == 0 && remainder == 0

如果您按原样保留该语句,您所说的是remainder2 == true &&remaining == 0,这可以解释为什么您会收到错误消息Int to Bool。

再举一个例子:

var tOrF = false

if tOrF == true {}if tOrF {} 是相同的语句。

关于swift - 无法将类型 'Int' 的值转换为预期参数类型 'Bool',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59416021/

相关文章:

ios - 为什么 Realm addOrUpdateObject 会导致属性设置为 nil?

swift - 如何将位掩码 Int 转换为一组 Int?

ios - 如何将cocoapod添加到ios框架

ios - NSOperation 在队列结束时释放

ios - UITableView 静态单元格上的单元格始终为零

ios - 在 swift 3 中过滤代词、动词、地名

swift - SKLabelNode 更改文本的淡入淡出动画

swift - PHAssetChangeRequest 以低分辨率保存

ios - 通用链接转到 Safari 而不是调用它的应用程序

swift - 谁能解释一下 string.compare(_ :options:range:locale:) with example?