Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

How to initialize OpenIntent parameter when returning OpensIntent in perform

I have an app that lets you create cars. I have a CarEntity, an OpenCarIntent, and a CreateCarIntent. I want to support the Open When Run option when creating a car. I understand to do this, you just update the return type of your perform function to include & OpensIntent, then change your return value to include opensIntent: OpenCarIntent(target: carEntity). When I do this, I get a compile-time error:

Cannot convert value of type 'CarEntity' to expected argument type 'IntentParameter<CarEntity>'

What am I doing wrong here?

struct CreateCarIntent: ForegroundContinuableIntent {
    static let title: LocalizedStringResource = "Create Car"
    
    @Parameter(title: "Name")
    var name: String
    
    @MainActor
    func perform() async throws -> some IntentResult & ReturnsValue<CarEntity> & OpensIntent {
        let managedObjectContext = PersistenceController.shared.container.viewContext
        
        let car = Car(context: managedObjectContext)
        car.name = name
        
        try await managedObjectContext.perform {
            try managedObjectContext.save()
        }
        
        let carEntity = CarEntity(car: car)
        return .result(
            value: carEntity,
            opensIntent: OpenCarIntent(target: carEntity) // FIXME: Won't compile
        )
    }
}

struct OpenCarIntent: OpenIntent {
    static let title: LocalizedStringResource = "Open Car"
    
    @Parameter(title: "Car")
    var target: CarEntity
    
    @MainActor
    func perform() async throws -> some IntentResult {
        await UIApplication.shared.open(URL(string: "carapp://cars/view?id=\(target.id)")!)
        return .result()
    }
}
Accepted Answer

I figured it out. I needed to add a custom initializer, since I don't have a CarEntity @Parameter to be able to send into the default initializer, I need a plain CarEntity (which can be assigned to the same target variable anyways so that's silly).

init() {}
    
init(carEntity: CarEntity) {
    target = carEntity
}

Hello

Think different

As for SwiftUI, AppIntents are a kind of pseudocode, you don't see all the real code. Try:

let carEntity = CarEntity(car: car)

// Build, then parameter the intent
let openCarIntent = OpenCarIntent()
openCarIntent.target = openCarIntent

return .result(
            value: carEntity,
            opensIntent: openCarIntent
        )
How to initialize OpenIntent parameter when returning OpensIntent in perform
 
 
Q