ByteCountFormatter 简介

ByteCountFormatter

  介绍一个 Foundation 中不常用的小工具类:ByteCountFormatter。这个类很简单,它就是将字节数格式化成适合的描述(KBMBGB 等),还是很方便的。

  示例如下:

let b1000KB1 = ByteCountFormatter.string(fromByteCount: 1000 * 1024, countStyle: .binary)
// Output: "1,000 KB"

let b1000KB2 = ByteCountFormatter.string(fromByteCount: 1000 * 1000, countStyle: .decimal)
// Output: "1 MB"

let byteFormatter = ByteCountFormatter()
byteFormatter.countStyle = .binary
byteFormatter.includesActualByteCount = true
byteFormatter.string(fromByteCount: 1024 * 1024 * 20)
// Output: "20 MB (20,971,520 bytes)"

  果然还是很方便的,居然还有 10001024 的不同方式。

  作为一个 iOS/OS X 开发,看到这个类的名字,会感觉有点熟悉吧,有没有想到这个呢:DateFormatterDateFormatter 常被咱们用来格式化日期。其实它们都继承于 Formatter

Formatter

  NSFormatter 是一个抽象类,就是用来格式化数据的。系统中已经有很多它的子类了:

ByteCountFormatter 格式化比特

  这个就不举例了
  

DateFormatter 格式化时间

let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none

let date = Date(timeIntervalSinceReferenceDate: 118800)

// US English Locale (en_US)
dateFormatter.locale = Locale(identifier: "en_US")
print(dateFormatter.string(from: date)) // Jan 2, 2001

// Japanese Locale (ja_JP)
dateFormatter.locale = Locale(identifier: "ja_JP")
print(dateFormatter.string(from: date)) // 2001/01/02

// Japanese Locale (zh_cn)
dateFormatter.locale = Locale(identifier: "zh_cn")
print(dateFormatter.string(from: date)) // 2001年1月2日

DateComponentsFormatter 格式化时间组件

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.includesApproximationPhrase = true
formatter.includesTimeRemainingPhrase = true
formatter.allowedUnits = [.minute]

// Use the configured formatter to generate the string.
formatter.string(from: 310.0) // About 5 minutes remaining

DateIntervalFormatter 格式化时间间隔

let formatter = DateIntervalFormatter()
formatter.dateStyle = .short;
formatter.timeStyle = .none;

// Create two dates that are exactly 1 day apart.
let startDate = Date()
let endDate = Date(timeInterval: 86400, since: startDate)

// Use the configured formatter to generate the string.
formatter.string(from: startDate, to: endDate) // "9/28/16 - 9/29/16"

EnergyFormatter 格式化物理能量数据(焦耳、卡路里等)

let formatter = EnergyFormatter()
formatter.string(fromValue: 100, unit:.joule) // "100 J"

LengthFormatter 格式化物理长度(cm、m、km 等)

let formatter = LengthFormatter()
formatter.string(fromValue: 100.0, unit: .kilometer) // "100 km"

MassFormatter 格式化物理质量(g、kg 等)

let formatter = MassFormatter()
formatter.string(fromValue: 100.0, unit: .gram) // "100 g"

NumberFormatter 极为丰富的数字格式化类(值得一看)

let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.string(from: 123456.789) // "123,456.789"

formatter.numberStyle = .scientific 
formatter.string(from: 123456.789) // "1.23456789E5"

formatter.numberStyle = .currency
formatter.string(from: 123456.789) // "$123,456.79"

formatter.numberStyle = .percent
formatter.string(from: 0.3) // "30%"

PersonNameComponentsFormatter 人名信息格式化类

var personNameComponents = PersonNameComponents()
personNameComponents.givenName = "振宁"
personNameComponents.familyName = "杨"
personNameComponents.nameSuffix = "先生"
personNameComponents.namePrefix = "物理学博士"

let formatter = PersonNameComponentsFormatter()
formatter.string(from: personNameComponents) // "杨振宁"

formatter.style = .long
formatter.string(from: personNameComponents) // "物理学博士杨振宁先生"

自己的格式化类

  这么多格式化类,基本能满足一般的需求,且它们大都支持本地化的,所以都非常实用。如果这些格式化类都不能满足你的需求,咱们还可以定义自己的格式类。

  要实现自己的格式化类,其实很简单,只需要至少覆盖下面两个方法:

1、string(for:)

2、getObjectValue(_:for:errorDescription:)

  要注意的是,因为一般格式化都是非常耗时的操作,所以在一个对象格式化后,需要将结果字符串与该对象关联起来,免得下回再次解析。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值