[英]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.