Swift Optional (Optional) Type basis, swiftoptional

Source: Internet
Author: User

Swift Optional (Optional) Type basis, swiftoptional

Can be empty type, it is certainly not unfamiliar to those who are familiar with C. In C #, the nominal value type cannot be blank. For example, the int type defaults to 0, and the bool type defaults to false. But what should we add to int? Is an empty type.

So what about Swift. Swift does not have a partition value type or a reference type (maybe, I have not seen this information yet ). So what is this void. The variables in Swift are not empty by default. Check the Code:

An error is returned if you specify nil as a null value for a variable of the String type.

 

So how can we leave a variable empty? Add a variable like C ?. Check the Code:

 

So how can we get the value of a variable that can be empty. Here we have to disassemble the concept of unwarp. For example, if you directly assign a value to another name variable using the above name, an error is returned. To use! Obtain the value.

 

Since the variable can be empty, we can leave it empty when using it. The same as C.

Here are two questions to mention. That's when you use it! = When determining whether it is not nil, pay attention to it. Be sure to be there! = Spaces are added on both sides. Otherwise, an error is reported. If no space is added, the compiler cannot distinguish whether to disassemble the variable or perform non-calculation. In addition, variables of the non-empty type cannot be empty. As mentioned above, variables cannot be empty by default, so it is meaningless to unjudge non-empty variables. Here, students who are used to C #/JAVA will be confused. Because we have developed a risk of null to perform the null check. As follows:

 

Think about it:

Here is a comparison with C .. . Net c # class value type, reference type. All variables of the reference type can be empty. The value type cannot be empty. If you want to be empty, you can use the optional null type. During the development, we can find that there are more reference types than value types. In addition to the FCL Class, all the custom classes are reference types. But Swift is the opposite. The variables in Swift cannot be blank by default. That is, the default value must be given to the value type in C. If this parameter is left blank, use the parameter value. Here we can also see two different ideas for the designers of the two languages. C # I think most of the time there is a requirement for empty Object Attributes/variables. Swift thinks that the attributes of objects are mostly empty. In C #, if you want to leave the variable empty, it is more flexible. However, this also results in the need to be careful when using it, so we are everywhere if (obj! = Null. Swift does not need to Check null everywhere, but it is not flexible enough. For example, when I find that a property may be empty, I need to modify the Declaration and modify the Declaration. All the code used in the previous variable must be changed, to be disassembled. The two solutions have their own advantages and disadvantages, but I prefer C #.

 

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.