Initializer

init(activityType:start:end:)

Instantiates and returns a new workout activity.

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

init(activityType: HKWorkoutActivityType, start: Date, end: Date, duration: TimeInterval, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, metadata: [String : Any]?)

Instantiates and returns a new workout activity that includes the total energy burned, total distance, and metadata for the workout.

init(activityType: HKWorkoutActivityType, start: Date, end: Date, workoutEvents: [HKWorkoutEvent]?, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, metadata: [String : Any]?)

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, and metadata properties.

init(activityType: HKWorkoutActivityType, start: Date, end: Date, duration: TimeInterval, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, device: HKDevice?, metadata: [String : Any]?)

Instantiates and returns a new workout activity that includes the total energy burned, total distance, device, and metadata for the workout.

init(activityType: HKWorkoutActivityType, start: Date, end: Date, workoutEvents: [HKWorkoutEvent]?, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, device: HKDevice?, metadata: [String : Any]?)

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, device, and metadata properties.

init(activityType: HKWorkoutActivityType, start: Date, end: Date, workoutEvents: [HKWorkoutEvent]?, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, totalFlightsClimbed: HKQuantity?, device: HKDevice?, metadata: [String : Any]?)

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, total flights, device, and metadata properties.

init(activityType: HKWorkoutActivityType, start: Date, end: Date, workoutEvents: [HKWorkoutEvent]?, totalEnergyBurned: HKQuantity?, totalDistance: HKQuantity?, totalSwimmingStrokeCount: HKQuantity?, device: HKDevice?, metadata: [String : Any]?)

Instantiates and returns a workout whose duration is calculated based on the start and end dates and the provided workout events; this method also specifies the total distance, total energy, total stroke count, device, and metadata properties.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software