Markdown is a way to construct AttributedString as a convenience. It is not the internal format of AttributedString. Most things that an AttributedString can represent cannot even be represented in Markdown. In the most basic example, AttributedString can define the exact font for a region. There is no way to express that in Markdown. At the extreme, AttributedString can represent any attributes, including custom ones defined in your code, not just ones defined by Foundation.
But in your case, I believe the confusion is more basic. The "Markdown" in question is just the string. If you want that, then that's fetched with:
String(attString.characters)
(Somewhat surprisingly there is no .string
property. This is because it's more expensive to construct than it looks.)
What you've built here isn't an AttributedString defined by Markdown. That would require calling try AttributedString(markdown: text)
. It's just a string that happens to have asterisks in it. If you really did create a formatted AttributedString with Markdown, and needed to get the Markdown back, you would need to track that yourself in a wrapper type that contained an AttributedString and the Markdown String.