7

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?

matt
  • 515,959
  • 87
  • 875
  • 1,141
La Win Ko
  • 254
  • 2
  • 10
  • From your second link (emphasis added): *“This project, swift-corelibs-foundation, provides an implementation of the Foundation API for platforms **where there is no Objective-C runtime.** On macOS, iOS, and other Apple platforms, apps should use the Foundation that comes with the operating system.”* – Martin R May 31 '20 at 17:28
  • @MartinR Sorry, I will delete the link. However, I can still asking this question right? – La Win Ko May 31 '20 at 17:31
  • 2
    @LaWinKo You've missed the point. The idea is not to delete the link, the idea is that the link answers your question. – matt May 31 '20 at 17:44

2 Answers2

15

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    Thanks, I understand now. I was confused between built-in Foundation for apple OS and Foundation for linux OS. – La Win Ko May 31 '20 at 17:56
0

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]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205