I’m trying to use the SwiftUI Button
initializer with init(_:image:action:)
and it is resulting with a compiler error as follows. Anyone ran into this problem?
Error I’m getting is Cannot convert value of type 'ProjectName.ImageResource' to expected argument type 'DeveloperToolsSupport.ImageResource'
Steps to reproduce.
- Create new Xcode project (I’m using Xcode 15.3)
- Set minimum iOS version 16.0
- Add any image to asset catalogue with name say
image1
- Add the following code to body of the content view
var body: some View {
if #available(iOS 17.0, *) {
Button("title", image: .image1, action: {}) // compiler error
} else {
Button("title", systemImage: "globe", action: {})
}
}
It seems to be some Xcode 15.3 bug with some steps to reproduce on a new project easily.
For the original problem however, I’ve managed to get around the compiler error by using the ImageResource's
full version of the initializer such as this.
Button("title", image: .init(name: "image1", bundle: .main), action: {})
This is strange because I can use generated asset’s name directly everywhere else such as
Image(.image1)
Hope the upcoming 15.4 version of Xcode fixes this.
3