fupio

Fupio es la forma más fácil de compartir en línea Más información »

Únete a Fupio
Hacking with Swift

Hacking with Swift

Swift's result builders are a powerful language feature that let us create domain-specific languages right inside our Swift code. With a little thinking, this means we can actually create whole websites in Swift, with our code automatically being converted to valid, accessible Swift, and we can even sprinkle in a little SwiftUI magic to complete the effect.
Let's get to it…

Starting from scratch

For over 30 years, HTML has been a great language for describing the structure of web pages.
For example, we can write HTML like this:
<h1>Wise words</h1>
<p>"If you don't take risks, you can't create a future" - <em>Monkey D. Luffy</em> in <a href="https://en-wp.org/wiki/One_Piece">One Piece</a></p>
That has a heading, a paragraph of text with some emphasis, and a link to another page. But, what happens if we forget the closing </em> tag? Without it, web browsers will assume everything that follows should also be emphasized.
That's not what I intended, but it's easy to do because HTML is just a bunch of text.
But even if you write perfect HTML, there are other, bigger problems:
1. How can you make sure your pages look the same on all browsers?
2. How can you make your page adapt to different screen sizes, such as iPhone and iPad?
3. How can you use more advanced UI elements such as dropdown menus, carousels, and accordions?
4. Most importantly, how can you be sure your site is accessible to everyone?
Ultimately, all these boil down to one huge problem: most people don't have enough time to become experts in Swift and also experts in HTML.
And so I want to suggest that the answer is to not use HTML, or at least not directly. Instead, I would you like to propose that we use Swift to build our websites.

Introducing result builders

Back in 2019 when Apple announced SwiftUI there the usual What's New in Swift presentation. During that talk they showed the following HTML:
<html>
    <head>
        <title>Jess...
https://www.hackingwithswift.com/articles/266/build-your-next-website-in-swift

Comments