Skip to content

In development. This website is being rebuilt. Content, screenshots, and links may still change.

Martin SchulzeiOS Developer

Demos

Missing SF Symbol

Image(systemName:) can't fail. When a name doesn't resolve, SwiftUI draws nothing, and you ship an empty gap. That happens with a typo, with a name that never existed, and when a symbol is newer than your deployment target. I hit exactly that in an app guide.

Symbol name
Fallback

Sample row

nothing renderedNothing renderedFavorites
Call
Image(systemName: "heart.fil")
Resolved
UIImage(systemName:) → nil

heart.fil does not resolve, and the fallback is off. SwiftUI draws nothing.

The drawings are original, not Apple symbols. heart.fill and heart are only the names.

The same row in Swift

UIImage.symbol(_:configuration:) calls UIImage(systemName:withConfiguration:). Nil falls through to heart. Image(systemName:fallback:) checks that UIImage(systemName:) != nil and otherwise uses the fallback. Image(systemName:) itself is not optional. You get nil for a typo, for a name that never existed, and when a symbol is newer than your deployment target. #available only helps when you already know that newer version. It will not catch a name no system includes.

Swift
import UIKit
import SwiftUI

/// `heart` is a real symbol. The guide uses it when a name does not resolve.
private let fallbackSymbolName = "heart"

extension UIImage {
    /// The image for `name`, or `heart` when the name does not resolve.
    ///
    /// `UIImage(systemName:withConfiguration:)` returns nil for a typo, for a
    /// name that never shipped, and when a symbol is newer than the system
    /// you are running. The fallback keeps the same configuration.
    /// `UIImage()` is only reached when the fallback name fails too.
    static func symbol(
        _ name: String,
        configuration: UIImage.Configuration? = nil
    ) -> UIImage {
        UIImage(systemName: name, withConfiguration: configuration)
            ?? UIImage(systemName: fallbackSymbolName, withConfiguration: configuration)
            ?? UIImage()
    }
}

/// `heart.fil` is a typo of `heart.fill`. It is not an SF Symbol name,
/// so the helper resolves the image to `heart`.
func guideImage() -> UIImage {
    let configuration = UIImage.SymbolConfiguration(pointSize: 17, weight: .semibold)
    return UIImage.symbol("heart.fil", configuration: configuration)
}

extension Image {
    /// `Image(systemName:)` is not failable. A missing name still builds an
    /// image, and SwiftUI draws nothing, which leaves an empty gap.
    ///
    /// Resolve the name with UIKit first. `fallback` has no default, so this
    /// initializer does not collide with `init(systemName:)`.
    init(systemName name: String, fallback: String) {
        let resolved = UIImage(systemName: name) != nil ? name : fallback
        self.init(systemName: resolved)
    }
}

/// `#available` only helps when you already know a real symbol is newer
/// than your deployment target. It cannot detect a name no system ships.

/// One guide row. `foregroundStyle` needs iOS 15.
@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
struct GuideRow: View {
    var title: String
    var detail: String

    var body: some View {
        HStack(spacing: 12) {
            Image(systemName: "heart.fil", fallback: "heart")
                .frame(width: 28, height: 28)
            VStack(alignment: .leading, spacing: 2) {
                Text(title)
                Text(detail)
                    .font(.subheadline)
                    .foregroundStyle(.secondary)
            }
        }
    }
}