Preface
Swift has been out for a long time, and the language ranking in July has reached 16th. It can be seen that Apple's Development Department and various IOs developers have invested a lot in this language, the momentum of Swift's replacement of OC on the iOS platform is unstoppable, otherwise Apple does not need to develop a new language.
At the same time, Google is also rewriting the android API to adapt to the later Android devices developed with go. Although SWIFT is not making great progress, however, it can be seen that major giants are also deploying the Smart car market in the future, whether it is smart furniture or wearable devices by introducing new languages. In short, this will bring momentum, so I want to say to the developers of. NET and C #, if they don't want to miss the next wave, let's get out of their comfort zone and fight.
Start
Many of these articles are from blogs outside China. I would like to thank Daniel for sharing their experiences in using SWIFT to develop apps. I hope you can tell me what is wrong.
The swift language itself does not have much to explain in detail. It is certainly much better for people who have programming basics in other languages to adapt to their syntax than they had been in contact with OC at the beginning, the way I learned and entered a new language is to look at his programming paradigm and start to take a small demo, so the article also follows this approach.
Too much nonsense. Let's get started!
Hello world!
Create an xcode project, create a single view IOS application, and name it day1helloworld. Be sure to select the iPhone and the swift language.
After the project is successfully created, drag a label, a text field, and a button in Main. storyboard.
Modify the label size according to your habits:
After completion, our storyboard should be changed:
The UI of our first project is complete. We can run the application to see what it will look like on a real machine in the future:
In the next step, we need to establish a connection between the UI elements and Code. Those who have learned about xcode development should know that xcode UI and code connections are completed by dragging and dropping:
When you release the mouse, the system prompts you to change the name to label:
Similarly, we create a connection for the text field of another read-only control named "textfield", but note that when we establish a connection for the button in the corresponding UI, it should be action:
After the element and code connection are established, we should think about the button action. In the ibaction code of the button, add the following code:
label.text = "Hello, \(textField.text)!"
It means that when we click the button, the label text is changed to hello, + textfield input information.
The Code is as follows:
The simplest swift-based app is completed in this way. Let's click run to see the result.
It's amazing. Just like we thought, using SWIFT, we developed apps much faster than the previous oC, and the entire code is readable and more appealing.
Well, the first version of swift and demo is over. Later, we will help you build a demo through SWIFT.