---
title: "init(activityType:start:end:duration:totalEnergyBurned:totalDistance:device:metadata:)"
framework: healthkit
role: symbol
role_heading: Initializer
path: "healthkit/hkworkout/init(activitytype:start:end:duration:totalenergyburned:totaldistance:device:metadata:)"
---

# init(activityType:start:end:duration:totalEnergyBurned:totalDistance:device:metadata:)

Instantiates a new workout activity that includes the device that produced the sample data.

## Declaration

```swift
convenience init(activityType workoutActivityType: HKWorkoutActivityType, start startDate: Date, end endDate: Date, duration: TimeInterval, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, device: HKDevice?, metadata: [String : Any]?)
```

## Parameters

- `workoutActivityType`: The type of activity performed during the workout. For a complete list of activity types, see doc://com.apple.healthkit/documentation/HealthKit/HKWorkoutActivityType.
- `startDate`: The date and time when the activity started.
- `endDate`: The date and time when the activity ended. This date must be equal to or later than the start date.
- `duration`: A time interval representing the workout’s actual duration. Passing a nonzero value sets the workout’s doc://com.apple.healthkit/documentation/HealthKit/HKWorkout/duration property. If you pass 0, this method calculates the doc://com.apple.healthkit/documentation/HealthKit/HKWorkout/duration property based on the workout’s start and end dates.
- `totalEnergyBurned`: A quantity using energy units, or nil. This property sets the workout’s doc://com.apple.healthkit/documentation/HealthKit/HKWorkout/totalEnergyBurned property. It represents the total active energy burned during the workout.
- `totalDistance`: A quantity using length units, or nil. This property sets the workout’s doc://com.apple.healthkit/documentation/HealthKit/HKWorkout/totalDistance property.
- `device`: The device that generated the data for this sample.
- `metadata`: The metadata dictionary contains extra information describing this workout. The dictionary’s keys are all doc://com.apple.documentation/documentation/Foundation/NSString objects . The values may be doc://com.apple.documentation/documentation/Foundation/NSString, doc://com.apple.documentation/documentation/Foundation/NSNumber, or doc://com.apple.documentation/documentation/Foundation/NSDate objects. For a complete list of predefined metadata keys, see Metadata Keys. Using predefined keys helps facilitate sharing data between apps; however, you are also encouraged to create your own, custom keys as needed to extend the workout’s capabilities.

## Return Value

Return Value A workout object with the provided duration, total energy burned, total distance, device, and metadata properties. The workoutEvents property is set to nil.

## Discussion

Discussion This method returns a workout with the specified duration, total energy burned, total distance, device, and metadata. The workoutEvents property is set to nil. If the total energy burned or total distance are non-zero values, create a set of corresponding samples that add up to the calculated totals. Associate these samples with the workout by calling the health store’s add(_:to:completion:) method.

## See Also

### Related Documentation

- [workoutActivityType](healthkit/hkworkout/workoutactivitytype.md)
- [totalDistance](healthkit/hkworkout/totaldistance.md)
- [duration](healthkit/hkworkout/duration.md)
- [metadata](healthkit/hkobject/metadata.md)
- [totalEnergyBurned](healthkit/hkworkout/totalenergyburned.md)
- [workoutEvents](healthkit/hkworkout/workoutevents.md)

### Creating workouts

- [init(activityType:start:end:)](healthkit/hkworkout/init(activitytype:start:end:).md)
- [init(activityType:start:end:duration:totalEnergyBurned:totalDistance:metadata:)](healthkit/hkworkout/init(activitytype:start:end:duration:totalenergyburned:totaldistance:metadata:).md)
- [init(activityType:start:end:workoutEvents:totalEnergyBurned:totalDistance:metadata:)](healthkit/hkworkout/init(activitytype:start:end:workoutevents:totalenergyburned:totaldistance:metadata:).md)
- [init(activityType:start:end:workoutEvents:totalEnergyBurned:totalDistance:device:metadata:)](healthkit/hkworkout/init(activitytype:start:end:workoutevents:totalenergyburned:totaldistance:device:metadata:).md)
- [init(activityType:start:end:workoutEvents:totalEnergyBurned:totalDistance:totalFlightsClimbed:device:metadata:)](healthkit/hkworkout/init(activitytype:start:end:workoutevents:totalenergyburned:totaldistance:totalflightsclimbed:device:metadata:).md)
- [init(activityType:start:end:workoutEvents:totalEnergyBurned:totalDistance:totalSwimmingStrokeCount:device:metadata:)](healthkit/hkworkout/init(activitytype:start:end:workoutevents:totalenergyburned:totaldistance:totalswimmingstrokecount:device:metadata:).md)
