Foundation, now in Swift!

Fito ToledanoFito Toledano
5 min read

Swift is about to get even faster, and so are your apps.

Photo by Joel Filipe on <a href="https://unsplash.com/s/photos/foundation?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyT

The Core Team at Apple has announced that the Foundation framework is going to be rewritten in Swift.

But what does this mean? First of all, we need to understand what the Foundation framework is, and why is it important to an iOS developer.

The Foundation framework is the underlying base that provides functionalities for almost all other frameworks that are used in development for Apple platforms, like the iPhone, the iPad, the Apple Watch, the Mac, and even Apple TV.

You can find examples of why the Foundation framework is so important very easily. Just about all the apps you use every day include at least one label indicating the date and time somewhere. It may be the time a message was sent in your favorite messaging app, the date a photo was taken or posted; or the time it is in a different city in the world right now.
Try opening Xcode on your Mac, and creating a rather simple playground like this. You can create a value that represents the current date in Swift by just using the default empty Date() initializer of this Type. You’ll notice you get an error indicating that ‘Date’ is not available in the current scope, and that Xcode prompts you to ‘import Foundation’.

The Date type is not available in the current scope for this Playground. Xcode suggests to add an import statement including the Foundation framework.

The Date type is not available in the current scope for this Playground. Xcode suggests to add an import statement including the Foundation framework.

If you import Foundation, your playground will run normally, and you’ll get access to other functionalities that you can use with the Date type, like the different formatting options introduced in iOS 15.
Like this:

A screenshot of an Xcode playground, with an import statement including the Foundation framework, a line of code that declares a constant value that contains the current date, and a print statement with a greeting and a formatted version of that date

The formatted(date:time:) API introduced in iOS 15 allows us to work with different date formattings in a much more convenient manner.

As you can imagine, the Foundation framework provides functionality to many other Data types beyond Dates and Times you work with every day, like: Strings and Text, Numbers, Data and Basic Values, and beyond - like more complex features of the language like working with Files and Data Persistence or Networking with URL.

For this reason, any changes to this Framework are critical to understand for developers for Apple platforms.

So what is happening now?

The Foundation project has actually already existed as an open-source project for quite some time. Since 2016, the swift-corelibs-foundation project boasted a Swift layer wrapping a preexisting open-source implementation of Foundation in the C programming language.

The Swift Core Team has now announced a new open-source Foundation project. In Swift.

What does this mean?

  1. Faster. There was a cost of converting the C implementation of the Foundation framework into Swift. This framework will now be a native package in Swift, eliminating these conversion costs.

  2. More opportunities to keep binary sizes light. A rewrite comes with downfalls, but it also comes with opportunities. The Swift Core team has observed and identified the many use cases this framework can take part in, and is working to break the Framework down into different packages. The official article cites three examples

    1. For cases where small binary sizes is a must, there’s a FoundationEssentials package being developed.

    2. For cases where the application’s users locations might be very distributed around the globe, and will require strong localization features, there’s a FoundationInternationalization package*.*

    3. For developers that want to work with legacy codebases, there’s a FoundationObjCCompatibility package.

  3. One package for all platforms. This rewrite will prevent a whole series of bugs that occurred when moving code across platforms. The Foundation package will now be the core of this framework regardless of the platform.

I found two things interesting:

  1. This rewrite opens the doors to many developers who really care about Swift, and may want to submit their proposals, to contribute to the language evolution in the language they’re proficient in.

    You could argue that the language evolution proposals should always be submitted by the most efficient developers in both Swift and in C - which is already a very highly performant language; but my opinion is that, just by opening the doors to developers who use this language every day, many ideas will be brought to the table that may otherwise not have seen the light of the day. Apple always emphasizes their focus on the Developer Experience for us, developers, for their platforms, and to me, this is a clear step forward in just that same direction. Time will tell.

  2. Second, I personally care a lot about performance “for the byte”, and I wonder how much the improvements in speed I’m actually going to notice, and I’m very curious to see the different ways I can use the breakdown of the Foundation framework to achieve smaller binary sizes - as well as the new features/improvements that might be included to this framework during the rewrite.

Now, what about you? Did you find these news interesting? What’s something in particular you are looking forward to? What’s something you’ll be paying attention to?

Let me know in the comments, or you can always reach out to me on twitter or linkedIn, or, catch me on the Swift Forums

You can follow more news about the Foundation framework rewrite in Swift, in the official post.

Credits:

Photo by Joel Filipe on Unsplash

0
Subscribe to my newsletter

Read articles from Fito Toledano directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Fito Toledano
Fito Toledano

💻 Senior iOS Developer at scale-up in the Netherlands 🌱 Interested in Health tech + AI 📫 Reach out via twitter @fitotoledano 😃 Pronouns: he/him ⚡ Fun fact: I once was a semi-pro tennis player