Listen to stories and learn Swift series - Xiao Ming and the red envelope (optionals - optional type)

Listen to stories and learn Swift series - Xiao Ming and the red envelope (optionals - optional type)

[[143647]]

The naughty child Xiao Ming and his moonlighting aunt

one

story

The naughty kid Xiao Ming is going to have his 7th birthday. He blocked the door of his house early in the morning and asked for red envelopes from relatives who came to celebrate his birthday. He also said that he would make trouble if they didn't give him red envelopes.
Xiao Ming's aunt is a typical moonlighter. On the day before Xiao Ming's birthday, she just remembered that she seemed to be spending all her money again... What to do? Take a gamble~ No matter how much money can be withdrawn from the bank card, put it in a red envelope and give it to Xiao Ming. Well, it's an empty red envelope, anyway, aunt has tried her best. Xiao Ming, you should know that aunt actually loves you.

Knowledge Points

  1. Optional data is like a red envelope. Whether it is a string or a structure, it can be included in the red envelope.

  2. There are only two states of red envelopes: one is without content, and the other is with content.
    There are only two optional data values, one is nil and the other is non-nil
    Note: nil in OC represents a null pointer, while in Swift, it can only be used to represent the state of Optional type data - the red envelope is empty
  3. "No matter how much money can be withdrawn from the bank card, put it in a red envelope and give it to Xiao Ming" This sentence is to illustrate the common use of Optional: when you are not sure whether an object has a value, you can use the Optional type to wrap it - such as the .toInt() method of a string (a string may not be convertible to int type data at all)

two

story

After eating the cake and seeing everyone off, Xiao Ming happily opened the red envelope. Wow! There are so many Mao Zedongs in this red envelope! Wow! There are actually US dollars in this red envelope! Wow! This red envelope is actually empty!!! Xiao Ming cried, cried so sadly, and the whole person collapsed.

Knowledge Points

  1. Opening a red envelope means forced parsing of Optional data. The parsing method is to use "!" after the optional data.
  2. Before forced parsing, make sure that the data of Optional type is not nil! Otherwise, the program will crash like Xiao Ming.

three

story

In the blink of an eye, Xiao Ming is about to celebrate his 8th birthday. Recalling the empty red envelopes he received when he was 7 years old, Xiao Ming prepared a transparent plastic bag early. When receiving red envelopes, he asked everyone to put the contents of the red envelopes into the plastic bag in front of him before he could go into the house to eat. If nothing fits into the plastic bag, Xiao Ming will throw a tantrum and spit (what a naughty child-_-|||)

Knowledge Points

This story is about optional binding of Optional type data.
Optional binding can be used to determine whether an optional type contains a value, and if so, assign the value to a temporary constant or variable. Optional binding can be used in if and while statements to determine the value of an optional type and assign the value to a constant or variable.

  1. if let money in plastic bag = money in red envelope() {
  2. Xiao Ming. Happily kiss a method
  3. } else {
  4. Xiao Ming. Throwing and spitting method
  5. }

Four

story

Xiao Ming has grown up and knows that it is not good to ask relatives to open the red envelopes in front of him. He doesn't care about some unscrupulous relatives giving empty red envelopes. He has learned to play dumb (how did he suddenly change so much-_-|||). Although he still gets upset when he opens an empty red envelope.
Xiao Ming knows who is trustworthy, and there must be money in the red envelope, so he will not collapse even if he opens it in front of others; if someone is not trustworthy, then he will not open the red envelope of someone who is not trustworthy.
This is growth.

Knowledge Points

Implicitly unwrapped optional types Sometimes in the program architecture, after the first assignment, it can be determined that an optional type will always have a value. In this case, it is very inefficient to check and parse the optional value every time, because it is certain that it will always have a value.
This type of optional state is defined as implicitly unwrapped optionals. To declare an implicitly unwrapped optional, replace the question mark (String?) after the type you want to use as an optional with an exclamation mark (String!).
Implicitly unwrapped optionals are useful when it is certain that an optional will always have a value after it is assigned a value for the first time. Implicitly unwrapped optionals are mainly used in the class construction process in Swift. An implicitly unwrapped optional is actually a normal optional type, but it can be used as a non-optional type, and does not need to be parsed every time to get the optional value.

end

Well, Xiao Ming's story is over! Don't give your juniors empty red envelopes in the future! Otherwise, when he grows up, he won't be able to implicitly interpret you! Haha

<<:  25 great tips for intermediate Git users

>>:  Don’t use subclasses! Swift is protocol-oriented at its core

Recommend

Tips for promoting and attracting traffic on Weibo!

Weibo can be said to be a big brother-level platf...

2016 China APP Category Ranking

In 2016, competition in the mobile Internet arena...

0.1mm "bear" is expected to help humans realize space travel

Author: Duan Yuechu and Huang Xianghong A tardigr...

Using vegetable oil instead of milk? What is non-dairy ice cream?

Some time ago, a certain brand positioned as high...