Apple's Swift programming language: Cheat sheet

1 month ago 14
PR Distribution

Apple's Swift has far-reaching effects connected each platforms, not conscionable iOS, OS X, watchOS and tvOS. Learn wherefore Swift matters, however to usage the programming connection and however it differs from Objective-C.

Objective-C has won the hearts of galore developers. It started arsenic a programming connection that bundled object-oriented programming (OOP) with the likeness of the C programming language. Objective-C called NeXT and Apple location wherever it was the default programming connection for NeXTSTEP, OS X and iOS.

In 2010, Apple started processing Swift, a caller programming connection that would rival Objective-C successful a fewer areas--specifically, benignant safety, security, and amended on-hardware performance. Swift is much than 2.6x faster than Objective-C and much than 8.4x faster than Python. Swift 1.0 was released successful September 2014.

SEE: Apple's Swift programming language: Cheat expanse (free PDF) (TechRepublic)

Executive summary

  • What is Apple's Swift? Swift is the newest programming connection from Apple; it offers amended benignant safety, information and performance. Swift is disposable for processing iOS, OS X, watchOS and tvOS apps.
  • Why does Apple's Swift matter? It's an casual programming connection to learn, which is wherefore galore students, entry-level developers and long-standing Mac and iOS developers are focusing their improvement skills connected Swift. In addition, Apple unfastened sourced Swift, making it disposable for processing connected different platforms, not conscionable those designed by Apple.
  • Who does Apple's Swift affect? Apps built with Swift tin beryllium tally connected iOS devices dating backmost to iOS 7 oregon later, and OS X devices dating backmost to OS X 10.9 oregon later.
  • When is Apple's Swift available? Swift is readily disposable successful the astir caller mentation of Xcode.
  • How tin you get Apple's Swift? Get the latest mentation of Swift by downloading Xcode from the Mac App Store oregon the Apple Developer Center. Once Xcode is installed, Swift and the Objective-C compiler (LLVM) volition beryllium installed connected your Mac.

SEE: 31 Mac keyboard shortcuts concern users request to cognize (free PDF) (TechRepublic)

How does Swift disagree from Objective-C?

While Apple hasn't announced plans to sunset the Objective-C connection (in fact, Apple is inactive updating it), Swift is rapidly overtaking Objective-C to go much fashionable connected Apple's platforms.

Objective-C apt inactive has a agelong life, arsenic Apple has yet to update its ain Frameworks to beryllium written successful Swift. Until Swift 3.0, Apple volition not beryllium including the Swift runtime connected iOS oregon OS X, leaving Xcode to bundle unneurotic the runtime into the iOS oregon OS X app to guarantee binary compatibility.

Swift does bring a batch of modern programming niceties to Apple's platforms. Here's a look astatine the main differences betwixt Objective-C and Swift.

Header files

Objective-C was based connected C, but added OOP paradigms to the connection done entity messaging. Because of that, Objective-C made usage of header files that publically declared its functions and definitions.

Swift gets escaped of this practice, allowing developers to constitute a azygous .swift record that contains emblematic header information, properties and each of the people defines into a azygous file. Say goodbye to header files, and hullo to cleaner and leaner code.

SEE: All of TechRepublic's cheat sheets and astute person's guides

REPL

Objective-C and Swift are compiled languages, contempt Swift having a REPL (Read-Eval-Print Loop) for investigating that typically lone supports interpreted languages.

The REPL is disposable successful the bid enactment and Xcode. Inside of Xcode, it is known arsenic Playgrounds, allowing developers to constitute Swift code, and person it evaluated immediately, printing retired the results successful the sidebar (Figure A). To make a caller Swift Playground successful Xcode, unfastened Xcode and prime File | New | Playground. A caller model volition appear, and you tin participate Swift codification to trial things.

Figure A

swiftfigurea.png

Playgrounds are a spot to measure and trial codification without creating an full iOS oregon OS X task first.

Image: Cory Bohon/TechRepublic

Type inference

With Objective-C and Swift, entity types are powerfully typed—meaning, the compiler indispensable cognize precisely what benignant of entity (string, array, dictionary, customized object, etc.) needs to beryllium stored successful representation for a peculiar variable. With Swift, Apple decided to support beardown types, but let the compiler to automatically infer the benignant based connected the objects assigned to a variable.

This means you tin constitute codification similar the pursuing snippet, and Swift volition spot that myVariable is simply a String type, due to the fact that it has a String assigned to it.

let myVariable = "some drawstring here"

Developers tin inactive execute beardown typing for clarity by doing this, but it is unnecessary. However, it does adhd clarity, particularly to developers that whitethorn beryllium touching the codebase aft you've worked connected it.

let myVariable: String = "some drawstring here"

Available platforms

When Apple announced Swift at WWDC 2014, the institution reported that league attendees were utilizing 1 of the archetypal publically released Swift apps: the WWDC app. It was developed partially successful Swift, and allowed attendees to presumption league schedules, maps and more.

Officially, iOS 7 and higher enactment Swift; OS X 10.9 (Mavericks) and higher enactment it arsenic well. All versions of watchOS and tvOS person enactment for moving Swift-built apps.

How tin I larn and usage Swift?

You'll request a Mac, and if you volition beryllium processing for iOS, watchOS oregon tvOS, you'll request 1 of those devices arsenic well.

After you download and instal Xcode, the Objective-C and Swift compiler (LLVM) is installed connected your Mac. At this point, opening Xcode volition contiguous options for opening projects successful either Swift oregon Objective-C.

The archetypal spot to commencement is with Playgrounds and the Swift REPL, wherever you tin larn by playing with Swift's features successful an situation that won't messiness up immoderate in-flight apps, and without the request to make immoderate afloat investigating iOS oregon OS X project.

SEE: How to make Apple apps and widgets: Go from novice to adept with this training (TechRepublic Academy)

Be definite to instrumentality vantage of Apple's large Swift resources, which see these books astir Swift that are disposable successful the Apple Books app: 

What does Swift mean for enterprises and developers?

Swift is an unfastened root connection that is being adopted astatine a precise accelerated pace. It allows developers to prototype and constitute iOS, OS X and different Apple level apps faster and with less bugs and crashes than ever before.

This is important for enterprises, particularly businesses that person made investments successful Apple's platforms, and investments successful processing their ain B2B/enterprise apps, due to the fact that keeping those apps up to day has ne'er been easier.

In addition, Swift being unfastened sourced means that the connection volition beryllium expanded to different platforms, beyond conscionable what Apple has created. We are already seeing Swift being ported to Linux and different platforms.

One imaginable country wherever Swift volition flourish is web apps. Swift being ported to Linux means that Swift apps tin present tally connected low-cost, low-maintenance Linux servers that are already the cornerstone of existing web APIs and services. There are already frameworks for Swift that marque it truthful Linux-based server apps tin beryllium built wrong of Xcode, allowing businesses to utilize existing Swift developers to physique the APIs and services that iOS and OS X apps often consume.

Additional resources

What's caller successful Swift 2.0?

Apple released Swift 2.0 astatine WWDC '15, and it added galore features and refinements. These are immoderate of the caller features successful Swift 2.0.

  • Swift is unfastened root with a caller Linux larboard available.
  • A caller mistake handling exemplary utilizing try, throw, and drawback keywords.
  • It targets older versions of iOS and OS X. It's besides safer, with the #available artifact that lets you wrapper lines of codification that volition beryllium executed connected systems wherever the model is available.
  • SDKs are present Swift-ier, acknowledgment to Apple adding generics and nullability to existing Objective-C frameworks to marque them interface amended with Swift code.

What's caller successful Swift 3.0?

Apple released Swift 3.0 astatine WWDC '16; it was the archetypal large merchandise of Swift since Apple unfastened sourced the connection successful December 2015. The caller features successful Swift 3.0 include:

  • Refinements to the halfway connection and modular room to escaped the connection of NS prefixes and different Objective-C holdovers.
  • Major additions to the Linux larboard of Swift.
  • The summation of the Swift Package Manager to marque it easier to negociate dependencies.

What's caller successful Swift 4.0?

Like with different Swift releases, 4.0 was announced astatine WWDC '17 and released successful beta signifier to developers alongside Xcode 9. This merchandise of Swift added rather a fewer refinements and features to the programming language, including:

  • New Codeable protocol that allows for casual serialization of information wrapped successful structs.
  • String literals tin present interruption into aggregate lines utilizing the caller """ declaration (three sets of punctuation marks) to unfastened and adjacent the multiline string.
  • Strings received a large overhaul, making them collections of characters.

What's caller successful Swift 5.0?

Swift 5.0 ushered successful much improvements for the astir fashionable connection for iOS and Mac improvement by adding galore modular room additions similar show improvements and more.

The biggest diagnostic of Swift 5.0 was the declaration of ABI Stability and Binary Compatibility; some of these mean that the Swift modular libraries are incorporated successful the releases of macOS, iOS, tvOS and watchOS going forward. Those libraries nary longer request to beryllium included successful each app's binary, and the app bundle tin beryllium overmuch smaller.

What's caller successful Swift 5.3?

Swift 5.3 was released with Xcode 12 connected September 16, 2020. This merchandise of Swift chiefly focused connected refinements to the connection itself and expanding the Swift ecosystem. 

The Swift radical astatine Apple took the accidental to optimize runtime show by reducing the codesize for Swift apps (especially for SwiftUI apps), improving the representation optimizations and reducing the heap representation utilization. 

On the developer broadside of things, Apple focused connected making errors much precise and much actionable with tips successful the compiler suggestions, codification completion was improved much than 5x, and there's present amended runtime mistake messages, making it easier to fig retired issues erstwhile you brushwood them during debugging.

Apple besides expanded its level support. Swift is present supported connected Ubuntu, CentOS 8, Amazon Linux Z, Windows (with Swift 5.3 only), and AWS Lambda runtime environment.

In addition, Apple took into relationship galore Swift Evolution suggestions from the community. 

What's caller successful Swift 5.4?

Swift 5.4 was released connected April 26, 2021 to each users of Xcode 12.5. On the improvements database determination were galore niceties, including compiler improvements, improved codification completion erstwhile typing expressions and immense improvements for incremental compiles. 

On the coding side, determination are galore additions from the Swift Evolution community, including: 

  • Improved Implicit Member Syntax (SE-0287).

  • Multiple variadic parameters successful relation calls (SE-0284).

  • Result builders (SE-0289).

  • Local functions enactment overloading (SE-10069).

In addition, Swift 5.4 introduces the quality to sanction a adaptable the aforesaid arsenic a function, and spot wrappers are present supported for section variables. 

Swift Package Manager improvements tin besides beryllium recovered successful Swift 5.4 and Xcode 12.5, including the quality to state packages executable targets (SE-0294).

What's caller successful Swift 5.5?

Swift 5.5 was released successful beta signifier with Xcode 13 connected June 7, 2021 astatine WWDC 2021. The main improvements with this merchandise halfway astir concurrency with the instauration of the async/await mechanics of performing tasks that indispensable execute enactment and hold for thing to implicit earlier continuing. Learn much astir concurrency successful Swift 5.5 by watching the WWDC 2021 league entitled "Explore structured concurrency successful Swift" connected the Apple Developer website, oregon by speechmaking the Swift Concurrency documentation.

What is Swift Playgrounds for iPad?

At WWDC '16, Apple announced the summation of Swift Playgrounds, an app that lets developers and opening coders programme utilizing the Swift connection connected the iPad successful a Playground situation that was pioneered connected the Mac with Xcode 6.

At WWDC '17, Apple announced 2 caller versions of Swift Playgrounds. Swift Playgrounds 1.5 is disposable instantly and provides the quality to interface with Bluetooth-connected devices similar drones, Sphero and LEGO toys to bring real-world programming for students and developers.

Also announced astatine WWDC '17 was Swift Playgrounds 2.0. This caller mentation was made disposable arsenic a beta to developers astatine the league and features integrated API documentation tools, Swift 4.0 and Swift 3.2 support, enactment for the iOS 11 SDK, and enactment for utilizing Camera and Augmented Reality APIs.

Swift Playgrounds 3.0 was released successful May 2019, and was a important update to the Swift Playgrounds app that has received stableness updates and improvements—version 3.3.1 is present available. Version 3.0 is important due to the fact that it added the quality to import your ain Swift files into a playground, and stock Swift files amongst your playgrounds. It besides includes coding mistake suggestions, acheronian mode support, further tools for educators to physique retired their ain Swift playground books and Swift mentation updates truthful the latest Swift codification tin beryllium built wrong of the Swift Playgrounds app. 

apple-swift.jpg

The caller Swift Playgrounds app showing a afloat Swift / SwiftUI exertion being built and developed connected the iPad.

Image: Apple, Inc.

With the instauration of Swift Playgrounds 4 astatine WWDC '21, Apple added the quality to make iOS apps wrong the Swift Playgrounds exertion connected iPadOS 15. Doing this requires apps to beryllium built with SwiftUI for the idiosyncratic interfaces and Swift for the programming connection (you cannot usage Objective-C oregon Interface Builder files successful the physique process). Apps built with the Swift Playgrounds app tin beryllium tested close connected the iPadOS instrumentality being built with, and tin adjacent beryllium shipped to the App Store. This caller mentation of Swift Playgrounds for iPadOS volition beryllium disposable to iPad owners opening this autumn successful the App Store. 

Swift Playgrounds is simply a escaped app that tin beryllium downloaded for iPad from the App Store.

Additional resources

What is SwiftUI?

At WWDC successful June 2019, Apple released SwiftUI, which is simply a caller mode to make UIs for Swift apps. This acceptable of tools and APIs let developers to constitute declarative Swift syntax to specify and contiguous a idiosyncratic interface, getting galore caller features that travel with iOS 13 for free, including:

  • Dynamic Type.
  • Dark Mode.
  • Localizations for communal items.
  • iOS accessibility features.

SwiftUI allows you to authorities what the interface should do, and iOS takes power successful presenting that interface to your users. You tin make a database of items, and past picture the alignment, font and colour for each point each successful codification without having to utilize Interface Builder.

Using the caller Design Tools successful Xcode, you tin spot what the codification is presenting to the user. The unrecorded preview model volition automatically sync with the codification successful the side-by-side exertion to preview arsenic you type.

SwiftUI is autochthonal connected each Apple platforms opening with iOS 13 and newer, including iPhone, Mac, iPad, Apple Watch and Apple TV. Best of all, a UI that you make for 1 instrumentality volition automatically beryllium translated (or tin beryllium translated with small work) to each platforms.

SwiftUI is besides the mode you tin easy larboard your iOS apps to Mac by adding the Mac arsenic a deployment enactment successful your iOS task settings. This caller model volition yet pb to specified accessible, much maintainable, much portable apps for Apple's platforms.

For much accusation connected implementing SwiftUI successful your applications, cheque retired the pursuing WWDC 2019 league videos:

At WWDC 2020, Apple unveiled a caller mentation of SwiftUI that added adjacent much features for gathering full iOS, macOS, tvOS, and watchOS apps utilizing SwiftUI. They besides took a leap guardant for SwiftUI, making it the default mode to physique widgets for iOS 14. New features similar outlines, grids and toolbars marque SwiftUI adjacent much almighty and susceptible than before. 

WWDC '21 brought astir a fewer caller SwiftUI changes, including expanding compatibility with macOS to marque much Mac developers power to the caller UI layout strategy that SwiftUI brings about. Many of the changes trust connected bringing much modular components from the Mac interface implicit to SwiftUI, including the multi-column view, and hunt views for macOS and iOS and a caller low-level graphics layout presumption compatible with SwiftUI to regenerate the aging drawRect from Swift and Objective-C.

Swift 5.5 and SwiftUI bring astir different niceties into the language. One specified caller diagnostic is called AsyncImage, which is simply a caller presumption for remotely loading images. Other niceties see the quality to adhd swipe actions for database rows, propulsion to refresh and tidying up the SwiftUI API for commonly utilized items.

Beyond the league videos above, which are inactive relevant, Apple besides released caller league videos astatine WWDC 2021 that explains each of the caller SwiftUI features: 

What is Swift Package Manager?

In caller years, the Swift Package Manager has picked up popularity amongst iOS and macOS developers arsenic different unfastened root bundle managers person waned successful the community. The Swift Package Manager, often abbreviated SPM, adds the quality starting successful Swift 3.0 and Xcode 8 to bundle unneurotic a babelike Swift task oregon root files into a module that tin easy beryllium added and versioned by Git, past downloaded, built and embedded into an iOS, macOS, watchOS oregon tvOS task by Xcode.

This bundle manager is built into Xcode and adds an casual mode to abstracted retired reusable codification that tin beryllium shared betwixt aggregate projects oregon aggregate owners—in the lawsuit of unfastened root oregon cross-company sharing of projects. 

Building a module with SPM is casual and tin beryllium done with a fewer lines of codification and git repository. Learn astir creating and utilizing a Swift Package successful Xcode here, oregon larn much astir how the strategy works connected the Swift.org blog.

What is Swift's mentation history?

  • Mid 2010: Development begins.
  • July 17, 2010: First Swift commit to the Swift GitHub repository.
  • June 2, 2014: Apple announces Swift astatine WWDC 2014, giving developers a pre-release mentation of Swift and Xcode 6.
  • September 15, 2014: Apple releases Swift 1.0 with the Gold Master of Xcode 6.
  • October 15, 2014: Swift 1.1 is released with Xcode 6.1.
  • April 8, 2015: Swift 1.2 is released with Xcode 6.3.
  • June 8, 2015: Apple announces Swift 2.0 astatine WWDC 2015, giving developers a pre-release mentation of Swift 2 and Xcode 7.
  • September 15, 2015: Apple releases Swift 2.0 with the Xcode 7 Gold Master build.
  • October 20, 2015: Apple releases Swift 2.1 with the merchandise of Xcode 7.1.
  • December 3, 2015: Apple announces the Swift 3.0 roadmap connected GitHub.
  • March 21, 2016: Apple releases Swift 2.2 with the merchandise of Xcode 7.3.
  • September 13, 2016: Apple releases Swift 3.0 with the merchandise of Xcode 8.
  • March 27, 2017: Apple releases Swift 3.1.
  • June 5, 2017: Apple announces Swift 4.0 astatine WWDC '17.
  • March 29, 2018: Apple releases Swift 4.
  • September 17, 2018: Apple releases Swift 4.2.
  • March 25, 2019: Apple releases Swift 5.0.
  • September 20, 2019: Apple releases Swift 5.1 with Xcode 11.
  • March 24, 2020: Apple releases Swift 5.2.
  • June 2020: Apple announces Swift 5.3 astatine WWDC '20.
  • September 16, 2020: Apple releases Swift 5.3 with Xcode 12.
  • November 13, 2020: Apple releases Swift 5.3.1 with Xcode 12.1.

  • December 15, 2020: Apple releases Swift 5.3.2 with Xcode 12.2.

  • January 25, 2021: Apple releases Swift 5.3.3 with Xcode 12.3.

  • April 26, 2021: Apple releases Swift 5.4 with Xcode 12.5.

  • May 25, 2021: Swift 5.4.1 released. 
  • November 13, 2020: Apple releases Swift 5.3.1 with Xcode 12.1.
  • December 15, 2020: Apple releases Swift 5.3.2 with Xcode 12.2.
  • January 25, 2021: Apple releases Swift 5.3.3 with Xcode 12.3.
  • April 26, 2021: Apple releases Swift 5.4 with Xcode 12.5.
  • May 25, 2021: Swift 5.4.1 released. 
  • June 7, 2021: Apple reveals Swift 5.5 and SwiftUI 3, released with Xcode 13 beta.
  • June 28, 2021: Apple releases Swift 5.4.2.

  • September 9, 2021: Swift 5.4.3 is released.

  • September 20, 2021: Apple releases Xcode 13 with the Swift 5.5 programming language, including the overmuch awaited (no pun intended) async/await functionality, and caller SwiftUI features announced astatine WWDC '21.

Additional resources

Developer Essentials Newsletter

From the hottest programming languages to the jobs with the highest salaries, get the developer quality and tips you request to know. Weekly

Sign up today

Editor's note: This nonfiction has been updated to bespeak the latest merchandise of Swift.

Read Entire Article