SwiftUI【0】

SwiftUI【0】

Recently I started to learn about SwiftUI. Refer to Jianshu, CSDN and some documents of Baidu to help me have a better understanding.

Preface

Of course, the highlight of Apple 's WWDC in 2019 is SwiftUI: a new declarative syntax, a binding API, and a responsive framework Combine. All of this heralds a revolution in the Apple Native layout system. To this end, Apple has made efforts in many aspects, which contributed to the current appearance of SwiftUI. Want to learn about the new features of Swift, SwiftUI data flow and SwiftUI layout system? Let's see it together.

Insert picture description here

Get started quickly

The official teaching website
can enter to understand related concepts and learn technical grammar.

What is SwiftUI?

Apple official introduction

写更少的代码,打造更出色的 app。
SwiftUI 是一种创新、简洁的编程方式,通过 Swift 的强大功能,在所有 Apple 平台上构建用户界面。
借助它,您只需一套工具和 API,即可创建面向任何 Apple 设备的用户界面。
SwiftUI 采用简单易懂、编写方式自然的声明式 Swift 语法,可无缝支持新的 Xcode 设计工具,让您的代码与设计保持高度同步。
SwiftUI 原生支持“动态字体”、“深色模式”、本地化和辅助功能——第一行您写出的 SwiftUI 代码,就已经是您编写过的、功能最强大的 UI 代码。


Apple's official introduction revealed a lot of hidden information, such as breaking through the barriers between all Apple devices through SwiftUI, and realizing the unification of the Apple world. Through SwiftUI to bridge the gap between programmers and designers, making App development more artisan spirit. Provide the expressiveness of your code through SwiftUI, a sentence is worth a thousand words.

Understanding of the introduction

SwiftUI is a user interface toolkit that allows us to design applications in a declarative manner. This is a peculiar way. We tell SwiftUI how we want the UI to be displayed and how it works, and it knows how to achieve this when the user interacts with it.

SwiftUI is a very simple and innovative method that can use the powerful capabilities of Swift to build user interfaces on all Apple device platforms. With SwiftUI, developers can build user interfaces for all Apple devices using only a set of tools and APIs. SwiftUI uses declarative Swift syntax that is easy to read and write, and can work seamlessly with the new Xcode design tools to keep your code and design in perfect sync. SwiftUI automatically supports dynamic typing, dark mode, localization, and accessibility. Your SwiftUI code will become the most powerful UI code you have ever written.

SwiftUI also acts as a cross-platform user interface layer that can run across iOS, macOS, tvOS and even watchOS. This means you can now learn a language and a layout framework, and then deploy the code anywhere.

SwiftUI advantages

  • Apple says SwiftUI is the shortest path to building great apps on every device. Apple says SwiftUI is the shortest path to building great apps on every device.
  • SwiftUI satisfies programmers’ needs for complex pasting code. Traditional IB and storyboard editing page methods make it difficult to reuse code.
  • Cross-platform convenience

Swift 5.1 new syntax and introduction to SwiftUI algorithm

Reference articles: In-
depth interpretation of series of articles|Those things behind SwiftUI

About swiftUI, just read this one

Application examples

Example 1:

To implement a list, click on the item of the list to jump to the corresponding details.
Insert picture description here

Example 2:

A simple example based on SwiftUI
introduces the layout of the UI by declaring and modifying the view, and the use of state variables to update the UI.
Insert picture description here

Example 3:

SwiftUI in action: develop an app from 0 to 1

Insert picture description here
The author uses swiftui to develop an APP for recording habits. The page is concise and has a concise taste of iOS. The article also has open source code for viewing;
the effect of the APP is as follows:

Insert picture description here

Use problem

  • Where can SwiftUI be used?

SwiftUI runs on iOS 13, macOS 10.15, tvOS 13, and watchOS 6, or any higher version of these platforms. This means that if the application you use must support iOS N-1 or even N-2 (that is, the current version and one or two versions before that version), then you may even need a year or two to consider migrating to SwiftUI.

However, it is important not to treat SwiftUI as a multi-platform framework similar to Java's Swing or React Native. The official statement seems to be that SwiftUI is not a multi-platform framework, but a framework for creating applications on multiple platforms.

It may sound the same, but there is an important difference: Apple is not saying that you can use the same SwiftUI code on every platform, because some things are impossible – you can’t use the Apple Watch’s digital crown on a Mac, for example , And similarly having a tab bar on the watchOS app will not work.

  • Will SwiftUI replace UIKit?

will not. Many parts of SwiftUI are built directly on top of existing UIKit components, such as UITableView. Of course, many other parts are not. They are new controls presented by SwiftUI rather than UIKit.

But the point is not the extent to which UIKit is involved. Instead, the point is that we don't care. SwiftUI more or less completely obscures the behavior of UIKit, so if you write an app for SwiftUI and Apple replaces UIKit with a singing elephant in two years, then you don’t need to care – as long as Apple makes the elephant have the same method And attributes can expose the UIKit to SwiftUI, and your code remains unchanged.

  • Does SwiftUI use auto layout?

Although Auto Layout will definitely be used behind the scenes for certain things, it is not exposed to us as SwiftUI designers. Instead, it uses a flexible box layout system, which will be familiar to developers from the Web.

  • Is SwiftUI fast?

SwiftUI is surprisingly fast-in all my tests so far, it seems to surpass UIKit. After talking with the team that did this, I began to understand why: First, they actively flattened the layer hierarchy so that the system does not have to do more drawing, but in the second step, many operations completely bypass the Core. Animation, went directly to Metal for additional processing. speed.

So yes: SwiftUI is very fast, and all of this does not require us to do any additional work.

  • Why can't I see my code preview?

When using SwiftUI, it is very helpful to be able to view the view code and view preview (appearance) at the same time. If you can see the code instead of the preview, it may be that you have not yet upgraded to macOS 10.15; a preview is required to work properly.

  • How well does the code match the preview?

When making any changes to the preview, it also updates the generated code. Similarly, if you change the code, it will also update the user interface. Therefore, the code and the preview are the same and are always in sync.

Prospects

What about Swift?

Author: iOSer
Source: know almost

Thanks to the open source of swift and the appeal of Apple, swift is developing very well. Has been unanimously approved by the majority of developers. Apple also attaches great importance to it. Some new libs and apps have been written in swift. Large foreign companies such as Uber and LinkedIn have used swift to develop for a long time. These actions prove that Swift is no longer a toy language that can be boldly used in development. Although there are still ABI unstable, and Xcode indexing will make people feel slow and other issues. But compared to the great progress of OC, more developers have chosen to endure, hoping that Apple can continue to optimize. However, OC's runtime is still irreplaceable, and there is no reason to ban it from a commercial point of view . So the two will exist with each other for a while. But I believe that swift's occupancy rate exceeds OC nodes will soon come. I think many people insist on OC because they only know OC. The mobile market is saturated. In 2008, Apple released the first SDK, and Android 1.0 was released at the end of the same year. The first year of mobile development. Mobile development grew out of nothing and has spread to all aspects of life.

From the mobile phone shipments in 2019 and observations around us, it is easy to conclude that the rapid growth of the cake of mobile development has ended. What does this mean? When an industry is growing rapidly, talent must be in short supply. So the company was forced to accept many newbies and was very friendly to them. I believe everyone has witnessed the miracle of employment in the past one or two years: it is an individual who can get it. So for many people who just make ends meet: this door has been closed. You continue to chase the next craze. I heard that JavaScript is going to unify the world, how about going to learn a front-end in 21 days? Closer to home, will mobile development be over?

My friend, IMHO: it’s not that mobile development is not good, but you are not good. Before the mobile wave, Internet traffic was all on the desktop. How many people were able to sit in front of computers and surf the Internet in 2008? Now, WeChat has more than 500 million active users this quarter. Although iOS's share is only a dozen percent. But this is more than ten percent that cannot be ignored. If the company has a mobile business, it will definitely be an iOS client.

So the iOS development market still exists, and it's not a small cake . In the first few years of mobile development, the only way to do functions on the mobile side was to develop Native apps. But this is the case with business. As demand increases, there will always be solutions to improve efficiency or some automation. I believe many people have seen similar articles: you don’t need to develop an app, you only need an official account. A while ago, WeChat launched a small program, the people who have never seen the world eat melon are also excited. In fact, this is just an economic account. Now for products, there are more choices. If a product itself has very low requirements for native capabilities, of course it will choose a cheaper way.

Except for the WeChat applet, which is embedded in WeChat. The Progressive Web App, a new technology initiated by the traditional web, is also worthy of attention. Simply put, the web can also have a convenient channel to generate a local app, and get some capabilities such as push and local storage. A little bit helpless is that iOS does not currently support pwa. Apple announced last year that it will support this standard within 5 years, but all other manufacturers except Apple already support it, and now it is supported on Android. So although this has not happened yet, there should be new developments in the near future. All in all, many mobile products no longer need to develop a native app.

However, don't be sad too early in everything, maybe there is something worse? React Native VS Weex I think those who use RN will eventually cry. Forget it, I know you will choose to be stubborn. Let’s talk emotionally first. Do you believe in Jack Ma's father or 404 Berg? RN's current flaws are: 1. The package cannot be updated incrementally 2. The long list is not optimized (the catastrophic tableview cell is not reused) 3. It does not support the web. Of course, these are not impossible to achieve, yes, you can implement the above yourself. The three puzzles. But what if there is already a ready-made solution? Yes, Ali's weex is already ahead of RN. I don’t know if Ali’s 996 worked harder or Dad Ma’s money was more, but that’s the fact. RN is a pure open source project, so it is impossible for RN to have a killer feature weex does not have in the future. The comparison is who walks faster and sees farther. Everyone must be confident. Our strength in mobile development is already world-class. So, the unfortunate news for native: even for native apps, many functions have to be implemented by the front end. This account is very clear: it turned out to need a front end, an iOS, and an Android. Now it only needs to be written once on the front end. A rough calculation saves two-thirds of the cost. But it's like run anywhere that java blew from the beginning. Any technology has its application scenarios, not everyone can use this technology. However, according to my observations, after optimizing the performance problem, there are a lot of pages in an app that do not need to be written natively, and this weex solution can be used to solve them. Moreover, the improvement in development efficiency is so obvious that there will be a large number of pages that no longer require native code to be released in the future. The future of mobile developers must first accept the fact that we are living in an era of fastest technological change. Unfortunately, the software industry is the most volatile industry of all industries.

Moore's Law doubles computing power every 18 months. What in other industries can double every two years and continue for decades? In other words, I chose software development. In the past two decades, apart from C++, C, and Java are still in large demand, the choice of other technologies or languages ​​has experienced ebb and flow . So how many languages ​​have programmers have since the beginning? Therefore, it is normal for a technology to rise and be left out on the scale of ten years. Our parents did not believe that state-owned enterprises would be laid off in the 1970s. You shouldn’t be familiar with a technology that can support you for a lifetime.
How do you understand iOS programming? After all, a certain technology or a certain programming language is just a tool. It turns out that you used chopsticks, and then you came to a western restaurant. You can't eat with only a knife and fork? You deserve to starve to death. It’s not that no one wants iOS. Many companies are now hiring iOS developers. The main reason is that many iOS developers no longer have the kind of innovative ideas that can’t keep up with the market demand . However, they have not recognized themselves, so they are eliminated. This industry is to blame. Every industry will be saturated and stabilized, but the survival of the fittest will always exist, so as long as your technology has been innovating to meet the needs of the market, there will be no one wants it. In the final analysis, I still eliminated myself

reference

The following are some references and citation addresses of this article:

Guess you like

Origin blog.csdn.net/cena1001/article/details/109105164