Contents

ByteCountFormatStyle

A format style that provides string representations of byte counts.

Declaration

struct ByteCountFormatStyle

Overview

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

let count: Int64 = 1024
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 Int64 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 style = ByteCountFormatStyle(style: .memory,                                 
                                 allowedUnits: [.kb],
                                 spellsOutZero: true,
                                 includesActualByteCount: false,
                                 locale: Locale(identifier: "en_US"))
let counts: [Int64] = [0, 1024, 2048, 4096, 8192, 16384, 32768, 65536]
let formatted = counts.map ( {style.format($0) } ) // ["Zero kB", "1 kB", "2 kB", "4 kB", "8 kB", "16 kB", "32 kB", "64 kB"]

Topics

Creating a byte count style

Customizing style behavior

Accessing style properties

Creating attributed strings

See Also

Applying byte-count styles