News for hackers. Stuff that matters.
Re:Apple’s use of Swift in iOS 12 by elcritch on Friday September 28, 2018 @03:57AM (#18091695)
I wonder if there is a way to correlate the number of bug / issues related to binaries with Swift. It’d be interesting to see if Swift correlates positively with bugs/crashes.
Re:Apple’s use of Swift in iOS 12 by 4eleven7 on Friday September 28, 2018 @05:03AM (#18091942)
Actually I disagree. Since I started writing new apps exclusively in Swift, my apps have crashed exactly zero times according to Fabric/Crashlytics. I adopted day one, never looked back. It is an incredibly safe language when used correctly (never force unwrap for example, ever). While previously my Objective-C apps did have numerous crashes in them. I took on an Objective-C contract recently, and it was a nightmare.

Of course, mixing with Objective-C may actually be the cause of issues or crashes? Who knows.

Re:Apple’s use of Swift in iOS 12 by gurkendoktor on Friday September 28, 2018 @06:51AM (#18092373)
As a counter-example, the macOS Dock was rewritten in Swift in macOS 10.12, and Mission Control was super buggy for me then. I'm not blaming this on Swift, most rewrites are buggy at first. And it never outright crashed, but getting stuck in inconsistent states is not much better.

In fact, I would argue that this new trend of defensive programming in Swift will make software worse in the long run. We had a tradition of sending crash reports back to developers. If everyone now starts their methods with `guard let param = param else { return }`, software will silently fail on end user devices, and everything will look fine in Crashlytics/App Store Connect.

I'm not saying that this is what your apps are doing. But I know that Apple bragged about their record low in crash numbers at a time when I ran into different glitches across all of their apps every single day. It's a flawed metric.

Re:Apple’s use of Swift in iOS 12 by dep_b on Friday September 28, 2018 @08:04AM (#18092666)
True, I think Crashlytics still doesn't work very well for logging unusual but not crashing program situations. I would like to have an elegant solution for that. I always throw assertionErrors in case something weird happens handling an optional.

However if you adopt different patterns with Swift you can exclude a lot of optionals, for example if you give every View a State enum with associated values you can avoid quite a lot of optionals:

    class PersonView: UIView {
        enum State {
            case empty
            case loading(personId: String)
            case loaded(person: Person)
        }

        var state: State = .empty {
            didSet {
                switch state {
                /* handle all different states */
                }
            }
        }
    }
Every time you switch state you need to give the right associated value and every time you are in this state this value is guaranteed to be there where before you would have an optional personId and an optional person.

And it's applicable almost everywhere. I barely use optionals anymore unless I really can't replace them with an emum.

Re:Apple’s use of Swift in iOS 12 by Unknoob on Friday September 28, 2018 @09:47AM (#18093279)
> I think Crashlytics still doesn't work very well for logging unusual but not crashing program situations.

Have you tried

    Crashlytics.sharedInstance().recordError(error)
I use it to log errors on API calls in my projects. It comes in handy when I'm using third-party services and they decide to break something on their end.
Re:Apple’s use of Swift in iOS 12 by jchb on Friday September 28, 2018 @07:17AM (#18092488)
The idea of an option type (Optional in Swift) is to use it only for a variables truly assume a "none" value at some point. With the guarantees of such a type it is easier to reason about the correctness of a program - compared to Objective-C where a pointer anywhere may be null at any time. Optional shouldn't be used for most variables and parameters. `guard let param = param else { return }` should be something pretty rare. I would mostly expect to see that for weak self variables in completion blocks.

Also, take into account that in Objective-C you can send any message to a nil pointer, and the result will be nil or 0 (for scalar types), as defined by the language standard.

Re:Apple’s use of Swift in iOS 12 by gurkendoktor on Friday September 28, 2018 @08:40AM (#18092825)
> Optional shouldn't be used for most variables and parameters.

I'd agree if Swift was a general-purpose C++ replacement, but most developers use Swift to write Cocoa apps. viewController.navigationController is optional, view.superview is optional, label.text is optional; all of Apple's frameworks are built on mutable state where almost everything can be or become nil.

(We can make sure that most variables and parameters aren't optional, but that only pushes the problem to other lines of code.)

If we know that we've loaded a view controller from a storyboard, is `self.storyboard!.foo` really a code smell? What else should we do? The type system doesn't let us express what we know/assume about the situation, unless we completely sidestep Apple's controller infrastructure and write our own thing.

> Also, take into account that in Objective-C you can send any message to a nil pointer, and the result will be nil or 0 (for scalar types), as defined by the language standard.

Right, I am not saying that Objective-C handled this any better. But it boggles my mind that we have Swift's complicated type system now, and use it to rebuild an implicit source of errors from Objective-C.

Re:Apple’s use of Swift in iOS 12 by jchb on Friday September 28, 2018 @09:19AM (#18093046)
Replying to your specific examples, I would say `storyboard!.foo` and `viewController.navigationController!` (when you know the view controller is in a navigation controller) is the correct thing to do. And if your assumptions are false that is going cause a trap at runtime, and you will get your crash report.

All the existing Apple frameworks where originally designed for Objective-C, and if redesigned with Swifts type system they could have a much safer API. But what are you suggesting Apple should do? Throwing thousands of man years of their own and third party developers code away, and ask everyone to start from a clean state? I prefer the current approach where, while the Swift language and tooling is maturing, Swift acts as an incremental improvement over Objective-C.

I should also add that if you avoid putting too much of your code in your views (networking, data storage ...) then you can put those components in embedded frameworks that can have a nice safe Swift API.

Re:Apple’s use of Swift in iOS 12 by gurkendoktor on Friday September 28, 2018 @12:12PM (#18094558)
> But what are you suggesting Apple should do?

Apple could have spent less time on their new programming language if they hadn't insisted on reverting every single design decision in Objective-C: the way mutability and constness are handled, NSObject as a root class, naming conventions, the string class, creating their own package manager, etc. There's so much pointless bridging going on.

That would have given Apple plenty of resources to iterate on their current UI frameworks. If the IB/storyboard infrastructure leads to nil-heavy and stringly-typed code, why not push a first-party UI DSL? Why do we even need a third-party platform like Crashlytics to debug errors? Why is it so hard to write UI tests and have them run on a CI? Has IB_DESIGNABLE started working reliably at some point? Why is basic stuff like this not a blocker? [1]

And yes, at some point I think UIKit and AppKit should be scrapped in favor of a new framework (with a slow migration path, not a clean cut). I was hoping for UXKit to be just that, but instead we got the monstrosity that is Marzipan. Apple's focus is on the language, when it should be on the frameworks.

[1] https://bugs.swift.org/plugins/servlet/mobile#issue/SR-6795

Re:Apple’s use of Swift in iOS 12 by dwaite on Friday September 28, 2018 @10:20PM (#18098365)
>Apple could have spent less time on their new programming language if they hadn't insisted on reverting every single design decision in Objective-C: the way mutability and constness are handled, NSObject as a root class, naming conventions, the string class, creating their own package manager, etc. There's so much pointless bridging going on.

But every one of these is arguably a good decision. Swift.String has a significantly better interface. immutable types are a huge win. It doesn't make sense to have a root class when you have non-class value types. You have to change the naming conventions if you change the calling syntax, which was a swift goal.

You also can't take LLVM gurus working on a new language and re-task them to take on UI framework feature adds without a lot of friction.

(BTW, my understanding was that NSObject isn't the only root)

Re:Apple’s use of Swift in iOS 12 by apple4ever on Saturday October 06, 2018 @10:54AM (#18155423)
I disagree. I think all their decisions were bad, which makes Swift a bad language. They focused on the wrong thing.
Re:Apple’s use of Swift in iOS 12 by dep_b on Friday September 28, 2018 @09:41AM (#18093236)
I did notice that a lot of Objective-C's uncertainties were paved over when the years passed. Most libraries feel a lot more Swift-y but there are some problems and it's a pity that Storyboards are one of them. I hope they'll revise the system some day.
Re:Apple’s use of Swift in iOS 12 by wool_gather on Friday September 28, 2018 @08:16AM (#18092716)
The point stands, however, that just because developers are forced to handle the `nil` somehow, doesn't mean they're handling it in a sensible way that makes for consistent UX. Crashing sucks from a user perspective, obviously data corruption is even worse, but just throwing up your hands and returning early from a view controller method doesn't really do anything for the user either.

I generally find it useful to have the concept of Optional, making me think about "could this thing be missing?" explicitly. But I've started to wonder if it is, rather than "preventing an entire class of bugs", actually just making them pop up elsewhere when Optional values start brushing up against that top level of user-visible stuff.

Maybe we (I) just need to get better at thinking about missing data conditions at the product design stage, or more robust defaults.

Re:Apple’s use of Swift in iOS 12 by jchb on Friday September 28, 2018 @09:27AM (#18093115)
> when Optional values start brushing up against that top level of user-visible stuff

Perhaps you are under-using sum types - or enums with associated values as they are called in Swift. Difficult to get into details in this format, but for example, instead of:

`enum State { case connected(Connection, SomeOtherStateRelatedToConnected) case disconnected }

let state: State `

You have: ` var connection: Connection? var someOtherState: SomeOtherStateRelatedToConnected? `

Re:Apple’s use of Swift in iOS 12 by lewisj489 on Sunday September 30, 2018 @05:53AM (#18105451)
j
Re:Apple’s use of Swift in iOS 12 by bunnycorn on Friday September 28, 2018 @12:10PM (#18094538)
> In fact, I would argue that this new trend of defensive programming in Swift

Swift is not defensive, Swift can be defensive, but it can not if you don't want to.

It's much more "offensive" than Objective-C, for example.

Just a "!" and it's the same as Null Pointer Exception == Fatal Error.

Re:Apple’s use of Swift in iOS 12 by Someone on Friday September 28, 2018 @07:22AM (#18092512)
”the macOS Dock was rewritten in Swift in macOS 10.12, and Mission Control was super buggy for me in that version. I'm not blaming this on Swift”

Foo was rewritten in Swift, and bar was super buggy. Why would you blame that on Swift?

Re:Apple’s use of Swift in iOS 12 by gurkendoktor on Friday September 28, 2018 @07:35AM (#18092551)
Mission Control is part of Dock.app.

Turns out one of these bugs even has its own blog post: https://medium.com/@julioromano/working-around-an-infamous-m...

Re:Apple’s use of Swift in iOS 12 by LeoNatan25 on Friday September 28, 2018 @04:10AM (#18091744)
If anything, since a lot of stuff is being rewritten from scratch, a lot of new bugs appear that were not there. At least that's my experience on the Mac with software that has been rewritten in Swift.

Obviously, it's not Swift's fault for that, but even if people at Apple make rookie mistakes such as using force unwrap operator, it tells something about the language.

Re:Apple’s use of Swift in iOS 12 by dep_b on Friday September 28, 2018 @08:05AM (#18092670)
> Obviously, it's not Swift's fault for that, but even if people at Apple make rookie mistakes such as using force unwrap operator, it tells something about the language.

It's still often suggested by Xcode if you use an optional which has not been unwrapped in any way. "Click here to make error go away" adds an !.

Re:Apple’s use of Swift in iOS 12 by wool_gather on Friday September 28, 2018 @08:20AM (#18092735)
Good point. Fortunately this fixit was improved over the summer: https://forums.swift.org/t/resolved-insert-is-a-bad-fixit/10... I believe it made its way into Swift 4.2/Xcode 10.
Re:Apple’s use of Swift in iOS 12 by dep_b on Friday September 28, 2018 @08:50AM (#18092871)
Good stuff. Now to fix my only other pet peeve which is declaring IBOutlets with a ! by default. In the ideal world IBOutlets would be checked compile time unless they're declared with a ?
Re:Apple’s use of Swift in iOS 12 by wool_gather on Sunday September 30, 2018 @11:55AM (#18106676)
Well, they have to be Optionals one way or another, because they can't get hooked up until after init[0], and Swift won't allow that. But yeah, I imagine this is on someone's todo list somewhere. IB already knows whether things are connected properly, and actually so does the source editor, since it shows the little circles in the gutter.

--

[0] Unless you could invert the order of the unarchiving...not sure what difficulties that would cause.

Re:Apple’s use of Swift in iOS 12 by sgt on Friday September 28, 2018 @08:47AM (#18092855)
If Apple's own developers are just slapping '!'s on optionals to make errors go away, I would be seriously concerned.
Re:Apple’s use of Swift in iOS 12 by dep_b on Friday September 28, 2018 @08:53AM (#18092893)
Well hiring practices in the US tend to be skewed toward people knowing a ton about data structures and algorithms versus people having years and years of practical knowledge programming things. I wouldn't be surprised. I've seen Swift code built by pretty smart people that was one big exclamationfest.
Re:Apple’s use of Swift in iOS 12 by the_clarence on Friday September 28, 2018 @06:38AM (#18092311)
Can you explain force unwrap?

Actually I was wondering with Swift had a lot of these things I don't see much in other languages. Optionals for example. Is it because it us focused on coding highly responsive UIs where different states might not yet be available?

Re:Apple’s use of Swift in iOS 12 by jernfrost on Friday September 28, 2018 @09:52AM (#18093316)
It is not much in other languages because those languages are old. A lot of the Swift features have been in more academic functional languages for a long time. However it has taken time for the mainstream languages to adopt these ideas. The industry moves much slower than academic niche languages.

But most new languages has some variant of this stuff: kotlin, scala, rust.

I program a lot in Julia which is a script language and it does not have allow you to use null freely either. May seem odd for a language that does not type check at compile time. But it actually makes sense.

Re:Apple’s use of Swift in iOS 12 by saagarjha on Friday September 28, 2018 @07:02AM (#18092419)
Force unwrapping is what it sounds like: you take an optional, and forcefully convert it to a nonoptional. Obviously there must be a valid value at this point in the program, or it will crash. Optionals are useful not only for UIs, but for handling any sort of state where it's possible for something to not have a valid value at any point in time. It just hoists such a state into the type system rather than checking this at runtime.
Re:Apple’s use of Swift in iOS 12 by MaxBarraclough on Friday September 28, 2018 @08:25AM (#18092757)
These bugs are likely to be of a lower average severity though, no?

C-family languages let you take off your whole leg with the slightest mistake.

Re:Apple’s use of Swift in iOS 12 by LeoNatan25 on Friday September 28, 2018 @02:22PM (#18095697)
No, clearly not. Take a look major components rewritten in recent years, such as Xcode code editor, new build system, Dock, those are crashing way more than their ObjC/C counterparts for whatever reason. A crash is a crash. You may claim that errors here are “less fundamental”, but the fact remains that the new software crashes more than the old.
Re:Apple’s use of Swift in iOS 12 by wool_gather on Friday September 28, 2018 @06:37PM (#18097633)
Right. Users don't give a hoot what language the software is written in, what design patterns you're using, if you're functionally immutable. The code is there to serve a purpose. If it crashes, it fails; the root cause doesn't really matter to the user.
Re:Apple’s use of Swift in iOS 12 by elcritch on Friday September 28, 2018 @10:01PM (#18098314)
This feeling that “man dock.app is crashing more” followed by a few otool queries showing more Swift libraries gives me a vague suspicion that Swift rewrites have been leading to more end user noticeable crashes. I’d find it interesting to see some actual statistics on it in the form of “x crashes per 1 hour of program usage”. Perhaps it’d actually be lower than Obj-C but it doesn’t feel that way. Is it due to Swift error handling or just to rewrites? This would be an ideal time to get such statistics ! As it’s not often a giant platform starts switching to a new language and offer the chance to gather real stats rather than bikesheading or doing undergrad cohort experiments would be very interesting.
Re:Apple’s use of Swift in iOS 12 by MaxBarraclough on Saturday September 29, 2018 @10:20AM (#18100478)
Sure, if it's a good old whole-program-crash either way, that means they're equivalent severity. It's not my intention to softball Swift with some nebulous idea of what counts as a proper 'fundamental' bug.

In defence of Swift, it could still be that they spent less time bug-hunting than when they were developing in Objective-C, to reach the same, or inferior, level of program correctness.

Also, we're comparing a new product to a mature one. Were the original Objective-C programs stable in their first release?

Re:Apple’s use of Swift in iOS 12 by LeoNatan25 on Saturday September 29, 2018 @01:00PM (#18101435)
My point isn’t to say Swift is bad. I personally don’t like it, but that isn’t the point. In my original comment, I wanted to say that rewriting software just because of a new language is probably a bad idea.

Apple could have continued using the existing software, rather than just throw away stable code for the sake of new devs to use a new language.

Re:Apple’s use of Swift in iOS 12 by MaxBarraclough on Saturday September 29, 2018 @02:30PM (#18101960)
Agree completely - I'm not convinced that Swift is so much better than Objective-C that it makes sense to throw out mature production code.

Mandatory link to Spolsky's blog post on exactly this:

https://www.joelonsoftware.com/2000/04/06/things-you-should-...

Re:Apple’s use of Swift in iOS 12 by adamnemecek on Friday September 28, 2018 @11:03AM (#18093928)
You are basing that on nothing.