Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

AppIntents issue when importing UIKit and using an Image for caseDisplayRepresentations of AppEnum

I’m creating a simple AppIntents Shortcut that requires UIKit to open a URL in my app. Though, once I add the import UIKit to my file, Xcode shows me the following warning at the caseDisplayRepresentations, because I use an image for the display repreentations. Without importing UIKit (or without the image for the DisplayRepresentations) everything works fine! Any ideas how to fix that?

Expected a direct call to the Image initializer, got Swift.Optional<AppIntents.DisplayRepresentation.Image> instead

Xcode Warning Screenshot

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The Code

import AppIntents
import UIKit

enum AppView: Int, AppEnum {
    case tab1
    case tab2
    
    static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "View")
    
    static var caseDisplayRepresentations: [AppView : DisplayRepresentation] = [
        .tab1: DisplayRepresentation(title: "Tab 1",
                                     image: .init(systemName: "1.circle")),
        .tab2: DisplayRepresentation(title: "Tab 2",
                                     image: .init(systemName: "2.circle"))
    ]
}

// MARK: - Intent
struct OpenAppIntent: AppIntent {
    static let title: LocalizedStringResource = "Open App"
    static let description: IntentDescription = "Opens the app in the given view."
    
    // Launches app when action is triggered
    static let openAppWhenRun: Bool = true
    
    // App View Parameter
    @Parameter(title: "View",
               description: "The view inside the app.",
               default: .tab1,
               requestValueDialog: "Where would you like to navigate to?")
    var view: AppView
    
    // Shortcuts Action Text
    static var parameterSummary: some ParameterSummary {
        Summary("Open \(\.$view)")
    }
    
    
    @MainActor
    func perform() async throws -> some IntentResult {
        if let url = URL(string: "myapp://\(view)") {
            await UIApplication.shared.open(url)
        }
        
        return .result()
    }
}

>Solution :

As I can see, when UIKit was imported, it changed the Image default initializer from returning Image to Optional<Image>.

Please focus on the Quick Help.
Before
After

You can solve this issue by force unwrapping the image (not recommended)

.init(systemName: "1.circle")!

or by helping Xcode specify your selected initializer.

.init(systemName: "1.circle", isTemplate: false)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading