From 5f3675c850031b6bd49805567109e8a168bf1848 Mon Sep 17 00:00:00 2001 From: Lukas Pistrol Date: Wed, 5 Jul 2023 16:11:42 +0200 Subject: [PATCH] add assertion for UIImage, simplified demo project Since Xcode Previews sometimes acts a little strange on iOS targets (cannot import NSImage) the plugin will now use UIImage instead in this specific case. --- Demo App/Demo App/ContentView.swift | 14 +++++++++++--- Sources/SFSymbolsMacroImpl/SFSymbolsMacro.swift | 8 +++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Demo App/Demo App/ContentView.swift b/Demo App/Demo App/ContentView.swift index d96dd6a..f01139e 100644 --- a/Demo App/Demo App/ContentView.swift +++ b/Demo App/Demo App/ContentView.swift @@ -27,16 +27,24 @@ struct ContentView: View { VStack { Label("Hello, World!", systemImage: Symbols.globe()) Spacer() - LazyVGrid(columns: .init(repeating: GridItem(.fixed(50)), count: 5), content: { - ForEach(Symbols.allCases, id: \.self.rawValue) { symbol in + LazyVGrid(columns: columns) { + ForEach(Symbols.allCases) { symbol in symbol.image .imageScale(.medium) .font(.largeTitle) } - }) + } } .padding() } + + private var columns: [GridItem] { + .init(repeating: GridItem(.fixed(50)), count: 5) + } +} + +extension Symbols: Identifiable { + var id: String { self.rawValue } } #Preview { diff --git a/Sources/SFSymbolsMacroImpl/SFSymbolsMacro.swift b/Sources/SFSymbolsMacroImpl/SFSymbolsMacro.swift index 0cb5e7b..e39a034 100644 --- a/Sources/SFSymbolsMacroImpl/SFSymbolsMacro.swift +++ b/Sources/SFSymbolsMacroImpl/SFSymbolsMacro.swift @@ -96,9 +96,11 @@ public struct SFSymbolMacro: MemberMacro { } private static func assertUINSImage(for id: (SymbolPair)) throws { - if let _ = NSImage(systemSymbolName: id.id, accessibilityDescription: nil) { - return - } + #if canImport(UIKit) + if let _ = UIImage(systemName: id.id) { return } + #else + if let _ = NSImage(systemSymbolName: id.id, accessibilityDescription: nil) { return } + #endif throw DiagnosticsError(diagnostics: [ .init(node: id.node, message: SFSymbolDiagnostic.noValidSFSymbol(symbol: id.0)) ])