swift-corelibs-foundation

repository·main·Indexed 26 days ago

https://github.com/swiftlang/swift-corelibs-foundation

A compatibility implementation of the Foundation API for non-Darwin platforms without an Objective-C runtime. It provides a bridge for legacy Foundation API code and includes implementations of NSObject, NSFormatter, and NSKeyedArchiver, as well as the plutil command-line tool for interacting with property list files.

Tokens
661
Snippets
2
Records
5
Agent score
40%

What's inside swift-corelibs-foundation

  1. Understand the Foundation ecosystem

    main

    Foundation provides basic utility classes, data structures, and internationalization support. Depending on your platform, you will interact with different implementations:

    • On Apple platforms (macOS, iOS, etc.): Use the built-in Foundation framework provided by the operating system.
    • On non-Darwin platforms: Use swift-corelibs-foundation, which provides a compatibility implementation of the Foundation API (including NSObject, NSFormatter, and NSKeyedArchiver).
    • Swift Foundation: A shared library in the Swift toolchain providing core types like URL, Data, JSONDecoder, Locale, and Calendar via the FoundationEssentials and FoundationInternationalization modules.
    • Foundation ICU: A private library wrapping ICU for internationalization, imported via FoundationInternationalization.
  2. Build swift-corelibs-foundation on Windows

    main

    Building on Windows requires manual setup of dependencies like zlib, curl, and libxml because they are not provided by the host OS.

    1. Use CMake to checkout and build the required dependencies using the WindowsSwiftPMDependencies target:
      cmake -G Ninja -B <build folder> -DFOUNDATION_SWIFTPM_DEPS=YES
      cmake --build <build folder> --target WindowsSwiftPMDependencies
    2. The build output will provide a list of environment variables. Set these environment variables in your terminal.
    3. Run swift build or swift test as you would on Linux.
  3. Use Foundation in a Swift project

    main

    To use Foundation, ensure you have the latest Swift binary distribution installed. You can import the module and use types like URLComponents directly in your Swift code. It is recommended to use the Swift Package Manager (SwiftPM) to build your applications.

    import Foundation
    
    // Make a URLComponents instance
    let swifty = URLComponents(string: "https://swift.org")!
    
    // Print something useful about the URL
    print("\(swifty.host!)")
    
    // Output: "swift.org"
  4. Use the plutil CLI tool

    main
    The plutil command-line tool is used to interact with property list (plist) files. It is invoked via the command line and processes arguments provided to the PLUCommand executor. The tool outputs results to standard output and error messages to standard error. It returns an exit code of 0 (EXIT_SUCCESS) on success and 1 (EXIT_FAILURE) on error.