AttributedSubstring
A portion of an attributed string.
Declaration
@dynamicMemberLookup struct AttributedSubstringOverview
AttributedSubstring provides no-copy access to the contents of the string within the relevant range. The distinction between an AttributedString and an AttributedSubstring lets you distinguish between whether you’re in possession of an entire string or just a slice of it.
Because AttributedSubstring and AttributedString both conform to AttributedStringProtocol, working with ranges of AttributedString is natural. Modifying attributes by range works the same as it does on the base string.
If you use an AttributedSubstring to mutate its base AttributedString, you must perform your mutation inline, as the following example shows:
// Correct use of copying.
attrStr[range].link = url
// Incorrect use of AttributedString copy. Copies the referenced range of the base
// AttributedString and mutates that.
var substr = attrStr[range]
substr.link = url