قالب وردپرس درنا توس
Home / IOS Development / Swift Changes are considered harmful • furbo.org

Swift Changes are considered harmful • furbo.org



I like Swift. We use it for new products like Linea, which was written from the ground of the new syntax. Despite this, I still consider the language harmful.

The best way to understand the problems is to imagine that you are an iOS developer who will write his first macOS app.

You will find this sample code and it has exactly what you need:

PhotoEditor: Design of Modern Cocoa Applications

The sample demonstrates several cocoa technologies and features including:

  • Storyboards
  • Title bar for unified window
  • Full Size Content
  • Sandboxing
  • Resume (state restoration)
  • Modernized Event Tracking
  • Modernized Drag and Drop
  • UI Validation
  • The best part is that these pieces are fresh ̵
    1; It was published in October 2016 for Xcode 8 running on MacOS Sierra. Perfect!

    Build and Not Run

    Everything is great until you try to build and run that project. Xcode shows 12 issues that you need to handle before you can try the sample.

    A number of errors are due to private protection levels on instance variables. Unfortunately, there is no automatic solution for this problem. But even if you're not an expert in Swift, you'll find that changing the definitions of public seems to clean up things.

    A more difficult problem is a method that does not override its super class: [19659020] override func observeValue (forKeyPath keyPath: String?, By object: AnyObject?, Change: [NSKeyValueChangeKey : AnyObject]?, Context: UnsafeMutablePointer ?) {

    }

    When you try to make the automatic fix for this problem, the code changes, but the error remains:

      override func observeValue (forKeyPath keyPath: String?, Object: Any?, Change: [NSKeyValueChangeKey : AnyObject]?, Context: UnsafeMutableRawPointer ) {
    ...
    }
    

    This is a problem you must solve with Google. After a few failed attempts, you finally land a solution at Stack Overflow:

      override func observeValue (for KeyPath keyPath: String?, By object: Any?, Change: [NSKeyValueChangeKey : Any]?, Context: UncertainMutableRawPointer?) {
    ...
    }
    

    So what does this whole test code tell us?

    Learning with Swift is Hard

    I can hear many of you say, "just fix the migration tool automatically!" But that's not the problem here. [19659002] Swift is a simple language to learn and has some amazing innovative tools to help beginners. But as soon as you're over the basics, the constant Swift syntax changes become an obstacle to learning.

    For public versus private the problem above, I wonder if this distinction is important in designing macOS apps. Do I break anything? And why do any of the instance variables OK work like privately ?

    The key value observation problem is an even greater concern. As an iOS developer, it is likely that you have not been exposed to this important part of macOS app design. You are forced to solve a problem where you have absolutely no experience. You do not know what you do not know.

    And the part that is broken looks at changes in contentLayoutRect : the mechanism that deals with everything from placing live content to support fullscreen windows. It is the core of the modernization of the windows system that started in Yosemite. For many developers, this is why they downloaded the test code in the first place.

    Take a look at the Stack Overflow page again. There is some good information there, but it is completely hidden by Swift code which is no longer relevant.

    Code Migration is a Crutch

    It's come to the point where every time I come across some information written in Swift, especially on Stack Overflow, I'm crawling. I know I will not only figure out the code, but also the version used for the answer.

    For many of us, sample code is the best documentation possible. It not only shows an API, but also how it works in context. This test code is displayed in many places besides the web: it's in WWDC videos, PDFs of presentations, books and downloadable projects like the one used above.

    And none of this code can be solved easily with a migration tool.

    It is up to the author of the original information to make an audit when the next version of Swift rolls out and breaks things again. Some Stack Overflow answers will get this treatment, but most will not. Do you think Apple should edit the videos with non-working Swift code?

    In my last book, all test codes were made in Objective-C. I would do the projects in Swift, but after seeing the language change three times I realized that such examples would be outdated in months.

    And that's too bad. [19659016] It's ok to turn up

    Brian Kernighan has some experience with programming languages. This comment from a recent talk about successful language design sums up the situation nicely:

    If you design a language and others use it, you learn very quickly that you have screwed up. … this tension between changing something because you mistakenly or because you have a better idea, as opposed to keeping it stable so that people who learn something do not have to keep up with your language.

    … [19659002] Swift keeps change and people have code that works and "oops" it no longer works with the next version. And that's a problem. And how do you stop it?

    I think the answer to this question is to realize that everything in our business is missing. Perfect is the enemy of good.

    With the latest announcement for Swift 4, it feels like people are still searching for perfect, when many of us want is just a great and stable language.

    For that purpose, I invite you to copy this radar.


Source link