La Win Ko
La Win Ko

Reputation: 304

Swift foundation vs standard library?

Question - What is the difference between Swift Foundation and standard library?

I know they are not in the same repository. Swift standard library and Swift Foundation.

My understanding is that Swift standard library is low level library that are support for core data types, array, etc... written in swift. And Swift Foundation is for higher level which are already included common utilities codes written in swift language. But my confusion is that why are the objective-c classes like NSArray are included in Foundation library? Can you explain me with more details?

Upvotes: 9

Views: 1902

Answers (2)

yoAlex5
yoAlex5

Reputation: 34225

Swift Standard Library

NeXTSTEP -> Cocoa -> Swift Standard Library

[Cocoa]

Swift Standard Library:

  • describes fundamental data types, protocols, functions... their definitions and algorithms to work with
  • implementation - swift/stdlib/public/
    • Standard library core - swift/stdlib/public/core/ - data structures and algorithms
    • Runtime(Swift Run Time Library) - swift/stdlib/public/runtime/ - low level stuff which written on more low level language(C++, Objective-C) which is a layer between compiler and Standard library core. It responsible for dynamism(runtime features) - memory management, reflection...
    • SDK Overlays - which helps to support compatibility with Swift with Objective-C
      • Swift Foundation - swift/stdlib/public/Darwin/Foundation/ -> swift-corelibs-foundation is a part of SDK Overlays which allows you to work with Objective-C Foundation framework from Swift codebase.

[Binary representation]

Upvotes: 2

matt
matt

Reputation: 535138

To understand what's going on here, first distinguish three things:

  • Swift library is the Swift native types, like String and Array.

  • Foundation is the Cocoa Objective-C basic set of types, like NSString and NSArray and NSDate and NSData.

  • Swift also contains an overlay library that shadows (without obscuring) types in the Foundation. For example, Date shadows NSDate, and Data shadows NSData. This makes the Cocoa Foundation a lot easier to use in Swift.

Note that there are two very different relations in which a Swift type can stand with respect to a Cocoa Objective-C type.

  • String and Array are completely independent native Swift types. They are bridged to NSString and NSArray respectively, but they exist without Foundation.

  • Data and Date are merely facades for NSData and NSDate respectively. If you import Swift but not Foundation, Data and Date are not even present.

Okay, so far so good. But this situation presents a quandary, because one would like to use Data and Date without the need for Foundation, in places like Linux that do not have it in the first place. Therefore, the project you have pointed to, https://github.com/apple/swift-corelibs-foundation, provides a backing for Data and Date (and so on) independent of Foundation. But if you are developing for iOS or MacOS you would never use it, because the real Foundation is present.

Upvotes: 18

Related Questions