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 AttributedA format style that converts byte counts into attributed strings.struct UnitsThe units to use when formatting a byte count, such as kilobytes or gigabytes.
Initializers
init(from: any Decoder) throwsinit(style: ByteCountFormatStyle.Style, allowedUnits: ByteCountFormatStyle.Units, spellsOutZero: Bool, includesActualByteCount: Bool, locale: Locale)Initializes a byte count format style.
Instance Properties
var allowedUnits: ByteCountFormatStyle.UnitsThe units the format style can use to express the byte count.var attributed: ByteCountFormatStyle.AttributedAn attributed format style based on the byte count format style.var includesActualByteCount: BoolA Boolean value that indicates whether the format style should include the exact byte count, in addition to expressing it in terms of units.var locale: LocaleThe locale to use to format the numeric part of the byte count.var spellsOutZero: BoolA Boolean value that indicates whether the format style should spell out zero-byte values as text.var style: ByteCountFormatStyle.StyleThe semantic style the format style uses to represent a byte count value.
Instance Methods
func format(Int64) -> StringFormats a numeric byte count, using this style.func locale(Locale) -> ByteCountFormatStyleModifies the format style to use the specified locale.
Enumerations
enum StyleThe semantic style to use when formatting a byte count value.
Relationships
Conforms To
FormatStyleSwift.DecodableSwift.EncodableSwift.EquatableSwift.HashableSwift.SendableSwift.SendableMetatype