Contents

Measurement.FormatStyle.ByteCount

A format style that provides string representations of byte counts, expressed as measurements of information storage.

Declaration

struct ByteCount

Overview

Use this style with a Measurement whose unit type is UnitInformationStorage to format byte counts according to locale conventions.

The following example creates a measurement of 1,024 bytes, and then formats it as an expression of memory storage, with the default byte count format style:

let count = Measurement(value: 1024, unit: UnitInformationStorage.bytes)
let formatted = count.formatted(.byteCount(style: .memory)) // "1 kB"

You can also customize a byte count format style, and use this to format one or more Measurement instances. The following example creates a format style to only use kilobyte units, and to spell out the exact byte count of the measurement.

let count = Measurement(value: 1024, unit: UnitInformationStorage.bytes)
let style = Measurement.FormatStyle.ByteCount(style: .memory,
                                              allowedUnits: .kb,
                                              spellsOutZero: true,
                                              includesActualByteCount: true,
                                              locale: Locale(identifier: "en_US"))
let customFormatted = style.format(count) // "1 kB (1,024 bytes)"

Topics

Creating a byte count style

Accessing style properties

Creating attributed strings

See Also

Applying byte-count styles