Skip to content

Structure

ByteCountFormatStyle

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
A format style that provides string representations of byte counts.
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

Structures

  • struct Attributed
    A format style that converts byte counts into attributed strings.
  • struct Units
    The units to use when formatting a byte count, such as kilobytes or gigabytes.

Initializers

Instance Properties

Instance Methods

Enumerations

  • enum Style
    The semantic style to use when formatting a byte count value.

Relationships

Conforms To

  • FormatStyle
  • Swift.Decodable
  • Swift.Encodable
  • Swift.Equatable
  • Swift.Hashable
  • Swift.Sendable
  • Swift.SendableMetatype