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 - 无法在 Swift 3.0 中将 CIImage 转换为 UIImage

uitableview - 如何将 UILabels 正确插入到每个 tebleview 单元格中

ios - Swift:XCTest 类 'FirstDemoTests' 没有初始化器

ios - 使用自动调整单元格设置 UICollectionView 后出现异常

ios - 无法从 Apple Music 获取播放列表名称

ios - 在从函数返回之前等待 Firebase 加载

ios - 无法使用 Swift Closure 作为参数调用函数

cocoa - 使用 NSToolBar Outlet xcode 6 和 Storyboard?

swift - UIViewanimation trantitionfromView 使用 UIViewflip 动画

xcode - 在 Swift 中清空和保存核心数据表的最简单方法是什么?