Chuyển đổi byte sang MB Swift

Kiểu này bị hỏng theo một số cách nhất định trong Xcode 13 [nhưng đã được sửa trong Xcode 14]. Cung cấp bất kỳ đơn vị nào trên gigabyte sẽ gây ra sự cố với thông báo. "Lỗi nghiêm trọng. giá trị Đơn vị không hợp lệ”

Hãy tự mình thử nghiệm để xem liệu bạn có thể làm điều này phù hợp với mình hay không, nhưng bạn có thể muốn quay lại với ByteCountFormatter cũ hơn trong trường hợp này

Tùy chọn có sẵn

Cả hai kiểu định dạng đều có sẵn các tùy chọn sau

Tùy chọnMô tảstyleChỉ định hiển thị số lượng byte lưu trữ hoặc tệp_______0_______0Đơn vị nào sẽ hiển thị

let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
1Chỉ định xem giá trị trống có được hiển thị là “0” hay “Không”
let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
2Chỉ định xem có bao gồm số lượng byte ban đầu hay không

StyleDescription

let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
3Chỉ định hiển thị số lượng byte của tệp. Hành vi thực tế cho điều này là dành riêng cho nền tảng; . 8, cái này sử dụng kiểu thập phân, nhưng kiểu đó có thể thay đổi theo thời gian.
let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
4Chỉ định hiển thị số lượng byte bộ nhớ. Hành vi thực tế cho điều này là dành riêng cho nền tảng; . 8, điều này sử dụng kiểu nhị phân, nhưng điều đó có thể thay đổi theo thời gian.
let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
5Khiến 1000 byte được hiển thị dưới dạng 1 KB. Tốt hơn nên sử dụng
let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
4 hoặc
let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
3 trong hầu hết các trường hợp_______0_______8Làm cho 1024 byte được hiển thị dưới dạng 1 KB. Tốt hơn là sử dụng
let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
4 hoặc
let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"
3 trong hầu hết các trường hợp

UnitsDescription

terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
1Will use all values ​​as appropriate
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
2Uses the default value [
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
1]
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
4As bytes
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
5As kilobytes [1,000 bytes]
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
6As megabytes [1,000,000 bytes]
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
7As gigabytes [1,000,000,000 bytes]
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
8As terabytes [1,000,000,000,000 bytes]
terabyte.formatted[.byteCount[style: .binary].attributed]

terabyteMeasurement.formatted[.byteCount[style: .binary].attributed]
9As petabytes [1,000,000,000,000,000 bytes]ByteCountFormatStyle0As exabytes [1,000,000,000,000,000,000 bytes]ByteCountFormatStyle1As

Bạn có thể cung cấp một ByteCountFormatStyle3 cho tham số ByteCountFormatStyle4 thay vì một đơn vị riêng lẻ. Làm như vậy sẽ khiến kiểu sử dụng đơn vị sẽ cung cấp số lượng chữ số nhỏ nhất

 

// MARK: - Int64

let terabyte: Int64 = 1_000_000_000_000

var integerByteCountStyle = ByteCountFormatStyle[]
integerByteCountStyle.style = .file
integerByteCountStyle.allowedUnits = [.gb, .tb]
integerByteCountStyle.includesActualByteCount = true

integerByteCountStyle.format[terabyte] // "1 TB [1,000,000,000,000 bytes]"
terabyte.formatted[integerByteCountStyle] // "1 TB [1,000,000,000,000 bytes]"

terabyte.formatted[.byteCount[style: .binary]] // "931.32 GB"
terabyte.formatted[.byteCount[style: .decimal]] // "1 TB"
terabyte.formatted[.byteCount[style: .file]] // "1 TB"
terabyte.formatted[.byteCount[style: .memory]] // "931.32 GB"

terabyte.formatted[.byteCount[style: .file, allowedUnits: .bytes]] // "1,000,000,000,000 bytes"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .bytes]] // "1,000,000,000,000 bytes"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .kb]] // "976,562,500 kB"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .mb]] // "953,674.3 MB"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .gb]] // "931.32 GB"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .tb]] // "0.91 TB"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .pb]] // "0 PB"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .zb]] // "0 PB"
terabyte.formatted[.byteCount[style: .file, allowedUnits: .ybOrHigher]] // 0 PB

Int64[1_000].formatted[.byteCount[style: .file, allowedUnits: [.kb, .mb]]] // "1 kB"
Int64[1_000_000].formatted[.byteCount[style: .file, allowedUnits: [.kb, .mb]]] // "1 MB"

Int64.zero.formatted[.byteCount[style: .file, spellsOutZero: true]] // "Zero kB"
Int64.zero.formatted[.byteCount[style: .file, spellsOutZero: false]] // "0 bytes"

Int64[1_000].formatted[.byteCount[style: .file, includesActualByteCount: true]] // "1 kB [1,000 bytes]"

// MARK: - Measurement

let terabyteMeasurement = Measurement[value: 1, unit: UnitInformationStorage.terabytes]

terabyteMeasurement.formatted[.byteCount[style: .binary]] // "931.32 GB"
terabyteMeasurement.formatted[.byteCount[style: .decimal]] // "1 TB"
terabyteMeasurement.formatted[.byteCount[style: .file]] // "1 TB"
terabyteMeasurement.formatted[.byteCount[style: .memory]] // "931.32 GB"

terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .bytes]] // "1,000,000,000,000 bytes"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .bytes]] // "1,000,000,000,000 bytes"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .kb]] // "976,562,500 kB"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .mb]] // "953,674.3 MB"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .gb]] // "931.32 GB"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .tb]] // "0.91 TB"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .pb]] // "0 PB"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .zb]] // "0 PB"
terabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: .ybOrHigher]] // 0 PB

let kilobyteMeasurement = Measurement[value: 1, unit: UnitInformationStorage.kilobytes]
let megabyteMeasurement = Measurement[value: 1, unit: UnitInformationStorage.megabytes]

kilobyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: [.kb, .mb]]] // "1 kB"
megabyteMeasurement.formatted[.byteCount[style: .file, allowedUnits: [.kb, .mb]]] // "1 MB"

let zeroMeasurement = Measurement[value: 0, unit: UnitInformationStorage.bytes]

zeroMeasurement.formatted[.byteCount[style: .file, spellsOutZero: true]] // "Zero kB"
zeroMeasurement.formatted[.byteCount[style: .file, spellsOutZero: false]] // "0 bytes"

megabyteMeasurement.formatted[.byteCount[style: .file, includesActualByteCount: true]] // "1 MB [1,000,000 bytes]"

Tùy chỉnh ngôn ngữ

Bạn có thể đặt ngôn ngữ bằng cách thêm phương thức ByteCountFormatStyle5 vào cuối kiểu định dạng

let franceLocale = Locale[identifier: "fr_FR"]

terabyte.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyte.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyte.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

terabyteMeasurement.formatted[.byteCount[style: .binary].locale[franceLocale]] // "931,32 Go"
terabyteMeasurement.formatted[.byteCount[style: .decimal].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .file].locale[franceLocale]] // "1To"
terabyteMeasurement.formatted[.byteCount[style: .memory].locale[franceLocale]] // "931,32 Go"

Đầu ra chuỗi được phân bổ

Bạn có thể xuất các chuỗi được gán bằng cách thêm phương thức ByteCountFormatStyle6 vào cuối kiểu định dạng

Chủ Đề