Skip to Main Content
iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
book

iOS 编程基础:Swift、Xcode 和Cocoa 入门指南

by Matt Neuburg
January 2017
Intermediate to advanced content levelIntermediate to advanced
552 pages
9h 43m
Chinese
China Machine Press
Content preview from iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
432
10
与之类似,
NSDateComponents
提供了进行日期计算的正确方式。如下示例展示了如何
为给定的日期增加一个月:
let d = NSDate() // or whatever
let comp = NSDateComponents()
comp.month = 1
let greg = NSCalendar(calendarIdentifier:NSCalendarIdentifierGregorian)!
let d2 = greg.dateByAddingComponents(comp, toDate:d, options:[])
你可能还会考虑以字符串表示的日期。如果不对日期的字符串表示进行显式的处理,那
么其字符串表示格式会让你吃惊的。比如,如果
print
一个
NSDate
,那么它会以
GMT
时区的形式表示日期,如果不在这儿住,那么这个结果会让你感到困惑。一个简单的解
决办法就是调用
descriptionWithLocale:
;它
会考虑到用户当前的时区、语言、区域
格式以及日历设置等:
print(d)
// 2016-08-10 22:00:00 +0000
print(d.descriptionWithLocale(NSLocale.currentLocale()))
// Wednesday, August 10, 2016 at 3:00:00 PM Pacific Daylight Time
请使用
NSDateFormatter
来创建并解析日期字符串,它使用了类似于 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Mars Geldard, Jonathon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787111556350