On the auto-unpacking problem of the property system generated by Swift@iboutlet connection

Source: Internet
Author: User
Tags unpack

about the properties that are dragged out by default with an exclamation mark and why we don't need to assign a value to it at first .   @IBOutlet weak var display: UILabel!     because the system automatically helps you initialize it, display is a optional and has been assigned nil before use. ForOptional is usually not a question mark? But there is no difference between the actual types. It is still a optional value, and its value here may be a Uilabek type. But their usage is completely different, This is exactly what the compiler did to help you get things done. But when we change the exclamation mark to question marks, the compiler will report some error prompts. Swift Compiler error Value of optinal type ' UILabel? ' not unwrapped; Did you mean to user '! ' or '? '? , such as forDisplay.text will prompt that the object does not have a member variable called text. That means you can't give it a text message. Of course, you can unpack it when you use it, such as:Display!.text= digit This is also true. But you have to unpack it every time you use it. So it's still a table. The method generated by the system. Sdisplay is not initialized to nil at the start of the class. Only Xib is loaded by the corresponding Viewcontroller. This variable is set and initialized, and it is permanently initialized throughout the class. So these were set earlier. Attribute variables do not have to be initialized at the time of creation as they are written. And it's set up very early, After that it has been set. So this is a optional type, but it will automatically unpack the variable. In some cases, you need to get display before the UI loads, it crashes because it hasn't unpacked yet. This is called unwrapped optional.

About the auto-unpacking problem of the property system generated by the [email protected] connection

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.