本文介绍了Swift 3:日期与 NSDate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NS 类和非 NS 类有什么区别?特别是 NSDate vs Date?NS 是否代表了围绕核心非 NS 功能的某种类型的包装器?

What is the difference between the NS and non NS classes? Particularly NSDate vs Date? Does NS represent some type of wrapper around the core non NS functionality?

推荐答案

Swift 3 为现有的覆盖值类型引入了一些新的基础类类型,例如 DateNSDateDataNSData 等等.完整列表和详细信息可以在

Swift 3 introduced some new overlay value types for existingFoundation class types, such as Date for NSDate, Data forNSData and some more. The full list and details can be found in

部分原因是

  • 提供适当的值语义,
  • letvar 而不是可变和不可变的变体,
  • 更多Swifty"API.
  • Provide proper value semantics,
  • let and var instead of mutable and immutable variants,
  • more "Swifty" APIs.

新的覆盖类型应该提供所有功能相应的 Foundation 类型具有,但如有必要,您总是可以从一种类型转换为另一种类型.

The new overlay types should provide all functionality thatthe corresponding Foundation type has, but if necessary, youcan always cast from one type to the other.

当现有的 Foundation API 是导入到 Swift 中,类型会自动桥接.

When existing Foundation APIs areimported into Swift, the types are bridged automatically.

关于DateNSDate:Date是一个值类型并且可以是常量或变量:

With respect to Date and NSDate: Date is a value typeand can be a constant or variable:

var date = Date()
date += 10.0 // Add 10 seconds

NSDate 是引用类型且不可变.另外 DateComparable

whereas NSDate is a reference type and immutable.Also Date is Comparable

let date1 = Date()
let date2 = Date()

if date1 < date2 { }

NSDates 只能与 .compare() 进行比较.

whereas NSDates can only be compared with .compare().

备注:对于这些覆盖类型",值类型(struct)例如 Date 和它的 Foundation 对应物(类),例如 NSDate是不同的类型,两者都可以在 Swift 中使用.不得与

Remark: For these "overlay types", the value type (struct)such as Date and its Foundation counterpart (class) such as NSDateare different types and both can be used from Swift.It must not be confused with

对于某些 Foundation,NS 前缀被简单地删除了类,例如NSBundle 在 Swift 3 中重命名Bundle.

where the NS prefix has simply been dropped for certain Foundationclasses, e.g. NSBundle is renamed to Bundle for Swift 3.

这篇关于Swift 3:日期与 NSDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!