What is `@Dependency`?

I came across @Dependency in the context of AppIntent. Is this a Swift feature or something specific to AppIntents?

Are there any official documents about it (I couldn't find ones)? I assume it’s related to dependency injection, but I’m not entirely sure how or when it should be used in this context.

struct ResumeTrailActivity: ResumeWorkoutIntent {
    static let openAppWhenRun: Bool = true
    static let title: LocalizedStringResource = "Resume Trail Activity"
    static let description = IntentDescription("Resumes tracking a trail activity.",
                                               categoryName: "Activity Tracking",
                                               searchKeywords: ["workout"])
    
    @Dependency
    private var activityTracker: ActivityTracker
    
    func perform() async throws -> some IntentResult {
        await activityTracker.resumeActivity()
        // Set the Action button functionality back to `NextTrailManeuver`.
        return .result(actionButtonIntent: NextTrailManeuver())
    }
}

Apparently it's a typealias for this AppDependency property wrapper: AppDependency | Apple Developer Documentation

According to the .swiftinterface file for AppIntents, there's an underscore-prefixed protocol that some of the types conform to:

@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
public protocol _SupportsAppDependencies {
}

and that protocol defines the alias so that it's in scope when you define a type that transitively conforms to that protocol:

@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
extension AppIntents._SupportsAppDependencies {
  public typealias Dependency = AppIntents.AppDependency
}

Since it's part of an underscore-prefixed protocol, that appears to cause it to be excluded from the framework's documentation.

Since this is an Apple framework issue rather than a Swift issue, you might want to file a Feedback Request with Apple to improve this.

1 Like

Thank you for the info! That helps a lot.