Swift UI - galloping across Europe

    image


    22:35. Delight


    Viewed WWDC 2019 Key Notes. The expected declarative UI really came true, and this is truly a universal event for the world of iOS development. “We need to write an article about this,” I thought, and thousands more iOS developers around the world are in exaltation.


    05:30. Tutorials


    Swift UI - A new framework developed by Apple, written in Swift, designed to declaratively describe the UI in code.

    I noticed that every year in terms of documentation, Yabloko is getting cooler and cooler. This time, under Swift UI, they saw a few complete tutorials with step-by-step addition and interactive display of the result on view, and at the end they carefully added control questions to consolidate the results. Well, just a fairy tale! There are also links to example projects.



    Handsomely!


    I will not retell the tutorial in Russian, in such a beautiful form it is better to poke it in the original source. I will describe my impressions and observations about this whole story with Swift UI and a little indulge in it.


    07:00 a.m. Installation


    The new Xcode has a new code editing mode - Editor And Canvas.



    I did not see the canvas at once - for this it is not enough to download Xcode 11.0, you also need to update Makos to 10.15. Without it, Xcode will work, but without the delights of a canvas and, possibly, something else.


    I was glad that when you select the code, the corresponding element in the canvas is also highlighted.


    New axis, exampl'y launched. Is it crumbling? Well, yes, it does. Does the backlight fall off? No, of course - the same has never happened in Xcode;) But the canvas works, and the view changes are reflected instantly, if this is not a table with complex cells.



    09:22. New project


    When creating a new project, the Use Swift UI option is now available and the project is created with the appropriate configuration.



    A new file is immediately evident in SceneDelegatewhich the window and its root view are created. But AppDelegatethere is not a word about him. But there is a new method that creates UISceneConfiguration:


    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
            // Called when a new scene session is being created.
            // Use this method to select a configuration to create the new scene with.
            return UISceneConfiguration(name: «Default Configuration», sessionRole: connectingSceneSession.role)
        }

    Well, she herself Default Configurationlies in Info.plistand is SceneDelegateindicated there. Everything fell into place.



    But back to SceneDelegate- the launch is in it.


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
            // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
            // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
            // Use a UIHostingController as window root view controller
            let window = UIWindow(frame: UIScreen.main.bounds)
            window.rootViewController = UIHostingController(rootView: ContentView())
            self.window = window
            window.makeKeyAndVisible()
        }

    UIHostingController- this is normal generic UIViewController, which can have any type of content under the new protocolView


    open class UIHostingController : UIViewController where Content : View {
        ///
    }

    The protocol Viewis obscene:


    public protocol View : _View {
        associatedtype Body : View
        var body: Self.Body { get }
    }

    That is, he only needs to realize body.
    But the trick is that a Viewwhole ton of extensions is written on this protocol , for example, for hanging gestures, for tracking the appearance and disappearance of a view from the screen, for indents, frames, and much, much more. You can see all this in the dock View | Apple Developer Documentation . This means that any view you created (under the protocol View) out of the box gets a bunch of all kinds of superpowers!


    We pass to ContentView.swift.


    struct ContentView : View {
        var body: some View {
            Text(«Hello World»)
        }
    }

    It's simple: we create a view from the already implemented Views and Controls | Apple Developer Documentation . Could make it more complicated using various containers View Layout and Presentation | Apple Developer Documentation and the view that we already created ourselves.


    Layout with Swift UI is a separate story about which many more materials will be written, and Apple also has a worthy tutorial. I will not dwell on it. Let's go back to the second part ContentView.swift, there is such a code:


    #if DEBUG
    struct ContentView_Previews : PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    #endif

    Obviously by name, it is he who is responsible for what is displayed in the canvas - and he will display previews, in our case ContentView().


    Let's try to create a screen with a primitive table:


    struct ContentView : View {
        var birds: [Birds] = []
        var body: some View {
            List(birds) { bird in
                Text(verbatim: bird.name)
            }
        }
    }

    All. Done. Simple, concise, elegant. Here it is all the charm of a declarative UI!


    It can be seen that Listthis is a table under the hood. But not a UITableView, but some UpdateCoalesingTableView.



    And it’s also clear that there is no auto-layout. There is no contstaint's, everything is on frames, which means that there are no these complex systems with linear equations and a bunch of calculations. But, on the other hand, the layout is adaptive and the frame is somehow calculated — let's see in future WWDC sessions exactly how.


    Swift UI - is it a wrapper over UIKit? Yes and no at the same time, it seems.


    Joe Groff tweeted the following:


    "Some entities are wrappers over UI / NS views, but the fact that they do this and what kind of view they wrap is a thing that can change."

    Оригинал:
    Some things wrap UI/NS views, but whether they do, and what view type they wrap, is subject to change. Best to think about it as a distinct thing.


    Что заметил еще:



    11:00. Итого


    Увы, все эти прелести


    @available (iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)

    То есть переходить на них — это отказываться от старых осей, что слишком радикально и лишено заботы о пользователе.


    I am sure that the mechanism is still crude, there will be many more changes, and a lot of bugs will pop up, and this is natural. But in a year or two it will be possible to implement in the product.


    Swift UI is a direct revolution in the world of people calling themselves iOS developers, and it's cool that this new road, though not ideal, has opened.


    In the meantime, nothing prevents you from using this in your pet projects, getting your hand in and getting aesthetic pleasure :)


    Also popular now: