As was discussed in the previous thread, you seem to be conflating the Swift language with platform libraries such as SwiftUI.
You asked why we cannot present an alert simply by writing:
alert("hello")
But we can - check it out:
func alert(_ message: String) {
// ??? - show an alert
}
alert("hello")
The Swift language absolutely allows you to define a function which takes a string parameter and does something with it. The language and syntax is not the issue. It's up to the system's UI libraries, which are responsible for presenting alerts, to decide whether that is an appropriate interface for their task.
The language has some features which help those libraries expose simpler interfaces - such as result builders - and I think it is quite successful at that.
I can show you a concrete example. I'm developing a (non-UI) library to process URLs, and there are a lot of complex edge-cases, so I wanted to create a live viewer where I could type in a string and have it processed both by my library and by the standard's reference implementation (written in Javascript), and for the app to highlight if there are any differences.
This is just a sort of interactive playground app - literally I am the only person who ever used it, while developing some other software. It's not a production app, and I don't really want to spend too much time on it. Anyway, here's one of the components - a URLForm
:
// URLValues and URLModelProperty defined elsewhere
struct AnnotatedURLValues {
var values: URLValues? = nil
var flaggedKeys: [URLModelProperty] = []
}
struct URLForm: View {
let label: String
@Binding var values: AnnotatedURLValues
var body: some View {
GroupBox(label: Text(label)) {
ScrollView(.horizontal) {
VStack(alignment: .leading) {
ForEach(URLModelProperty.allCases, id: \.self) { property in
row(
name: property.name,
value: values.values.map { $0[property] ?? "(nil)" } ?? "(URL is nil)"
)
.foregroundColor(values.flaggedKeys.contains(property) ? .red : nil)
}
}
}
}
}
private func row(name: String, value: String) -> some View {
HStack(spacing: 10) {
Text(name)
.bold()
.frame(minWidth: 100, maxWidth: 100, alignment: .trailing)
Text(value)
.frame(alignment: .leading)
Spacer()
}
}
}
That is super simple, right? A box, containing a scrollview (to handle overflow, so the text doesn't wrap), with some rows in it. The rows have a name and value, and some rows can be flagged as "bad". It's just a simple little component, and it only needs a very small amount of code.
Doing this with UIKit (the previous UI framework) would be much more complex - I'd either have to get involved with AutoLayout, which can be tricky and verbose, or write my own, imperative layout code, calculating and setting frames and such. By contrast, this is all declarative. I wrote it in a couple of minutes, and even though I haven't touched it in almost a year, I can read and understand it in seconds.
Integrating this component in another view is super easy, too:
var body: some View {
// ...
URLForm(label: "WebURL", values: $modelData.weburlResult)
URLForm(label: "Reference result", values: $modelData.referenceResult)
// ...
}
And it just works. Of course, the library doesn't exhibit any incorrect values in practice
, but if I modify the library to introduce a bug, we can test it:

It also works on iOS, with no changes. It supports light/dark mode, automatically, and I think it also supports dynamic text sizes.
So yeah - some things (like the flags which determine whether an alert is visible or not, and state management in general) are quite different, and might take some getting used to. But overall I'm relatively happy with SwiftUI, and the Swift language features which enable it - especially when it comes to keeping simple things simple.
You can't just say that anything that isn't a single function call isn't simple. Here, I composed a bunch of system views in to a custom view, then composed a couple of those views in to yet another view. It all works in a very straightforward way, in a very small amount of code. Productivity was high, maintainability is high. I think it's simple.