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()
}
}