Unity by Elly Bangs 📚

As I have noted several times that current circumstances make it difficult for me to sit down and read a novel, and I was only able to make my way through this one due to our recent travel days, the first in a very long time. This was a good book for the trip, in that it is well written, interleaving the stories of several engaging characters facing personal and global crises; but at the same time it was challenging to follow the conceptual threads, which pass through increasingly abstract layers of identity to ask fundamental questions about what it means to be human.

The story begins in a future world in convulsion due to catastrophic climate change, with nuclear, biological, and nanotech warfare threatening the end of humanity. The cast of characters seem to be typical post-apocolyptic tropes, but it is soon revealed that some of them are post-human gestalts created using the titular Unity technology. Amid the background of escape and conflict the natures of these post-humans are gradually revealed; it’s not a single facet, but several different paths rooted in earlier decisions (shown in flashbacks) and reflecting the various aspirations and morality of the characters in question.

With such transformative technology followed to its logical conclusion it would be hard to escape a deus ex machina ending, and Unity does not escape this fate; but the characters themselves reach satisfying resolutions of their own, despite disasters both global and personal.

Recommended.

<img src=“https://m.media-amazon.com/images/I/51ZqDw68oDL.jpg" alt=“Book cover for Unity by Elly Bangs, a small yellow stick figure in the center, surrounded by linked stick figures in circles.,” title=“Book cover” class=“cover”/>

SwiftUI attributed strings

I’ve been working on a pure SwiftUI feed reader which uses Text elements for most of the content, but using the iOS 14 SDK I had not yet implemented embedded links, as the Text element did not support attributed strings. This has changed with the new iOS 15 SDK, which adds an AttributedString struct which can be passed to Text elements in place of a String.

In addition it is now possible to pass String values with a subset of Markdown formatting to Text elements directly, to create an AttributedString, and to create the Foundation basis NSAttributedString. But this does not always work as I expected.

When I pass a String literal with Markdown I get the expected behavior; thus

Text("**This** is a _test_ with a [link](https://elegantnewt.blog).")

yields the expected result

This is a test with a link.

However if I pass a reference to the same String to the Text element then the Markdown is not parsed:

let str = "**This** is a _test_ with a [link](https://elegantnewt.blog)."
Text(str)

results in

**This** is a _test_ with a [link](https://elegantnewt.blog).

If I use a string literal but try to inject the URL via interpolation I find that the link text is correct but is not marked as a link:

let url = "https://elegantnewt.blog"
Text("**This** is a _test_ with a [link](\(url)).")

produces

This is a test with a link.

However, if I create an AttributedString instance using the same input, and then pass that to the Text element, I do get the correct result, although I need to verify that the construction succeeds:

let markdown = "**This** is a _test_ with a [link](\(url))."
if let att = try? AttributedString(markdown: markdown) {
    Text(att)
}

works as expected:

This is a test with a link.

As an alternative, if I construct an NSAttributedString from the same Markdown string reference, I again get the correct rendering:

if let nsAtt = try? NSAttributedString(markdown: markdown),
   let att = try? AttributedString(nsAtt, including: AttributeScopes.SwiftUIAttributes.self) {
    Text(att)
}

also yields

This is a test with a link.

But I need to be careful about how I construct that NSAttributedString. For example, if I build it from the corresponding HTML markup then the bold and italic modifiers aren’t rendered.

let html = "<b>This</b> is a <i>test</i> with a <a href=\"https://elegantnewt.blog\">link</a>."
let data = Data(html.utf8)
if let htmlNSAtt = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil),
    let htmlAtt = try? AttributedString(htmlNSAtt, including: AttributeScopes.SwiftUIAttributes.self) {

    Text(htmlAtt)
}

results in

This is a test with a link.

Obviously I need to compare the NSAttributedString instances to determine the exact attributes being used in these different cases.

Trying to get caught up, despite the busy schedule we did manage to fit in a swim in a pond yesterday.