Fehlendes SF Symbol
Image(systemName:) kann nicht fehlschlagen. Löst sich ein Name nicht auf, zeichnet SwiftUI nichts, und du lieferst eine leere Lücke aus. Das passiert bei einem Tippfehler, bei einem Namen, den es nie gab, und wenn ein Symbol neuer ist als dein Deployment Target. In der Anleitung einer App ist mir genau das passiert.
Beispielzeile
- Aufruf
Image(systemName: "heart.fil")- Auflösung
UIImage(systemName:) → nil
heart.fil löst sich nicht auf, der Fallback ist aus. SwiftUI zeichnet nichts.
Die Zeichen sind eigene Zeichnungen, keine Apple-Symbole. heart.fill und heart sind nur die Namen.
Dieselbe Zeile in Swift
UIImage.symbol(_: ruft UIImage( auf. Kommt nil zurück, nimmt der Helfer heart. Image( prüft UIImage(systemName:) != nil und fällt sonst auf den Fallback. Image(systemName:) selbst ist nicht optional. Nil gibt es bei einem Tippfehler, bei einem Namen, den es nie gab, und wenn ein Symbol neuer ist als dein Deployment Target. #available hilft nur, wenn du so eine neuere Version schon kennst. Einen Namen, den kein System führt, fängt das nicht ab.
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)
}
}
}
}