Initializer

init(activityType:start:end:)

Instantiates a new workout.

Declaration

convenience init(activityType workoutActivityType: HKWorkoutActivityType, start startDate: Date, end endDate: Date)

Parameters

workoutActivityType

The type of activity being performed during the workout. For a list of possible activity types, see 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.

Return Value

A workout activity.

Discussion

The workout’s duration is calculated from its start and end times. The workout’s total distance, total energy burned, workout events, device, and metadata are all set to nil.

let basketball = HKWorkout(activityType:HKWorkoutActivityType.Basketball,
                           startDate: start, endDate: end)
 
 
healthStore.saveObject(basketball) { (success, error) -> Void in
    guard success else {
        // Perform proper error handling here...
        fatalError("*** An error occurred while saving this " +
            "workout: \(error?.localizedDescription)")
    }
}

See Also

Creating Workouts