Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Check class with guard statement swift

In my class i want to check my model class using guard feature. I try to do following;

func bindWithModel(model: Any)-> Void {

        guard let model is VacanciesItem else {

        }
    }

However, it throw me an error - Variable binding in a condition requires an initializer

How to fix it?

Upvotes: 1

Views: 375

Answers (2)

Péter Aradi
Péter Aradi

Reputation: 390

You don't need the let keyword:

guard model is VacanciesItem else

Upvotes: 2

Daij-Djan
Daij-Djan

Reputation: 50099

remove the let.

let x = x would be an assignment. you dont have any assignment though as you only test with is.

so guard model is VanaciesItem

OR if you want to cast and assign it in one:

guard let model as? VanaciesItem

(I think that what you want most often)

Upvotes: 3

Related Questions