简体   繁体   English

类型“Int”不符合协议“BooleanType”?

[英]Type 'Int' does not conform to protocol 'BooleanType'?

I know there is another thread with the same question, but it doesn't tell what is actually causing the problem我知道还有另一个线程有同样的问题,但它没有说明问题的真正原因

Im new to swift, so Im a bit confused on this.我是 swift 的新手,所以我对此有点困惑。 I wrote a very simple program that is supposed to start with a default number of followers (0) and assign that to 'defaultfollowers' and once that becomes 1 its supposed become "followers", but I get the error "Type 'Int' does not conform to protocol 'BooleanType'".我编写了一个非常简单的程序,它应该以默认的关注者数量 (0) 开始并将其分配给“defaultfollowers”,一旦变为 1,它就应该成为“关注者”,但我收到错误“Type 'Int'不符合协议“BooleanType””。 What is causing this and why这是什么原因造成的,为什么

    var followerdeafault = 0
var followers = 0
if (followerdeafault++){
 var followers = followerdeafault

}

In Swift you can't implicitly substitute Int instead of Bool.在 Swift 中,您不能隐式地替换 Int 而不是 Bool。 This was done to prevent confusion and make code more readable.这样做是为了防止混淆并使代码更具可读性。

So instead of this所以而不是这个

let x = 10
if x { /* do something */ }

You have to write this:你必须这样写:

let x = 10
if x != 0 { /* do something */ }

Also you can't pass an Optional instead of Bool to check if it's nil , as you would do in Objective-C.此外,您不能像在 Objective-C 中那样通过 Optional 而不是 Bool 来检查它是否为nil Use explicit comparison instead:改用显式比较:

if myObject != nil { /* do something */ }

As the comments said, you're trying to use an Int in a Bool comparison statement.正如评论所说,您试图在Bool比较语句中使用Int What you're looking for is probably something like this:你正在寻找的可能是这样的:

if followerdeafuaut++ == 1 { ... }

Also side note: the ++ operator is deprecated, moving towards using +=另附注:不推荐使用++运算符,转向使用+=

暂无
暂无

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

相关问题 类型'Int'不符合协议'BooleanType' - Type 'Int' does not conform to protocol 'BooleanType' 类型&#39;()&#39;不符合协议&#39;BooleanType&#39; - Type '()' does not conform to protocol 'BooleanType' 类型HalfOpenInterval <T> 不符合协议BooleanType - Type HalfOpenInterval<T> does not conform to protocol BooleanType 类型“ AnyObject”不符合协议“ BooleanType” - Type 'AnyObject' does not conform to protocol 'BooleanType' 类型“NSPersistentStore”在swift中不符合协议“BooleanType” - Type 'NSPersistentStore' does not conform to protocol 'BooleanType' in swift 类型'Boolean'不符合协议'BooleanType' - Type 'Boolean' does not conform to protocol 'BooleanType' 游乐场执行失败: <EXPR> :15:33:错误:类型&#39;Int&#39;不符合协议&#39;BooleanType&#39; - Playground execution failed: <EXPR>:15:33: error: type 'Int' does not conform to protocol 'BooleanType' 将Objective-C转换为Swift-错误:类型&#39;Int&#39;不符合协议&#39;BooleanType&#39; - Translating Objective-C to Swift - Error: Type 'Int' does not conform to protocol 'BooleanType' 内联if语句在void返回闭包中改变inout参数,奇怪的错误(错误:类型&#39;Int1&#39;不符合协议&#39;BooleanType&#39;) - Inline if statement mutating inout parameter in a void return closure, weird error (Error: type 'Int1' does not conform to protocol 'BooleanType') 类型“()”未确认协议:“ BooleanType” - Type '()" does not confirm to protocol: 'BooleanType'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM