TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

SwiftUI Defaults Considered Harmful

185 pointsby Aqua_Geekabout 5 years ago

29 comments

saagarjhaabout 5 years ago
SwiftUI has defaults because nobody knew what UIKit&#x27;s were supposed to be. Some of them were fiddly secret layout guides in Interface Builder, and some were accessed through the magic &quot;-&quot; in a VFL constraint, but overall it was very difficult to actually know how much spacing things <i>should</i> have. And on Apple&#x27;s platforms, this matters: users know what things should look like, and if you&#x27;re off you just look out of place. And if they change in the future, which they have on occasion due to new iOS versions or hardware (iPhone X, anyone?) you might end up off even if you didn&#x27;t change anything in your code.<p>SwiftUI <i>intentionally</i> abstracts all of these things away from you so you don&#x27;t have to deal with it, or even know about it. You&#x27;re used to programming on the web? Well, the web has no standard. And anyways, people looking for magic defaults is how &quot;8&quot; and &quot;44&quot; show up seemingly randomly in iOS apps. Let the system decide for you…if you <i>really</i> need to override it, you can add your own explicit value.
评论 #22414473 未加载
评论 #22416123 未加载
013aabout 5 years ago
There are definitely a few things it does that are weird, and this article hits on a few of them.<p>But, I&#x27;ll call special attention to the whole &quot;adaptive padding thing&quot; as a broader signal for something I feel that SwiftUI does very, very right and more UI frameworks need to start thinking about. Its made to design iOS apps. iOS has a design language that (1) is relatively consistent, (2) changes over time, and (3) changes across the many devices Apple supports.<p>In this era of many, many devices (don&#x27;t just think Apple here; think any device; think VR, think TVs, etc), we need to operate at a higher layer of abstraction than most UI systems operate at. Most websites are pure garbage on 35&quot; ultra-ultra-wide displays. It took a decade before mobile web entered the &quot;generally useable&quot; state it is in today. Ever tried to enable an ultra-high DPI setting on, say, Ubuntu or Windows?<p>All of these are getting better, but they only get better because the underlying technologies are becoming more generic.<p>&gt; If I forgot to put padding between HStack elements, well, shame on me, there should be no padding! All mistakes are mine.<p>Wrong, and Apple has the data to prove it. When you release a poorly designed app to the App Store, the mistake is your&#x27;s; your customers suffer because of it; and, it hurts the optics of application quality on the App Store, and thus Apple suffers.<p>This is relatively easy to draw a comparison to, say, manual memory management in C++. &quot;If I forgot to properly bounds check a memory segment, well, shame on me, there should be no bounds checking! All mistakes are mine&quot;. Sure; and now your customers are vulnerable to hackers. You have a higher responsibility than just your Pride.<p>The defaults should be Safe. There should be options to override them when I feel confident I know what I&#x27;m doing (and SwiftUI has this, not the least of which being, you don&#x27;t have to use SwiftUI, you can mix-and-match SwiftUI components with traditional iOS components).
评论 #22413816 未加载
评论 #22413890 未加载
评论 #22415543 未加载
kenforthewinabout 5 years ago
This website&#x27;s background color is considered harmful, and &quot;considered harmful&quot; essays are considered harmful.
评论 #22413629 未加载
评论 #22526522 未加载
评论 #22414104 未加载
评论 #22415339 未加载
评论 #22415655 未加载
kenabout 5 years ago
Just when I thought Lisp had been ransacked for every last feature that language designers were willing to steal, the &quot;War on commas&quot; pops up -- complete with discussion of the essential differences might exist between &quot;(&quot; and &quot;{&quot;, the pain of needing another way to say &quot;if&quot;, and the lack of support for the rest of the language.<p>And the last couple interesting Swift libraries I saw had .gyb files and #if directives.<p>Apple tried <i>really hard</i> to make a fully static language that was visually elegant (and looked like C), but in the end we need so many dynamic features that what we ended up with seems much more complex than just having had well-designed macros from the start.<p>The cynic in me suspects that they know this would be worse, but has the advantage that only Apple can add such features, not the common programmer. OTOH, the cynic in me knows that staying far away from Lisp is par for the course, and every new language needs their code to look like an example from the designer&#x27;s tattered copy of &quot;K&amp;R&quot;.<p>At least they managed to drop C-style for-loops in a mainstream language, so next time a company decides to design a new language, we can hopefully start there. I predict we&#x27;re about 3 generations of corporate languages away from seeing macros get &quot;invented&quot; again.
评论 #22415088 未加载
评论 #22415518 未加载
jmullabout 5 years ago
I really don&#x27;t think this is a good criticism of SwiftUI.<p>The author is confused about the whole approach to SwiftUI and spends a lot of words complaining about issues arising out of that confusion. E.g., SwiftUI doesn&#x27;t use Swift&#x27;s DSL capability to avoid commas. It uses it to have a declarative DSL integrated into Swift programs. You could argue about the entire approach, but instead the author complains about commas and ifs. The things about the ifs and other potential control flow constructs makes it clear that the author doesn&#x27;t get what a declarative approach really means.<p>Likewise, the section about &quot;wraps&quot; where the author feels the SwiftUI API is inconsistent because stacks &quot;wrap&quot; and padding &quot;wraps&quot; so they should use the same syntax. But I think that&#x27;s just a misunderstanding of the author&#x27;s. &quot;wrapping&quot; isn&#x27;t a fundamental concept of SwiftUI so there&#x27;s no particular reason for the API to be organized around it. (I would say stacks arrange a list of views and padding pads a view. Also, bold() operates on Text and returns Text because Text holds styled text and bolded Text is still Text. padding operates on any View and returns some View. The types flow naturally from the problem domain, so that&#x27;s good.)<p>Also the conclusion: &quot;The solution…is to be dumb and explicit!&quot; That&#x27;s really asking for a completely different framework. It seems a little off to criticism SwiftUI for not being what it was never intended to be.<p>I think there is good criticism in here, though. SwiftUI introduces a LOT of patterns, concepts, many of which will not already be familiar to many developers. It&#x27;s concise and reads pretty intuitively, but many developers won&#x27;t get far before hitting a lot of new stuff... that means steep learning curves and confusion, like the author. I don&#x27;t know if that makes it bad, but it&#x27;s certainly anyone consider using it needs to be aware of.
评论 #22416925 未加载
sleepinseattleabout 5 years ago
I’m guessing the magic layout things are intentional because SwiftUI is designed to make it easy to have one app feel native on iPhone, iPad, Apple Watch, Mac and AppleTV, which all have their own design language and ergonomics.<p>As long as it provides ways to override defaults and take full control where needed, this seems reasonable. I’m guessing you can drop a Metal view into your SwiftUI app, so quit complaining :)
评论 #22413576 未加载
erikrothoffabout 5 years ago
I was really excited about SwiftUI after having done a lot of React development recently. The declarative style is so much nicer than the MVC&#x2F;procedural code that regular UIKit code becomes.<p>After 30 minutes of updating Xcode I was able to start writing SwiftUI code. Then another 2 hours upgrading our Swift project to the latest version of Swift. I googled for documentation, found barely anything and the code samples on developer.apple.com were already outdated. I finally got something up and running, but _nothing_ in the semantics worked as expected. Getting a button to render nicely was insanely complex (which view should have the background color? font? trying different things didn&#x27;t work at all). Oh and recompiling took a minimum of 30 seconds each time.<p>All this prompted me to finally give react-native a try. In under 15 minutes I had a working example. After making a change I saved the file, alt-tabbed to the simulator and the changes were already applied. And it works instantly on Android, I can try it out on my phone using expo without any hassle.<p>I just can&#x27;t comprehend if and how Apple will catch up with that experience?
评论 #22415350 未加载
评论 #22415174 未加载
评论 #22414864 未加载
评论 #22415540 未加载
评论 #22417222 未加载
rkapsoroabout 5 years ago
While I don&#x27;t yet have great confidence in my mental model of SwiftUI&#x27;s type system, my understanding is that the DSL builders exist for more reasons than just to eliminate commas.<p>The new Function Builder and `some` return type features of Swift allow you to return a complex, nested type describing not just that it&#x27;s a View but rather also all the structure within.<p>This means that at <i>runtime</i> you can have a (non-erased!) type describing the contents of your `var body` like this:<p><pre><code> Button&lt;Group&lt;_ConditionalContent&lt;Text, Image&gt;&gt;&gt; </code></pre> This particular body displays a Text if a state boolean is true, and an image if not.<p>Super interesting - and having types this descriptive can potentially enable a host of interesting features and optimizations. For example, across evaluations of your UI SwiftUI needs to track the identity (and location in your view graph) of all of your views, in order to keep their state, bindings, and other stuff that needs to remain consistent across time. The position if your view in a type hierarchy is the default way SwiftUI does so. This starts to blur the line between code and the type system, which I think the Functional Programming folks have been exploring for years.<p>One more nitpick: the use of View Modifiers on children noticed by parents (like the `navigationBarTitle` example he uses (as discussed in the &quot;Child Privacy Invasion&quot; section), is to allow for children to specify metadata that the parent might care about. This is actually pretty nice, because then you can nicely compose together the various parts of your app without having to spread around the configuration for your tab bars and such (which is a bit messy in UIKit).<p>edit: formatting. I always trip up with it on HN due to the lack of a Preview feature.
评论 #22414573 未加载
boukeabout 5 years ago
Observing the launch of SwiftUI as a bystander (I&#x27;m not developing for any Apple platform), it seems that people are very excited about SwiftUI, but also get burned by it a lot. This is mostly Apple&#x27;s fault by releasing this framework without any caveats. But the framework is more like an early alpha, not to be used in production. Tooling is not yet ready, documentation is not yet ready, heck even Swift –the language and compiler– aren&#x27;t even ready yet. Apple should&#x27;ve put a big warning notice on any SwiftUI related website to set the correct expectations.
评论 #22413828 未加载
评论 #22413984 未加载
评论 #22414431 未加载
评论 #22415991 未加载
0xferruccioabout 5 years ago
I tried out SwiftUI a couple of months ago and it was buggy even when sticking to the standard Navigation components..<p>But now it seems like a lot of problems have been solved<p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;59279176&#x2F;navigationlink-works-only-for-once" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;59279176&#x2F;navigationlink-...</a>
评论 #22413694 未加载
评论 #22414284 未加载
评论 #22413596 未加载
GeneralTspoonabout 5 years ago
&gt; Some things are probably just plain mistakes (very funny though). E.g. NavigationView takes its properties not from its constructor or via modifiers, but instead from the properties of its first child. WHY?<p>This sounds like it&#x27;s just inheriting the default iOS behaviour? The current ViewController sets the Navigation View&#x27;s properties.<p>I haven&#x27;t touched SwiftUI yet, but I&#x27;d imagine the existing NavigationController setup still exists within the SwiftUI framework, and this weirdness exists due to legacy reasons essentially.
madeofpalkabout 5 years ago
Reading through the syntax changes they made makes me really appreciate the decisions made around JSX and how that works, syntactically. It&#x27;s <i>just javascript</i>, and none of these weird constructs need to be added into some DSL to support it. There&#x27;s the reasonably minimal html-ish syntax, then &quot;escape to javascript&quot; to add in conditionals or loops or whatever.<p>I wonder if there was (or still is) a way to do something similar for Swift and SwiftUI.
评论 #22414412 未加载
评论 #22414116 未加载
评论 #22414392 未加载
jiofihabout 5 years ago
This is a very myopic view of UI development. The library is built to give you sane, well designed defaults that is lay together well when following the HIG, and which you should rarely attempt to override. Case in point, HStack, whose sole purpose is to <i>stack items horizontally with proper spacing</i> and is quite hard to achieve correctly on the web. If you want “dumb and explicit” why use a UI kit?
TuringTestabout 5 years ago
<i>&gt; When fixing broken layout, it is always easier to add stuff that you forgot than removing stuff that your framework did for you and you that can’t see.</i><p>This paragraph reminded me of this old Jargon File koan:<p>---<p>In the days when Sussman was a novice, Minsky once came to him as he sat hacking at the PDP-6.<p><i>“What are you doing?”</i>, asked Minsky.<p><i>“I am training a randomly wired neural net to play Tic-Tac-Toe”</i> Sussman replied.<p><i>“Why is the net wired randomly?”</i>, asked Minsky.<p><i>“I do not want it to have any preconceptions of how to play”</i>, Sussman said.<p>Minsky then shut his eyes.<p><i>“Why do you close your eyes?”</i>, Sussman asked his teacher.<p><i>“So that the room will be empty.”</i><p>At that moment, Sussman was enlightened.
评论 #22415930 未加载
qubexabout 5 years ago
The “X considered harmful” trope for deliberately strident technical screeds that quibble about what are mainly niche concerns has tired me out.<p>“‘X considered harmful’ considered harmful”, I guess.
评论 #22414059 未加载
rubyn00bieabout 5 years ago
I think everything after the first gripe is a fair point, but I don&#x27;t get what the problem is with the first code chunks... it seems like the author is saying &quot;this is weird to me, and I don&#x27;t like that,&quot; but I have no idea how else it&#x27;d work properly.<p>For example, explicitly defining out function arguments is exactly what Scala does for function, check out this bad mofo, defining a function with 22 arguments (that I fell in love with years ago): <a href="https:&#x2F;&#x2F;www.scala-lang.org&#x2F;api&#x2F;current&#x2F;scala&#x2F;Function22.html" rel="nofollow">https:&#x2F;&#x2F;www.scala-lang.org&#x2F;api&#x2F;current&#x2F;scala&#x2F;Function22.html</a><p>Why&#x27;d I fall in love with it? Because it makes sense, that&#x27;s how it should work right? I don&#x27;t see how defining out 10 items explicitly; so, that each can have it&#x27;s own type, is a problem. It&#x27;s pretty damn nice they did it out to ten arguments, if you want more, extend the object... &#x2F;shrug<p>If you used a list instead, e.g. `T[A]` you&#x27;d take a hot-fat erasure-shit on the type information of each item in the list (or at least you would last time I used Swift extensively)... this way you keep that type information around.<p>P.S. To be honest, explicitly defining things out like that can give you HUGE wins in languages like Swift because you give the compiler so much more information. It just doesn&#x27;t usually pop onto most folks radar because it looks obtuse... with most modern code editors though it&#x27;s easy enough to write in a couple minutes, if there&#x27;s no macro system.
K0nservabout 5 years ago
I really don&#x27;t agree with most of this post but I do find the function builder[0] feature that was introduced to support SwiftUI fairly overkill. I don&#x27;t fully understand why they didn&#x27;t use a lighter abstraction based on arrays and normal return values like React does instead of adding all this &quot;magic&quot;, maybe there&#x27;s a really good reason that I am not seeing.<p>All the other &quot;magic&quot; and defaults are excellent ideas because they encode Apple&#x27;s Human Interface Guidelines[1] straight into the framework which helps you create apps for the different Apple platforms that will fit in and feel at home. With a web or React Native&#x2F;Flutter&#x2F;Cordova&#x2F;... context this might seem jarring but the whole point is that an app it shouldn&#x27;t be a completely free form canvas and the frameworks should make it easy to build a best in class HIG compliant Apple app.<p>0: <a href="https:&#x2F;&#x2F;www.swiftbysundell.com&#x2F;articles&#x2F;the-swift-51-features-that-power-swiftuis-api&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.swiftbysundell.com&#x2F;articles&#x2F;the-swift-51-feature...</a><p>1: <a href="https:&#x2F;&#x2F;developer.apple.com&#x2F;design&#x2F;human-interface-guidelines&#x2F;" rel="nofollow">https:&#x2F;&#x2F;developer.apple.com&#x2F;design&#x2F;human-interface-guideline...</a>
ogre_codesabout 5 years ago
&gt; I mean, the only difference before the two is whether you have to put a comma between elements or not.<p>Personally, I vastly prefer minimizing punctuation in code. Most punctuation like this only lends itself to creating places where additional errors and bugs can creep in. Listing elements on separate lines gives you the separation needed so it&#x27;s clear it&#x27;s a list of things, the comma at the end of the line is needless.<p>&gt; All because someone in charge of API design was afraid of lists and had more power than someone in charge of Swift language.<p>I suspect the people in charge of designing the Swift language have a similar aversion to needless punctuation in the language. They made a point of calling out the lack of a semi-colon in the original Swift launch announcement. This isn&#x27;t some API designer pushing the issue, it&#x27;s part of the philosophy of the language.
mattsteabout 5 years ago
I recently started a SwiftUI project and this is a very accurate take based on my experience.<p>How I spend my time in SwiftUI: 1. Wondering how I can get some conditional logic. Coming from React, this is a major issue that needs to be addressed. 2. Figuring out where and what the error actually is. 3. Restarting Xcode for my view to magically start working again.<p>The two-way data binding is a blessing and a curse. I like React&#x27;s method of passing callback functions to keep the state logic separated. This comes up with me when working with sheets.<p>I see the potential. I like having an all-in-one experience that IDE + framework integration provides but the poor defaults and breaking bugs really make me question the execution.
ChrisMarshallNYabout 5 years ago
I&#x27;m in no hurry to start using SwiftUI. I&#x27;m giving it another year.<p>That doesn&#x27;t mean that I don&#x27;t like it. I think it&#x27;s a great idea. It&#x27;s just having a hard time clambering out of the bassinet.
评论 #22414256 未加载
hombre_fatalabout 5 years ago
&gt; I hope you have a wide enough monitor to read this. Not only does this seem ad-hoc and unpretty, but it also doesn’t allow you to put more than 10 elements in a container! All because someone in charge of API design was afraid of lists and had more power than someone in charge of Swift language.<p>This explanation doesn&#x27;t sound right to me coming from other statically typed languages where you&#x27;re stuck either using codegen or arity hardcoding. Currying also gives a nice solution where your DSL simply builds up a function A-&gt;B-&gt;C-&gt;...<p>Hard-coding each level of arity is done so that your generics keep upstream type knowledge.<p>Here&#x27;s an example in Elm, though it has to keep renaming the function due to lack of arity overloading.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;elm&#x2F;json&#x2F;blob&#x2F;063aaf05e0dc5a642bacbdaae59c33dcfd116898&#x2F;src&#x2F;Json&#x2F;Decode.elm#L420-L450" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;elm&#x2F;json&#x2F;blob&#x2F;063aaf05e0dc5a642bacbdaae59...</a><p>You can&#x27;t use variadic functions because then you function doesn&#x27;t even know how many arguments it has at compile time much less any specifics about them.<p>That Swift doesn&#x27;t require commas between closure returns seems like a different point.<p>You&#x27;d still need this solution even if you were just spitting out generic TupleView&lt;(View, View)&gt;, TupleView&lt;(View, View, View)&gt;, etc.<p>Unless, of course, you don&#x27;t actually care about type info the same way you don&#x27;t care how many arguments are passed to a variadic max(1, 4, 66) function.<p>Another comment says there will be a variadic viewbuilder function once Swift gets variadic generic functions which will still throw away type info, so this seems to be the case. Still, I don&#x27;t see the relationship with commas.
ericlewisabout 5 years ago
Just to point something out: his first complaint is over needing to have up to 10 generics, this will be going away with variadic ViewBuilders that should be coming soon, towards swift 6 perhaps. And will also remove the limit for number of children. This is mostly a limitation in swift generics, not swiftui it self
layoutIfNeededabout 5 years ago
Honestly, just use UIKit. SwiftUI is a bolt-on framework on top of UIKit to provide a React-like experience for web devs so they won’t rage too much when Apple inevitably bans ReactNative and Flutter.
评论 #22415725 未加载
tayistayabout 5 years ago
Would it be possible to wrap SwiftUI such that we don&#x27;t have to use that DSL, don&#x27;t have that 10 child limit (how many hours wasted figuring that one out?), and don&#x27;t have bizarre non-localized error messages?<p>My guess is the answer is no because SwiftUI builds view hierarchies using parameterized types, so you&#x27;re always stuck with `some View` and the associated complexity.
barbecue_sauceabout 5 years ago
Is SwiftUI only compatible with the newest iOS versions? Will using SwiftUI mean only people running iOS 13+ can use it?
评论 #22413767 未加载
kdotabout 5 years ago
Interesting how most of his points are exactly how Flutter is implemented. For example the trailing commas, and no assumed defaults.
RedCometabout 5 years ago
Things with &quot;considered harmful&quot; in the title should be considered harmful.
评论 #22419224 未加载
halayliabout 5 years ago
I am not sure it&#x27;s a good idea to take advice from someone that picks such a background color
Iwork4Googleabout 5 years ago
To the person who wrote this, I would suggest SwiftUI is not for you. Please just go back to writing Objective-C or learn some good ol&#x27; UIKit. You can then have full control over whatever color you want your buttons to show up as, and where to have them show up, and with whatever padding you like, and as a bonus you can use commas and all kinds of great additional syntax all over the place. (I think what SwiftUI is for, has largely flown over your head).swiftly