Has not initializers - Swift IOS

Asked

Viewed 39 times

0

Good morning friends.

For some reason, I’m having this mistake, as I’m new on this platform I’m not able to identify the problem, could someone please help me?

Thank you!

inserir a descrição da imagem aqui

1 answer

3


The variable image needs to be initialized in your creation or within the class constructor, or you can say that it can be optional as follows:

var image: UIImage?

This way you will have to check that it is not null when using, or you can declare so:

var image: UIImage!

So you guarantee that it will be initialized in use, but if it is not will give error.

  • Thank you very much for the answer that was exactly the problem, because I have one more question, for example , what is the difference between 'image = Uiimage()' ' for 'image: Uiimage''

  • @Brunobafilli Como Swift is a dynamic language (however strongly typed), when you do var image: UIImage would be something like UIImage image in another language (object not yet initialized), already var image = UIImage() you are instantiating (initiating) an object of the type UIImage and he takes care of typing the variable for you.

  • I understood, so it means that when I do image =Uiimage() I start a variable of the type of the same object, if I do not do it I create a variable of the type of the object because it is not yet ready for use until initialization ?

  • That’s right my dear.

  • Sorry to bother you with so many questions, but why does it work var button: Uibutton button = view.viewWithTag(i) as! Uibutton , and this is not var image: Uiimage image = Uiimage(named: "Circle.png")!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.