简体   繁体   中英

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. 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'". 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. 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. 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. What you're looking for is probably something like this:

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

Also side note: the ++ operator is deprecated, moving towards using +=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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