Apple’s use of Swift in iOS 10.1 and macOS 10.12

Posted: November 1st, 2016 | Author: | Filed under: Analysis, iOS, macOS, Programming, Swift | 5 Comments »

Swift has been announced at the WWDC 2014, more than 2 years ago. Most of the sample code projects from Apple are now written in Swift. But does Apple use Swift in iOS 10.1 and macOS 10.12.1?

How to detect if a binary is using Swift?

A na├»ve approach would be to check if an app contains the Swift libraries in its Frameworks folder: libswiftCore.dylib, libswiftFoundation.dylib, …

Here is the content of the Frameworks folder of the MRT.app on macOS 10.12.1
/System/Library/CoreServices/MRT.app/Contents/Frameworks/ :


MRT.app

However this is not a good approach since iOS and macOS contain a private copy of the Swift libraries in /System/Library/PrivateFrameworks/Swift/ . Several apps in iOS and macOS link directly to these system libraries.

Here is the content of the Frameworks folder of the PIPAgent.app on macOS 10.12.1
/System/Library/CoreServices/PIPAgent.app/Contents/Frameworks/ :


PIPAgent.app

A much better approach is to check whether a binary links to a Swift library. This can easily be done with the command line tool ‘otool’ using the -L option:

-L Display the names and version numbers of the shared libraries that the object file uses, as well as the shared library ID if the file is a shared library.

When running this command on the PIPAgent application:

otool -L /System/Library/CoreServices/PIPAgent.app/Contents/MacOS/PIPAgent | grep swift

you would get the following output:

/System/Library/PrivateFrameworks/Swift/libswiftAppKit.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftCore.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftCoreData.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftDarwin.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftDispatch.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftFoundation.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftIOKit.dylib (compatibility version 1.0.0, current version 800.8.18)
/System/Library/PrivateFrameworks/Swift/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 800.8.18)

Building a script

Using the otool command line tool, it is easy to write a bash function that tells if a file is a binary linked to the Swift libraries:


#------------------------------------------------------------------------
# Function to check if a file (passed as argument $1) is using Swift
# It returns the number of occurrences of the string 'swift'
# from the output of otool
#------------------------------------------------------------------------
isFileUsingSwift ()
{
	otool -L $1 2>/dev/null | grep -o swift | wc -l
}

The processFile bash function takes a file as parameter and will print its path if it’s a binary linked to the Swift libraries:


#------------------------------------------------------------------------
# Function to process a file (passed as argument $1).
# It calls the function isFileUsingSwift() to determine
# if this is a binary using Swift and in this case
# print the path of this file.
#------------------------------------------------------------------------
processFile ()
{
	isFileUsingSwift=$( isFileUsingSwift $1 )
 	if [ ${isFileUsingSwift} != 0 ]
 	then
 		# We found a binary using Swift
	 	echo "   $1"
	fi
}


Looping through all the files of a folder is now a single line:

find ${PATH_TO_CHECK} -type f -exec bash -c 'processFile "$0"' {} \;

Final script

Below is the complete bash script that loops through all the files of a folder and print the paths of all the binaries found that use Swift.
Note: You can download the complete script here.


#!/bin/bash

#---------------------------------------------------------------------
# Bash script that loops through all the files of a folder and
# print the paths of all the binaries found that use Swift
# Created by Alexandre Colucci on 01.11.2016
# http://blog.timac.org/?p=1398
#---------------------------------------------------------------------


#---------------------------------------------------------------------
# Force expand a wildcard pattern into the list of matching pathnames
#---------------------------------------------------------------------
shopt -s nullglob

#---------------------------------------------------------------------
# Function to print the usage
#---------------------------------------------------------------------
printUsage ()
{
	echo "Usage: detectSwift.sh PATH"
	echo "PATH: Folder to search for binaries using Swift"
	echo ""
	echo "Examples:"
	echo "  detectSwift.sh /System/Library"
	echo "  detectSwift.sh /System"
	echo "  detectSwift.sh /"
	echo ""
	echo "Note: run as root in order to avoid permission issues."
	echo ""
}

#---------------------------------------------------------------------
# Function to check if a file (passed as argument $1) is using Swift
# It returns the number of occurrences of the string 'swift'
# from the output of otool
#---------------------------------------------------------------------
isFileUsingSwift ()
{
	otool -L $1 2>/dev/null | grep -o swift | wc -l
}

#---------------------------------------------------------------------
# Function to process a file (passed as argument $1).
# It calls the function isFileUsingSwift() to determine
# if this is a binary using Swift and in this case
# print the path of this file.
#---------------------------------------------------------------------
processFile ()
{
	isFileUsingSwift=$( isFileUsingSwift $1 )
 	if [ ${isFileUsingSwift} != 0 ]
 	then
 		# We found a binary using Swift
	 	echo "   $1"
	fi
}

#---------------------------------------------------------------------
# Check if the script was called with the expected usage
#---------------------------------------------------------------------
PARAMETER_NUMBER=$#
PARAMETER_REQUIRED=1
if [ $PARAMETER_NUMBER != $PARAMETER_REQUIRED ];
then
	printUsage
	exit 1
fi


#---------------------------------------------------------------------
# Get the folder path
#---------------------------------------------------------------------
PATH_TO_CHECK=$1

echo ""
echo "Start time:"
date
echo ""
echo "Apps using Swift in ${PATH_TO_CHECK}"


#---------------------------------------------------------------------
# Export the functions so that the subshell inherits them
#---------------------------------------------------------------------
export -f isFileUsingSwift
export -f processFile

#---------------------------------------------------------------------
# Find all the regular files in all subdirectories
# and call for each the function processFile()
#---------------------------------------------------------------------

find ${PATH_TO_CHECK} -type f -exec bash -c 'processFile "$0"' {} \;


#---------------------------------------------------------------------
# Finalizing
#---------------------------------------------------------------------
echo ""
echo "Completed at:"
date
echo ""

Running the script

The script is really slow: for each regular file it will create a subshell, call otool, grep and wc.
Running this script on the iOS 10.1 filesystem takes around 30 minutes.

For macOS 10.12.1, running the script on / takes dozen of hours. I recommend to only run this script on /System, /Applications and /usr. Processing these 3 folders in parallel will take around 2 hours.

Apple’s use of Swift in iOS 10.1

Running the script on iOS 10.1 (14B72c) of an iPhone 7 Plus will give you this list of binaries:

/Applications/Calculator.app/Calculator
/Applications/Music.app/Music
/Applications/Music.app/PlugIns/MusicMessagesApp.appex/MusicMessagesApp
/Applications/Music.app/PlugIns/RecentlyPlayedTodayExtension.appex/RecentlyPlayedTodayExtension
/System/Library/PrivateFrameworks/UpNextWidget.framework/PlugIns/UpNext.appex/UpNext

You will get these additional binaries from the dyld shared cache:

/System/Library/PrivateFrameworks/CoreKnowledge.framework/CoreKnowledge
/System/Library/PrivateFrameworks/Swift/libswiftAssetsLibrary.dylib
/System/Library/PrivateFrameworks/Swift/libswiftAVFoundation.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCloudKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftContacts.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCore.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreAudio.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreBluetooth.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreData.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreGraphics.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreImage.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreLocation.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreMedia.dylib
/System/Library/PrivateFrameworks/Swift/libswiftDarwin.dylib
/System/Library/PrivateFrameworks/Swift/libswiftDispatch.dylib
/System/Library/PrivateFrameworks/Swift/libswiftEventKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftFoundation.dylib
/System/Library/PrivateFrameworks/Swift/libswiftGameKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftGameplayKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftGLKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftHomeKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftLocalAuthentication.dylib
/System/Library/PrivateFrameworks/Swift/libswiftMultipeerConnectivity.dylib
/System/Library/PrivateFrameworks/Swift/libswiftObjectiveC.dylib
/System/Library/PrivateFrameworks/Swift/libswiftPassKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftRemoteMirror.dylib
/System/Library/PrivateFrameworks/Swift/libswiftSceneKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftsimd.dylib
/System/Library/PrivateFrameworks/Swift/libswiftSpriteKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftSwiftOnoneSupport.dylib
/System/Library/PrivateFrameworks/Swift/libswiftUIKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftWatchConnectivity.dylib
/System/Library/PrivateFrameworks/Swift/libswiftWatchKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftWebKit.dylib
/System/Library/PrivateFrameworks/UpNextWidget.framework/UpNextWidget

Note that you will get a similar output for an iPad except that the Calculator.app is not available.

Apple’s use of Swift in macOS 10.12.1

Running the script on macOS 10.12.1 will give you this list of binaries:

/Applications/Utilities/Console.app/Contents/MacOS/Console
/usr/bin/swift
/usr/bin/swiftc
/usr/sbin/usernoted
/System/Library/CoreServices/Dock.app/Contents/MacOS/Dock
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftAppKit.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftCore.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftCoreData.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftCoreGraphics.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftCoreImage.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftDarwin.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftDispatch.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftFoundation.dylib
/System/Library/CoreServices/MRT.app/Contents/Frameworks/libswiftObjectiveC.dylib
/System/Library/CoreServices/MRT.app/Contents/MacOS/MRT
/System/Library/CoreServices/NotificationCenter.app/Contents/MacOS/NotificationCenter
/System/Library/CoreServices/OSDUIHelper.app/Contents/MacOS/OSDUIHelper
/System/Library/CoreServices/PIPAgent.app/Contents/MacOS/PIPAgent
/System/Library/PrivateFrameworks/Swift/libswiftAppKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftAVFoundation.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCloudKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftContacts.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCore.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreAudio.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreBluetooth.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreData.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreGraphics.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreImage.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreLocation.dylib
/System/Library/PrivateFrameworks/Swift/libswiftCoreMedia.dylib
/System/Library/PrivateFrameworks/Swift/libswiftDarwin.dylib
/System/Library/PrivateFrameworks/Swift/libswiftDispatch.dylib
/System/Library/PrivateFrameworks/Swift/libswiftEventKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftFoundation.dylib
/System/Library/PrivateFrameworks/Swift/libswiftGameKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftGameplayKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftGLKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftIOKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftLocalAuthentication.dylib
/System/Library/PrivateFrameworks/Swift/libswiftMultipeerConnectivity.dylib
/System/Library/PrivateFrameworks/Swift/libswiftObjectiveC.dylib
/System/Library/PrivateFrameworks/Swift/libswiftOpenCL.dylib
/System/Library/PrivateFrameworks/Swift/libswiftRemoteMirror.dylib
/System/Library/PrivateFrameworks/Swift/libswiftSceneKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftsimd.dylib
/System/Library/PrivateFrameworks/Swift/libswiftSpriteKit.dylib
/System/Library/PrivateFrameworks/Swift/libswiftSwiftOnoneSupport.dylib
/System/Library/PrivateFrameworks/Swift/libswiftWebKit.dylib

Note that you will get a lot of matches in Xcode 8.1. If you exclude the various toolchains and platform SDKs, you will get:

/Applications/Xcode.app/Contents/Frameworks/IDEDocumentation.framework/Versions/A/IDEDocumentation
/Applications/Xcode.app/Contents/Frameworks/libswiftAppKit.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftAVFoundation.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftCore.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftCoreAudio.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftCoreData.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftCoreGraphics.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftCoreImage.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftCoreMedia.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftDarwin.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftDispatch.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftFoundation.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftIOKit.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftObjectiveC.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftQuartzCore.dylib
/Applications/Xcode.app/Contents/Frameworks/libswiftXPC.dylib
/Applications/Xcode.app/Contents/PlugIns/IDEDocViewer.ideplugin/Contents/MacOS/IDEDocViewer
/Applications/Xcode.app/Contents/PlugIns/IDELanguageSupportUI.ideplugin/Contents/MacOS/IDELanguageSupportUI
/Applications/Xcode.app/Contents/PlugIns/IDEQuickHelp.ideplugin/Contents/MacOS/IDEQuickHelp
/Applications/Xcode.app/Contents/PlugIns/XcodeBuiltInExtensions.appex/Contents/MacOS/XcodeBuiltInExtensions
/Applications/Xcode.app/Contents/SharedFrameworks/DNTDocumentationModel.framework/Versions/A/DNTDocumentationModel
/Applications/Xcode.app/Contents/SharedFrameworks/DNTDocumentationSupport.framework/Versions/A/DNTDocumentationSupport
/Applications/Xcode.app/Contents/SharedFrameworks/DNTSourceKitSupport.framework/Versions/A/DNTSourceKitSupport
/Applications/Xcode.app/Contents/SharedFrameworks/DNTTransformer.framework/Versions/A/DNTTransformer
/Applications/Xcode.app/Contents/SharedFrameworks/DVTDocumentation.framework/Versions/A/DVTDocumentation
/Applications/Xcode.app/Contents/SharedFrameworks/DVTMarkup.framework/Versions/A/DVTMarkup
/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/Resources/repl_swift
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/Frameworks/libswiftCore.dylib
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/Frameworks/libswiftCoreGraphics.dylib
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/Frameworks/libswiftDarwin.dylib
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/Frameworks/libswiftDispatch.dylib
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/Frameworks/libswiftFoundation.dylib
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/Frameworks/libswiftIOKit.dylib
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/Frameworks/libswiftObjectiveC.dylib
/Applications/Xcode.app/Contents/SharedFrameworks/SourceKit.framework/Versions/A/SourceKit

Conclusion

Apple’s use of Swift in iOS 10.1 and macOS 10.12.1 is extremely limited.
On iOS 10.1 there are only 2 apps and 2 private frameworks using Swift:

  • Calculator.app (iPhone only)
  • Music.app
  • UpNextWidget.framework
  • CoreKnowledge.framework

On macOS 10.12.1 the list of apps using Swift is limited to:

  • Console
  • swift
  • swiftc
  • usernoted
  • Dock.app
  • MRT.app
  • NotificationCenter.app
  • OSDUIHelper.app
  • PIPAgent.app
  • (Xcode.app)