🌐 Public

The Swift Programming Language

By William Bowers

Introduction

Read Apple's official guide to learning the Swift programming language.

William Bowers — I'm an engineer at Coursera and I love to learn.

1 About Swift

Important This is a preliminary document for an API or technology in development. Apple is supplying this information to help you plan for the adoption of the technologies and programming interfaces described herein for use on Apple-branded products. This information is subject to change, and software implemented according to this document should be tested with final operating system software and

3 The Basics

The BasicsSwift is a new programming language for iOS and OS X app development. Nonetheless, many parts of Swift will be familiar from your experience of developing in C and Objective-C. Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers, Double and Float for floating-point values, Bool for Boolean values, and String for

10 Enumerations

EnumerationsAn enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code. If you are familiar with C, you will know that C enumerations assign related names to a set of integer values. Enumerations in Swift are much more flexible, and do not have to

11 Classes and Structures

Classes and StructuresClasses and structures are general-purpose, flexible constructs that become the building blocks of your program's code. You define properties and methods to add functionality to your classes and structures by using exactly the same syntax as for constants, variables, and functions. Unlike other programming languages, Swift does not require you to create separate interface and implementation files for

12 Properties

PropertiesProperties associate values with a particular class, structure, or enumeration. Stored properties store constant and variable values as part of an instance, whereas computed properties calculate (rather than store) a value. Computed properties are provided by classes, structures, and enumerations. Stored properties are provided only by classes and structures. Stored and computed properties are usually associated with instances of a

13 Methods

MethodsMethods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C. The

15 Inheritance

InheritanceA class can inherit methods, properties, and other characteristics from another class. When one class inherits from another, the inheriting class is known as a subclass, and the class it inherits from is known as its superclass. Inheritance is a fundamental behavior that differentiates classes from other types in Swift. Classes in Swift can call and access methods, properties, and

17 Deinitialization

How Deinitialization WorksSwift automatically deallocates your instances when they are no longer needed, to free up resources. Swift handles the memory management of instances through automatic reference counting ( ARC), as described in Automatic Reference Counting. Typically you don't need to perform manual clean-up when your instances are deallocated. However, when you are working with your own resources, you might

18 Automatic Reference Counting

Automatic Reference CountingSwift uses Automatic Reference Counting (ARC) to track and manage your app's memory usage. In most cases, this means that memory management "just works" in Swift, and you do not need to think about memory management yourself. ARC automatically frees up the memory used by class instances when those instances are no longer needed. However, in a few

19 Optional Chaining

Optional ChainingOptional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil. If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil, the property, method, or subscript call returns nil. Multiple queries can be chained together, and the entire chain fails gracefully

20 Type Casting

Type CastingType casting is a way to check the type of an instance, and/or to treat that instance as if it is a different superclass or subclass from somewhere else in its own class hierarchy. Type casting in Swift is implemented with the is and as operators. These two operators provide a simple and expressive way to check the type

21 Nested Types

Nested TypesEnumerations are often created to support a specific class or structure's functionality. Similarly, it can be convenient to define utility classes and structures purely for use within the context of a more complex type. To accomplish this, Swift enables you to define nested types, whereby you nest supporting enumerations, classes, and structures within the definition of the type they

22 Extensions

ExtensionsExtensions add new functionality to an existing class, structure, or enumeration type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.) Extensions in Swift can: Add computed properties and computed

26 About the Language Reference

About the Language ReferenceThis part of the book describes the formal grammar of the Swift programming language. The grammar described here is intended to help you understand the language in more detail, rather than to allow you to directly implement a parser or compiler. The Swift language is relatively small, because many common types, functions, and operators that appear virtually

27 Lexical Structure

Lexical StructureThe lexical structure of Swift describes what sequence of characters form valid tokens of the language. These valid tokens form the lowest-level building blocks of the language and are used to describe the rest of the language in subsequent chapters. A token consists of an identifier, keyword, punctuation, literal, or operator. In most cases, tokens are generated from the

28 Types

TypesIn Swift, there are two kinds of types: named types and compound types. A named type is a type that can be given a particular name when it is defined. Named types include classes, structures, enumerations, and protocols. For example, instances of a user-defined class named MyClass have the type MyClass. In addition to user-defined named types, the Swift standard

31 Declarations

DeclarationsA declaration introduces a new name or construct into your program. For example, you use declarations to introduce functions and methods, variables and constants, and to define new, named enumeration, structure, class, and protocol types. You can also use a declaration to extend the behavior of an existing named type and to import symbols into your program that are declared

32 Attributes

AttributesAttributes provide more information about a declaration or type. There are two kinds of attributes in Swift, those that apply to declarations and those that apply to types. You specify an attribute by writing the @ symbol followed by the attribute's name and any arguments that the attribute accepts: Some declaration attributes accept arguments that specify more information about the

33 Patterns

PatternsA pattern represents the structure of a single value or a composite value. For example, the structure of a tuple (1, 2) is a comma-separated list of two elements. Because patterns represent the structure of a value rather than any one particular value, you can match them with a variety of values. For instance, the pattern (x, y) matches the

34 Generic Parameters and Arguments

Generic Parameters and ArgumentsThis chapter describes parameters and arguments for generic types, functions, and initializers. When you declare a generic type, function, or initializer, you specify the type parameters that the generic type, function, or initializer can work with. These type parameters act as placeholders that are replaced by actual concrete type arguments when an instance of a generic type

kuldeep Singh

Freshman

  • 0 Playlists
  • 0 Followers

Haris Hamdani

a daydreamer. he thinks that someday he'll visit north pole, who knows!

  • 0 Playlists
  • 0 Followers

Eng Soly

أنتِ جميلةٌ كوطنٍ محرر ... وأنا مُتعبٌ كوطنٍ محتل

  • 0 Playlists
  • 0 Followers

matzyc

Freshman

  • 0 Playlists
  • 0 Followers

Dante Greyson

Freshman

  • 2 Playlists
  • 1 Followers

Jonathan Arnold

Interaction Designer at @pixoona, likes taking pictures & loves Jesus.

  • 0 Playlists
  • 0 Followers

Jason Robles

Product Designer creating digital experiences for Hook & Loop based in New York.

  • 0 Playlists
  • 0 Followers

J. De Leon

A creative digital entrepreneur with a passion for building creative and useful digital products.

  • 0 Playlists
  • 0 Followers

Ashley

“I am still so naïve; I know pretty much what I like and dislike; but ...

  • 0 Playlists
  • 0 Followers

Julie Bellefontaine

Freshman

  • 0 Playlists
  • 0 Followers

Shivam Bhalla

Freshman

  • 0 Playlists
  • 0 Followers

Flora Edwards

Freshman

  • 0 Playlists
  • 0 Followers

Michele Cipriani

Freshman

  • 0 Playlists
  • 0 Followers

Erkan Keskin

Freshman

  • 0 Playlists
  • 0 Followers

bigredjoe

Freshman

  • 0 Playlists
  • 0 Followers

abdallah alshaer

big ambition and crazy dream

  • 0 Playlists
  • 0 Followers

Jeffrey Berry

Foodie, college student and data science noob

  • 0 Playlists
  • 0 Followers

Mallory Lee

Freshman

  • 0 Playlists
  • 0 Followers

Chris Aho

Ocean advocate, scuba diver, shark lover, photographer

  • 0 Playlists
  • 0 Followers

qndreq

Freshman

  • 0 Playlists
  • 0 Followers
Load more

Delete your comment

Are you sure you want to delete your comment?