No such module 'UIKit' in swift Macro

Hi All,

I am getting No such module 'UIKit' in swift Macro when I import UIKit in it.

Macro package has below platforms
platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)],

Thanks in advance.

Your macro runs on the computer you code on (Mac/Windows/Linux, probably), during the build process. It doesn't run on iOS, so it doesn't have access to UIKit**.

Why did you need UIKit within your Macro (as opposed to within the package that declares the macro)?

** I guess there's a small question as to whether you could create a "Mac Catalyst" macro that could import UIKit, but it's probably not useful even if it's possible

2 Likes

Hi Keith, Thanks for your reply. I need to create Image instance in my Macro and validate wether image exists or not. To access Image elements I need to import UIKit in my Macro.

Since your macro is running on the build machine, you don't have access to the same set of compiled & copied resources that your final app will draw from when it constructs an image, so you can't "just create an image" to find out if it will eventually exist at runtime.

The best you could do would be to parse the Package.swift files and Xcode projects and .xcassets bundles involved in the build, to determine what images will exist at runtime, I think. And even then, there'll be cases where you're wrong (eg. an image is created by a "run script" build phase).

Xcode 15's new UIImage.init(resource:) accessor (and friends on other types) probably already does what you're trying to do?

1 Like

Hi Keith, Thanks for your reply. In my macro, I need to throw an error if user gives the wrong systemName when create an image by using method UIImage(systemName:).
UIKit returns nil for UIImage(systemName:) if systemName is not correct. SwiftUI returns image even though systemName is not correct.

Your iOS project will likely only ever build on macOS, so your macro could import AppKit and ask the same question of NSImage.