Swift Language

Topics related to Swift Language:

Getting started with Swift Language

Swift logo

Swift is an application and systems programming language developed by Apple and distributed as open source. Swift interoperates with Objective-C and Cocoa/Cocoa touch APIs for Apple's macOS, iOS, tvOS, and watchOS operating systems. Swift currently supports macOS and Linux. Community efforts are in progress to support Android, Windows, and other platforms.

Swift development happens on GitHub; contributions are usually submitted via pull requests.

Bugs and other issues are tracked at bugs.swift.org.

Discussions about Swift development, evolution, and usage are held on the Swift mailing lists.

Other Resources

Switch

Supply a case for every possible value of your input. Use a default case to cover remaining input values you don't want to specify. The default case must be the last case.

By default Switches in Swift will not continue to check other cases after a case has been matched.

Reading & Writing JSON

Enums

Protocols

Optionals

Structs

Closures

Error Handling

Arrays

Arrays are an ordered collection of values. Values may repeat but must be of the same type.

Dictionaries

Some examples in this topic might have a different order when used because dictionary order is not guaranteed.

Strings and Characters

Extensions

Sets

Working with C and Objective-C

Functions

Numbers

Classes

Conditionals

Variables & Properties

Tuples

Booleans

Memory Management

When to use the weak-keyword:

The weak-keyword should be used, if a referenced object may be deallocated during the lifetime of the object holding the reference.

When to use the unowned-keyword:

The unowned-keyword should be used, if a referenced object is not expected to be deallocated during the lifetime of the object holding the reference.

Pitfalls

A frequent error is to forget to create references to objects, which are required to live on after a function ends, like location managers, motion managers, etc.

Example:

class A : CLLocationManagerDelegate
{
    init()
    {
        let locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.startLocationUpdates()
    }
}

This example will not work properly, as the location manager is deallocated after the initializer returns. The proper solution is to create a strong reference as an instance variable:

class A : CLLocationManagerDelegate
{
    let locationManager:CLLocationManager

    init()
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.startLocationUpdates()
    }
}

Generics

Advanced Operators

Access Control

  1. Basic Remark:

Below are the three access levels from highest access (least-restrictive) to lowest access (most-restrictive)

Public access allows access to classes, structs, variables, etc from any file within the model, but more importantly outside the module if the external file imports the module containing the public access code. It is popular to use public access when you create a framework.

Internal access allows files only with the module of the entities to use the entities. All entities have internal acccess level by default (with a few exceptions).

Private access prevents the entity from being used outside of that file.

  1. Subclassing Remark:

A subclass cannot have a higher access than its superclass.

  1. Getter & Setter Remark:

If property’s setter is private the getter is internal (which is the default). Also you can assign access level for both the getter and the setter. These principles also apply to subscripts as well

  1. General Remark:

Other entity types include: Initializers, Protocols, Extensions, Generics, and Type Aliases

Associated Objects

Loops

Reflection

  1. General Remarks:

A Mirror is a struct used in the introspection of an object in Swift. Its most prominent property is the children array. One possible use case is to serialize a struct for Core Data. This is done by converting a struct into a NSManagedObject.

  1. Basic Usage for Mirror Remarks:

The children property of a Mirror is an array of child objects from the object the Mirror instance is reflecting. A child object has two properties label and value. For example a child might be a property with the name title and the value of Game of Thrones: A Song of Ice and Fire.

OptionSet

Method Swizzling

When using method swizzling in Swift there are two requirements that your classes/methods must comply with:

  • Your class must extend NSObject
  • The functions you want to swizzle must have the dynamic attribute

For a complete explanation of why this is required, check out Using Swift with Cocoa and Objective-C:

Requiring Dynamic Dispatch

While the @objc attribute exposes your Swift API to the Objective-C runtime, it does not guarantee dynamic dispatch of a property, method, subscript, or initializer. The Swift compiler may still devirtualize or inline member access to optimize the performance of your code, bypassing the Objective-C runtime. When you mark a member declaration with the dynamic modifier, access to that member is always dynamically dispatched. Because declarations marked with the dynamic modifier are dispatched using the Objective-C runtime, they’re implicitly marked with the @objc attribute.

Requiring dynamic dispatch is rarely necessary. However, you must use the dynamic modifier when you know that the implementation of an API is replaced at runtime. For example, you can use the method_exchangeImplementations function in the Objective-C runtime to swap out the implementation of a method while an app is running. If the Swift compiler inlined the implementation of the method or devirtualized access to it, the new implementation would not be used.

Links

Objective-C Runtime Reference

Method Swizzling on NSHipster

Concurrency

Initializers

Getting Started with Protocol Oriented Programming

Functional Programming in Swift

Style Conventions

Type Casting

Logging in Swift

Performance

RxSwift

The Defer Statement

Design Patterns - Creational

Swift Package Manager

NSRegularExpression in Swift

Special Characters

   *?+[(){}^$|\./

Documentation markup

AES encryption

PBKDF2 Key Derivation

Typealias

Cryptographic Hashing

Dependency Injection

Function as first class citizens in Swift

Blocks

Caching on disk space

Algorithms with Swift

(Unsafe) Buffer Pointers

Swift Advance functions

Completion Handler

Design Patterns - Structural

Swift HTTP server by Kitura

Generate UIImage of Initials from String