Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library. (Pure Swift, Supports Linux)

Overview

SwiftFoundation

Swift Platforms Release License Build Status

Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library.

Goals

  • Provide a cross-platform interface that mimics Apple's Foundation framework.
  • Provide a POSIX-based implementation for maximum portability.
  • Rewrite Foundation with Protocol-Oriented Programming principals.
  • Long-term Pure Swift replacement for the Cocoa frameworks.

Problems with Apple's Foundation

  • Objective-C - Apple's Foundation is an old API designed for Objective-C. While it works great (on Apple's platforms) and has a nice API for Objective-C programming, when imported into Swift, you can see the shortcomings of its 20+ year old API.
  • Unimplemented - The open source version of Apple's Foundation is severly lacking implementation. Most methods are marked with NSUnimplemented(). Only a small subset of Foundation based on CoreFoundation is implemented (e.g. NSArray, NSString, NSDictionary). Basic Functionality like JSON, Base64, and even HTTP requests are not implemented.
  • Portability - Since Apple's Foundation is backed by CoreFoundation, the only supported platforms are currently Linux, Darwin, and (potentially) Windows. Supporting other platforms (e.g. ARM Linux, BSD, SunOS) would require changes to the CoreFoundation codebase, written in C, which is not good for a long term Swift base library. We want all of our code to be understood by any Swift programmer.
  • Protocol Oriented Programming - Perhaps the biggest reason to use this library, is to break free from the old Object-Oriented Programming paradigms. Swift structures and protocols free you from pointers and memory management, along with bugs related to multithreaded environments. Creating structs for basic types like Date and UUID allows you to use let and var correctly. Structs also bring huge performance improvements since the compiler can perform more optimizations and doesn't have to create all the metadata needed for the Swift class runtime.

Targeted Platforms

Implemented

To see what parts of Foundation are implemented, just look at the unit tests. Completed functionality will be fully unit tested. Note that there is some functionality that is written as a protocol only, that will not be included on this list.

  • Base64
  • Data
  • Date
  • FileManager
  • JSON
  • RegularExpression (POSIX, not ICU)
  • Thread
  • URL
  • UUID

License

This program is free software; you can redistribute it and/or modify it under the terms of the MIT License.

Comments
  • 'POSIXError' does not conform to 'ErrorType'

    'POSIXError' does not conform to 'ErrorType'

    I get this error when trying to compile on OSX with the latest swift 2.2 dec 31 snapshot.

    Compiling Swift Module 'SwiftFoundation' (51 sources) /SwiftFoundationExample/Packages/SwiftFoundation-1.1.0/Sources/SwiftFoundation/POSIXFileSystemStatus.swift:28:45: error: thrown expression type 'POSIXError' does not conform to 'ErrorType' throw POSIXError.fromErrorNumber!

    darwin 
    opened by madhavajay 7
  • Be more modular

    Be more modular

    I'm looking for a good foundation-free JSON serializer and came upon PureSwift's, which has the barebones - exactly what I need. However, including the entire SwiftFoundation project is overkill if I just want JSON.

    I propose to split all the large sections into separate repositories, and then this repository would just be the core that links them together and conforms the types to the necessary (probably FoundationConvertible) protocols. This might be a pain to do right now, but in the long run it's a much better solution.

    suggestion 
    opened by Danappelxx 4
  • base64 tests

    base64 tests

    For base64 tests you might want to include the ones here in addition to the tests you already have: https://github.com/drichardson/SwiftyBase64/blob/master/SwiftyBase64Tests/SwiftyBase64Tests.swift since they specifically cover different cases the base64 encoder has to handle (thanks for the header attribution, btw).

    Also, I think an open source, cross platform, pure swift replacement for Foundation is a great idea.

    enhancement suggestion unit test 
    opened by drichardson 4
  • Compilation under Windows

    Compilation under Windows

    Hey,

    this is not really an issue but more a feature. I want to implement a cross platform (macos, iOS, Windows, Linux) tool and need some JSON de-serialization and file management. Currently I think about which library I want to use. My main question is if it is generally possible to compile Swift foundation under Windows since the OS is not listed in your readme.

    enhancement question suggestion feature 
    opened by Blackjacx 2
  • Pure Swift extension methods for getting substrings from RegularExpressionMatch.Range and Range<Int>

    Pure Swift extension methods for getting substrings from RegularExpressionMatch.Range and Range

    Previous example code (e.g., in the RegularExpressionTests) relied on NSString and NSRange to obtain the matching string or the captured groups.

    For example:

    let stringRange = NSRange(match.range)
    
    let matchString = (string as NSString).substringWithRange(stringRange)
    

    With this code, it can be shortened, with a pure Swift implementation:

    let matchString = string.substring(match.range)
    

    Also added a test case for emoji support in RegularExpression that uses these methods.

    opened by cradnovich 2
  • add init to Date:  millisecondsSince1970

    add init to Date: millisecondsSince1970

    This convenience initializer take milliseconds as Long instead of a Double value of seconds. This is similar to the Date class in Javascript, and is useful for many JSON APIs that use number of milliseconds since epoch to represent dates.

    Further I think the word TimeInterval should be renamed to Seconds in the API of the Date object to be less vague.

    opened by nmn 2
  • Failed to compile `SwiftFoundation/Thread.swift`.

    Failed to compile `SwiftFoundation/Thread.swift`.

    On 713b3ce:

    First, not found CUUID.

    SwiftFoundation/Sources/SwiftFoundation/UUID.swift:14:12: error: no such module 'CUUID'
        import CUUID
               ^
    

    Edited Package.swift fixing the above error:

    --- a/Package.swift
    +++ b/Package.swift
    @@ -11,7 +11,8 @@ let package = Package(
         ],
         dependencies: [
             .Package(url: "https://github.com/PureSwift/CStatfs.git", majorVersion: 1),
    -        .Package(url: "https://github.com/PureSwift/CJSONC.git", majorVersion: 1)
    +        .Package(url: "https://github.com/PureSwift/CJSONC.git", majorVersion: 1),
    +        .Package(url: "https://github.com/PureSwift/CUUID.git", majorVersion: 1)
         ],
         exclude: ["Xcode", "Carthage"]
     )
    

    Then failed to compile SwiftFoundation/Thread.swift:

    `` src/SwiftFoundation/Sources/SwiftFoundation/Thread.swift:30:54: error: va$ ue of type 'Unmanaged<Thread.Closure>' has no member 'toOpaque' let pointer = UnsafeMutablePointer(holder.toOpaque()) ^~~~~~ ~~~~~~~~ src/SwiftFoundation/Sources/SwiftFoundation/Thread.swift:89:65: error: ca$ not convert value of type 'UnsafeMutablePointer' (aka 'UnsafeMutablePoint$ r<()>') to expected argument type 'OpaquePointer' let unmanaged = Unmanaged<Thread.Closure>.fromOpaque(arg!) ~~~^

    opened by weakish 1
  • Not really an issue

    Not really an issue

    Hey man! I really liked this. I saw you forked my HTTP Server / Web Framework. Really enjoying this pure swift experiment. I see you have done a lot of similar work like, regular expressions, base64, etc.. This is really cool. I'm going to take a closer look later. It would be very interesting to have a centralized solution for this new "problem" of having code that runs on Linux as well. Willing to contribute to this!

    Talking about another matter that it's very important, as well. I think it was you who did a post about using pure swift frameworks in command line applications by faking a bundle, was it? Is there another way? Swift not having a stable ABI sucks, and they (apple) have no clue when it'll happen.

    Having some sort of package management like NPM and the likes would be awesome too, because the current ones (cocoapod, carthage) aren't doing the trick. Will they ever?

    Sorry for using an issue to contact here. Is there a better way to contact another user in github?

    invalid question 
    opened by paulofaria 1
  • Date Formatter Support?

    Date Formatter Support?

    Hey folks, I'm working on a project that requires DateFormatter support. Is this on the roadmap for this project? If it's not, is this feature something you'd accept in a contribution?

    opened by oxcug 0
  • Updated for Swift 5

    Updated for Swift 5

    • Updated types for Swift 5 API compatibility.
    • Removed all non-standard (Apple) Foundation API.
    • Removed Carthage support
    • Added WASM support
    • Updated unit tests
    opened by colemancda 0
  • Add Apple Foundation compatibility

    Add Apple Foundation compatibility

    Motivation

    Apple has finally implemented Pure Swift value types for Foundation in Swift 3.0, but there are some issues with this:

    • Only a subset of types like Date are truly value type structs, the rest are structs, but backed by a reference type.
    • JSON is still AnyObject, not an enum
    • OSS Foundation is huge, will still have the CoreFoundation codebase, and it will still keep the NS* classes. This is not desirable for a Pure Swift portable library.
    • With Xcode, SwiftFoundation is a PITA to use because the types conflict with the new Foundation value types, which are imported implicitly.
    • They have only implemented this as a Swift addition in Darwin, the Linux support is still lacking, hence making this project still necessary.
    • This project provides a lot of functionality that OSS Foundation does not provide.
    • The compiled binary for this project is tiny compared to OSS Foundation.
    • OSS Foundation still depends on non-posix C libraries like CoreFoundation
    • Not as portable as SwiftFoundation because it's not truly Pure Swift.

    Solution

    • Make conflicting types only compile on Linux. On Darwin we will remove the implementation of conflicting types, and make any additions extensions (e.g. RawRepresentable for UUID).
    • Update APIs to have to mirror the Darwin APIs
    • Keep all additions (e.g. HTTP.StatusCode, Null, JSON.Value) for both platforms.
    • Remove FoundationConvertible since ReferenceConvertible exists.
    enhancement linux darwin feature 
    opened by colemancda 0
  • Building on OS X with SPM fails

    Building on OS X with SPM fails

    This is because the JSONParse.swift uses the JSON module on OS X:

    https://github.com/PureSwift/SwiftFoundation/blob/develop/Sources/SwiftFoundation/JSONParse.swift#L9

    but in the Package.swift the CJSONC module is defined as a dependancy instead:

    https://github.com/PureSwift/SwiftFoundation/blob/develop/Package.swift#L8

    Is there a reason for no using CJSONC on OS X or vice versa for JSON on Linux?

    invalid wontfix darwin 
    opened by ky1ejs 13
Releases(3.0.0)
Owner
Open Source #PureSwift frameworks
null
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift

Bow is a cross-platform library for Typed Functional Programming in Swift. Documentation All documentation and API reference is published in our websi

Bow 613 Dec 20, 2022
Generic Cross Platform Signal Handler

Signals Generic Cross Platform Signal Handler. Prerequisites Swift Swift Open Source swift-4.0.0-RELEASE toolchain (Minimum REQUIRED for latest releas

Kitura 91 Oct 2, 2022
Pure Declarative Programming in Swift, Among Other Things

Basis The Basis is an exploration of pure declarative programming and reasoning in Swift. It by no means contains idiomatic code, but is instead inten

TypeLift 314 Dec 22, 2022
A Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and other native frameworks.

ZamzamKit ZamzamKit is a Swift package for rapid development using a collection of micro utility extensions for Standard Library, Foundation, and othe

Zamzam Inc. 261 Dec 15, 2022
A μframework of extensions for SequenceType in Swift 2.0, inspired by Python's itertools, Haskell's standard library, and other things.

SwiftSequence Full reference here. (If you're looking for data structures in Swift, those have been moved to here) SwiftSequence is a lightweight fram

Donnacha Oisín Kidney 376 Oct 12, 2022
This is a app developed in Swift, using Object Oriented Programing, UIKit user interface programmatically, API Request and Kingfisher to load remote images

iOS NOW ⭐ This is a app developed in Swift, using Object Oriented Programing, UIKit user interface programmatically, API Request and Kingfisher to loa

William Tristão de Paula 1 Dec 7, 2021
How Swift standard types and classes were supposed to work.

How Swift standard types and classes were supposed to work. A collection of useful extensions for the Swift Standard Library, Foundation, and UIKit.

Goktug Yilmaz 3k Dec 22, 2022
Extensions for Swift Standard Types and Classes

Cent Cent is a library that extends certain Swift object types using the extension feature and gives its two cents to Swift language. Dollar is a Swif

Ankur Patel 225 Dec 7, 2022
This package will contain the standard encodings/decodings/hahsing used by the String Conversion Tool app.

This package will contain the standard encodings/decodings/hahsing used by the String Conversion Tool app. It will also, however, contain extra encoding/decoding methods (new encoding/decoding)

Gleb 0 Oct 16, 2021
Globant iOS Academy Base Project

The repository will be used for trainees to save course progress.

null 0 Aug 18, 2022
A parser combinator library written in the Swift programming language.

SwiftParsec SwiftParsec is a Swift port of the Parsec parser combinator library. It allows the creation of sophisticated parsers from a set of simple

David Dufresne 219 Nov 6, 2022
A Cocoa library to extend the Objective-C programming language.

The Extended Objective-C library extends the dynamism of the Objective-C programming language to support additional patterns present in other programm

Justin Spahr-Summers 4.5k Dec 30, 2022
XMachOViewer is a Mach-O viewer for Windows, Linux and MacOS

MachO file viewer/editor for Windows, Linux and macOS. Heuristic scan String viewer Hex viewer Disasm viewer(x86/64,ARM,PPC,m68k) Entropy viewer Hash

Hors 505 Dec 20, 2022
OTAtomics - Multi-platform Swift thread-safe atomics library

OTAtomics Multi-platform Swift thread-safe atomics library. The library has full

Steffan Andrews 2 Jul 8, 2022
Functional programming in Swift

Swiftz Swiftz is a Swift library for functional programming. It defines functional data structures, functions, idioms, and extensions that augment the

TypeLift 3.3k Dec 25, 2022
Numerals is a package containing additional numeric types for the Swift programming language.

swift-numerals Numerals is a package containing additional numeric types for the Swift programming language. Contents The package currently provides t

Alexandre H. Saad 0 Jul 28, 2022
A repository for showcasing my knowledge of the Swift programming language, and continuing to learn the language.

Learning Swift (programming language) I know very little about programming in the Swift programming language. This document will go over all my knowle

Sean P. Myrick V19.1.7.2 2 Nov 8, 2022
swift-highlight a pure-Swift data structure library designed for server applications that need to store a lot of styled text

swift-highlight is a pure-Swift data structure library designed for server applications that need to store a lot of styled text. The Highlight module is memory-efficient and uses slab allocations and small-string optimizations to pack large amounts of styled text into a small amount of memory, while still supporting efficient traversal through the Sequence protocol.

kelvin 4 Aug 14, 2022