Although both are based on the Cocoa and CocoaTouch frameworks, and although both are statically typed languages with dynamic running modes, there are still quite a few differences between Swift and OC. Therefore, I hope that through such a series, in the form of storytelling, I can map the syntax and concepts in Swift, which are quite different from those in OC, with the little things in life, so that knowledge can be integrated into life and knowledge can be melted into life. Oh my god, this is so pretentious, I even blushed╮(╯﹏╰)╭ The naughty child Xiao Ming and his moonlighting aunt Story 1: 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 a.Optional data is like a red envelope. Whether it is a string or a structure, it can be included in the red envelope. b. 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 c. "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) Story 2 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 a. Opening a red envelope means forced parsing of Optional data. The parsing method is: use "!" after the optional data. b. Before forced parsing, make sure that the data of Optional type is not nil! Otherwise, the program will crash like Xiao Ming. Story 3 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.
Story 4 Xiao Ming has grown up and knows that it is not nice to ask relatives to open red envelopes in front of him. He no longer cares about some unscrupulous relatives giving empty red envelopes. He has learned to play dumb (how could he change so much suddenly -_-|||). Although he still collapses when he receives an empty red envelope. Xiao Ming knows who is trustworthy, and there must be money in the red envelope. He will not collapse even if he opens it in person; if someone is not trustworthy, he will not open the red envelope of the untrustworthy person. This is what growing up is. Knowledge Points Implicitly Unwrapped Optional Types Sometimes in the program architecture, after the first assignment, it is certain 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 type is just a regular optional type, but it can be used as a non-optional type, without having to parse it 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 |
>>: Jack Ma, Lei Jun, Zhou Hongyi and other bigwigs followed suit and starred in TV series
Actual value cannot bring users. What users care ...
Many friends are not familiar with Douyin corpora...
There is no doubt that Android is a very popular ...
Drinking water is a very common thing, but some p...
This set of training courses includes Fanyin’s Ku...
SEO optimization is an ongoing process. Managing ...
In recent years, the financial industry has been ...
Investment and Financing Analyst Training Class (...
Marketing is like cooking Chinese food. The same ...
Introduction to the 2022 edition of MyBatis-Plus ...
Humans have conjoined twins, but it seems that we...
Wei Toutiao is a sub-function of Toutiao. It is r...
Advertising traffic is too expensive? SEM prices ...
In our home life, we use plastic wrap almost ever...
Nowadays, the short video industry is developing ...