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?

- 55,199
- 118
- 297
- 429
-
I think it's same, Apple change NSDate to Date in Swift 3 but I'm not sure. – Avi Rok Oct 01 '16 at 20:53
5 Answers
Swift 3 introduced some new overlay value types for existing
Foundation class types, such as Date
for NSDate
, Data
for
NSData
and some more. The full list and details can be found in
Some of the reasons were
- Provide proper value semantics,
let
andvar
instead of mutable and immutable variants,- more "Swifty" APIs.
The new overlay types should provide all functionality that the corresponding Foundation type has, but if necessary, you can always cast from one type to the other.
When existing Foundation APIs are imported into Swift, the types are bridged automatically.
With respect to Date
and NSDate
: Date
is a value type
and can be a constant or variable:
var date = Date()
date += 10.0 // Add 10 seconds
whereas NSDate
is a reference type and immutable.
Also Date
is Comparable
let date1 = Date()
let date2 = Date()
if date1 < date2 { }
whereas NSDate
s can only be compared with .compare()
.
Remark: For these "overlay types", the value type (struct)
such as Date
and its Foundation counterpart (class) such as NSDate
are different types and both can be used from Swift.
It must not be confused with
where the NS
prefix has simply been dropped for certain Foundation
classes, e.g. NSBundle
is renamed to Bundle
for Swift 3.

- 529,903
- 94
- 1,240
- 1,382
-
1Thanks Martin. Are there guidelines on which class to use when doing "standard" Swift development? – Marcus Leon Oct 01 '16 at 21:17
-
3@MarcusLeon: Since Swift already bridges to the new overlay type when importing existing Foundation APIs, one should prefer to use the new types. The advantages are listed in the proposal that I linked to (such as proper value semantics). – If some method is not available then you can cast back to the Foundation type and call the old method. Example: http://stackoverflow.com/questions/39519144/nslocale-swift-3. – Martin R Oct 01 '16 at 21:21
NS classes are Objective-C classes. As Objective-C is the older programming language for iOS or MacOS applications, Swift allows you to use those classes structs in your code. So you can migrate applications from Objective-C to Swift if you want to.
The non-NS classes or structs are the Swift equivalents of the the NS classes or structs. You can read more about this issue here or here.
But be aware: As Objective-C and Swift do are different programming languages, it can happen that NS and non-NS classes are not completely working the same way.

- 444
- 5
- 8
As well as the answers given above, in a more surface literal design sense, NS was a prefix given to everything in the NextStep days, before Steve Jobs was called back to Apple, and so was a natural part of Objective-C syntax.
Fast forward to the development of Swift, they needed a point of obvious difference, abd it could well have been CL as a prefix, (for Chris Lattner) but obviously chose to have an absence of any prefix

- 179
- 1
- 13
The difference between NS and non NS classes is that the NS classes are reference type whereas the non NS classes are value type . In swift 3 , the Apple has introduced Foundation classes like NSDate and re-named them to Date but since swift is based on value types rather than reference type therefore Date is a value type. For instance:
let date = NSDate() //1
date.dateByAddingTimeInterval(1000) //2
2 . Permissible as although date is constant but mutability is dependent on class rather than let vs variable.
whereas
let date = Date() //3
date.dateByAddingTimeInterval(1000) //4
4. is not allowed not allowed and would result in Error as let makes 'date' a constant with 'let' as date is Date() type which is a value type.

- 11
- 2
-
Minor clarifications: First, not all of these non-`NS` types are value types (e.g. `Operation` and `OperationQueue`). But, yes, in general, they are, and `Date` certainly is. Second, it's worth noting that it doesn't make sense to refer to the non-`NS` "classes" as the vast majority of them aren't classes at all. Most of them are `struct` value types. – Rob Oct 01 '16 at 21:32
-
1The second one fails not because `date` is a constant but because `dateByAddingTimeInterval` does not exist on `Date`. The equivalent would be `date + 1000`, which is legal. – Tom Harrington Oct 01 '16 at 22:38
-
Yeah, the `Date` syntax of line 4 is either `date.addingTimeInterval(1000)` or `date + 1000` ... – Rob Oct 01 '16 at 22:45
NSDate
and Date
are not the same thing. NS (next step) classes are at the current moment only used in Objective-C so in Objective-C to represent a date variable NSDate.
In regards to specifically Swift 3.0 the newest release of Swift Apple has entirely removed the need of using the NS prefix to any of the types. So just in Swift three we now use Date instead of NSDate. to make your code look cleaner

- 1,162
- 2
- 12
- 22
-
No, that is a different thing. `Date` and `NSDate` are *not* the same type, they are not covered in https://github.com/apple/swift-evolution/blob/master/proposals/0086-drop-foundation-ns.md. – Both NSDate and Date still exist can can be used in Swift. – Martin R Oct 01 '16 at 21:03
-
-
4NS is still required in some places in Swift 3, for instance with Core Data. I have an attribute that stores a Date. When I try to assign a Date() to it I receive an error that says "cannot assign value of type 'Date' to type 'NSDate'. If however I cast it to NSDate in the assignment it works fine. let theDate = Date(); coreDataEntity.date = theDate as NSDate – Scooter Dec 19 '16 at 13:29