UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 2: UIColor, UIFont are not resolved in playground

Forums > 100 Days of Swift


In Day 2 optional reading related to power of String interpolation. I had issues running the last code related to ColoredString because UIColor and UIFont are resolved in playground. How can I resolved that?

struct ColoredString: ExpressibleByStringInterpolation {
    // this nested struct is our scratch pad that assembles an attributed string from various interpolations
    struct StringInterpolation: StringInterpolationProtocol {
        // this is where we store the attributed string as we're building it
        var output = NSMutableAttributedString()

        // some default attribute to use for text
        var baseAttributes: [NSAttributedString.Key: Any] = [.font: UIFont(name: "Georgia-Italic", size: 64) ?? .systemFont(ofSize: 64), .foregroundColor: UIColor.black]

        // this initializer is required, and can be used as a performance optimization
        init(literalCapacity: Int, interpolationCount: Int) { }

        // called when we need to append some raw text
        mutating func appendLiteral(_ literal: String) {
            // print it out so you can see how it's called at runtime
            print("Appending \(literal)")

            // give it our base styling
            let attributedString = NSAttributedString(string: literal, attributes: baseAttributes)

            // add it to our scratchpad string

        // called when we need to append a colored message to our string
        mutating func appendInterpolation(message: String, color: UIColor) {
            // print it out again
            print("Appending \(message)")

            // take a copy of our base attributes and apply the color
            var coloredAttributes = baseAttributes
            coloredAttributes[.foregroundColor] = color

            // wrap it in a new attributed string and add it to our scratchpad
            let attributedString = NSAttributedString(string: message, attributes: coloredAttributes)

    // the final attributed string, once all interpolations have finished
    let value: NSAttributedString

    // create an instance from a literal string
    init(stringLiteral value: String) {
        self.value = NSAttributedString(string: value)

    // create an instance from an interpolated string
    init(stringInterpolation: StringInterpolation) {
        self.value = stringInterpolation.output

// now try it out!
let str: ColoredString = "\(message: "Red", color: .red), \(message: "White", color: .white), \(message: "Blue", color: .blue)"


Did you import UIKit?



@rooterboy Yes, I did.


If you look at the section on the right side of the playground, and hover your mouse over the line where it is showing your strings, a little eye icon should appear next to it. If you tap on the eye, it should show you what it will actually look like with the color.


BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.