Inhalt

Titelei

Impressum

Inhalt

Widmung

1 Die Programmiersprache Swift

1.1 Die Geschichte von Swift

1.2 Swift-Updates

1.3 Voraussetzungen zur Nutzung von Swift

1.4 Installation von Swift

1.4.1 macOS

1.4.2 Linux

1.5 Xcode

1.5.1 Erstellen von Dateien und Projekten

1.5.2 Der Aufbau von Xcode

1.5.3 Einstellungen

1.6 Playgrounds

1.6.1 Erstellen eines Playgrounds

1.6.2 Aufbau eines Playgrounds

1.6.3 Pages, Sources und Resources

1.6.4 Playground-Formatierungen

1.6.5 Swift Playgrounds-App für das iPad

1.7 Weitere Code-Editoren zur Arbeit mit Swift

1.7.1 Visual Studio Code

1.7.2 Syntra Small

1.7.3 IBM Swift Sandbox

1.8 Swift-Ressourcen und weiterführende Informationen

2 Grundlagen der Programmierung

2.1 Grundlegendes

2.1.1 Swift Standard Library

2.1.2 print

2.1.3 Befehle und Semikolons

2.1.4 Operatoren

2.2 Variablen und Konstanten

2.2.1 Erstellen von Variablen und Konstanten

2.2.2 Variablen und Konstanten in der Konsole ausgeben

2.2.3 Type Annotation und Type Inference

2.2.4 Gleichzeitiges Erstellen und Deklarieren mehrerer Variablen und Konstanten

2.2.5 Namensrichtlinien

2.3 Kommentare

3 Schleifen und Abfragen

3.1 Schleifen

3.1.1 for-in

3.1.2 while

3.1.3 repeat-while

3.2 Abfragen

3.2.1 if

3.2.2 switch

3.2.3 guard

3.3 Control Transfer Statements

3.3.1 Anstoßen eines neuen Schleifendurchlaufs mit continue

3.3.2 Verlassen der kompletten Schleife mit break

3.3.3 Weitere Control Transfer Statements

3.3.4 Labeled Statements

4 Typen in Swift

4.1 Integer

4.2 Fließkommazahlen

4.3 Bool

4.4 String

4.4.1 Erstellen eines Strings

4.4.2 Zusammenfügen von Strings

4.4.3 Character auslesen

4.4.4 Character mittels Index auslesen

4.4.5 Character entfernen und hinzufügen

4.4.6 Anzahl der Character zählen

4.4.7 Präfix und Suffix prüfen

4.4.8 String Interpolation

4.5 Array

4.5.1 Erstellen eines Arrays

4.5.2 Zusammenfügen von Arrays

4.5.3 Inhalte eines Arrays leeren

4.5.4 Prüfen, ob ein Array leer ist

4.5.5 Anzahl der Elemente eines Arrays zählen

4.5.6 Zugriff auf die Elemente eines Arrays

4.5.7 Neue Elemente zu einem Array hinzufügen

4.5.8 Bestehende Elemente aus einem Array entfernen

4.5.9 Bestehende Elemente eines Arrays ersetzen

4.5.10 Alle Elemente eines Arrays auslesen und durchlaufen

4.6 Set

4.6.1 Erstellen eines Sets

4.6.2 Inhalte eines bestehenden Sets leeren

4.6.3 Prüfen, ob ein Set leer ist

4.6.4 Anzahl der Elemente eines Sets zählen

4.6.5 Element zu einem Set hinzufügen

4.6.6 Element aus einem Set entfernen

4.6.7 Prüfen, ob ein bestimmtes Element in einem Set vorhanden ist

4.6.8 Alle Elemente eines Sets auslesen und durchlaufen

4.6.9 Sets miteinander vergleichen

4.6.10 Neue Sets aus bestehenden Sets erstellen

4.7 Dictionary

4.7.1 Erstellen eines Dictionary

4.7.2 Prüfen, ob ein Dictionary leer ist

4.7.3 Anzahl der Schlüssel-Wert-Paare eines Dictionary zählen

4.7.4 Wert zu einem Schlüssel eines Dictionaries auslesen

4.7.5 Neues Schlüssel-Wert-Paar zu Dictionary hinzufügen

4.7.6 Bestehendes Schlüssel-Wert-Paar aus Dictionary entfernen

4.7.7 Bestehendes Schlüssel-Wert-Paar aus Dictionary verändern

4.7.8 Alle Schlüssel-Wert-Paare eines Dictionary auslesen und durchlaufen

4.8 Tuple

4.8.1 Zugriff auf die einzelnen Elemente eines Tuple

4.8.2 Tuple und switch

4.9 Optional

4.9.1 Deklaration eines Optionals

4.9.2 Zugriff auf den Wert eines Optionals

4.9.3 Optional Binding

4.9.4 Implicitly Unwrapped Optional

4.9.5 Optional Chaining

4.9.6 Optional Chaining über mehrere Eigenschaften und Funktionen

4.10 Any und AnyObject

4.11 Type Alias

4.12 Value Type versus Reference Type

4.12.1 Reference Types auf Gleichheit prüfen

5 Funktionen

5.1 Funktionen mit Parametern

5.1.1 Argument Labels und Parameter Names

5.1.2 Default Value für Parameter

5.1.3 Variadic Parameter

5.1.4 In-Out Parameter

5.2 Funktionen mit Rückgabewert

5.3 Function Types

5.3.1 Funktionen als Variablen und Konstanten

5.4 Verschachtelte Funktionen

5.5 Closures

5.5.1 Closures als Parameter von Funktionen

5.5.2 Trailing Closures

5.5.3 Autoclosures

6 Enumerations, Structures und Classes

6.1 Enumerations

6.1.1 Enumerations und switch

6.1.2 Associated Values

6.1.3 Raw Values

6.2 Structures

6.2.1 Erstellen von Structures und Instanzen

6.2.2 Eigenschaften und Funktionen

6.3 Classes

6.3.1 Erstellen von Klassen und Instanzen

6.3.2 Eigenschaften und Funktionen

6.4 Enumeration vs. Structure vs. Class

6.4.1 Gemeinsamkeiten und Unterschiede

6.4.2 Wann nimmt man was?

6.5 self

7 Eigenschaften und Funktionen von Typen

7.1 Properties

7.1.1 Stored Property

7.1.2 Lazy Stored Property

7.1.3 Computed Property

7.1.4 Read-Only Computed Property

7.1.5 Property Observer

7.1.6 Type Property

7.2 Globale und lokale Variablen

7.3 Methoden

7.3.1 Instance Methods

7.3.2 Type Methods

7.4 Subscripts

8 Initialisierung

8.1 Aufgabe der Initialisierung

8.2 Erstellen eigener Initializer

8.3 Initializer Delegation

8.3.1 Initializer Delegation bei Value Types

8.3.2 Initializer Delegation bei Reference Types

8.4 Failable Initializer

8.5 Required Initializer

8.6 Deinitialisierung

9 Vererbung

9.1 Überschreiben von Eigenschaften und Funktionen einer Klasse

9.2 Überschreiben von Eigenschaften und Funktionen einer Klasse verhindern

9.3 Zugriff auf die Superklasse

9.4 Initialisierung und Vererbung

9.4.1 Zwei-Phasen-Initialisierung

9.4.2 Überschreiben von Initializern

9.4.3 Vererbung von Initializern

9.4.4 Required Initializer

10 Speicherverwaltung mit ARC

10.1 Strong Reference Cycles

10.1.1 Weak References

10.1.2 Unowned References

10.1.3 Weak Reference vs. Unowned Reference

11 Weiterführende Sprachmerkmale von Swift

11.1 Nested Types

11.2 Extensions

11.2.1 Computed Properties

11.2.2 Methoden

11.2.3 Initializer

11.2.4 Subscripts

11.2.5 Nested Types

11.3 Protokolle

11.3.1 Deklaration von Eigenschaften und Funktionen

11.3.2 Der Typ eines Protokolls

11.3.3 Protokolle und Extensions

11.3.4 Vererbung in Protokollen

11.3.5 Class-only-Protokolle

11.3.6 Optionale Eigenschaften und Funktionen

11.3.7 Protocol Composition

11.3.8 Delegation

12 Type Checking und Type Casting

12.1 Type Checking mit is

12.2 Type Casting mit as

13 Error Handling

13.1 Deklaration und Feuern eines Fehlers

13.2 Reaktion auf einen Fehler

13.2.1 Mögliche Fehler mittels do-catch auswerten

13.2.2 Mögliche Fehler in Optionals umwandeln

13.2.3 Mögliche Fehler weitergeben

13.2.4 Mögliche Fehler ignorieren

14 Generics

14.1 Generic Functions

14.2 Generic Types

14.3 Type Constraints

14.4 Associated Types

15 Dateien und Interfaces

15.1 Modules und Source Files

15.2 Access Control

15.2.1 Access Level

15.2.2 Explizite und implizite Zuweisung eines Access Levels

15.2.3 Besonderheiten

16 Cocoa, Objective-C und C

16.1 Interoperability

16.1.1 Swift und Cocoa

16.1.2 Swift und Objective-C

16.1.3 Swift und C

16.2 Mix and Match

16.2.1 Mix and Match innerhalb eines App-Targets

16.2.2 Mix and Match innerhalb eines Framework-Targets

16.3 Migration

17 Objektorientierte vs. protokollorientierte Programmierung

17.1 Objektorientierte Programmierung

17.1.1 Praxis

17.1.2 Vor- und Nachteile

17.2 Protokollorientierte Programmierung

17.2.1 Praxis

17.2.2 Vor- und Nachteile

17.3 Fazit

18 Weitere Sprachmerkmale und Profi-Wissen

18.1 Zahlenliterale

18.2 Fortgeschrittene Operatoren

18.2.1 Ternary Conditional Operator

18.2.2 Nil-Coalescing Operator

18.2.3 Unary Minus- und Unary Plus-Operatoren

18.2.4 Bitweise Operatoren

18.2.5 Operator Methods

18.2.6 Eigene Operatoren erstellen

18.3 Option Sets

18.3.1.1 Deklaration eines Option Sets

18.3.1.2 Verwenden eines Option Sets

18.4 Closures

18.4.1 Escaping Closures

18.4.2 Closure Capture List

18.5 Recursive Enumerations

18.6 Optionals im Detail

18.7 Generic Where Clause

18.8 Dynamic Method Lookup

18.9 Weitere Objective-C-Makros

18.9.1 NS_SWIFT_NAME

18.9.2 NS_SWIFT_UNAVAILABLE

Thomas Sillmann

Swift 3
im Detail

Einführung und Sprachreferenz

1. Auflage

Der Autor:

Thomas Sillmann, Aschaffenburg, www.thomassillmann.de

Alle in diesem Buch enthaltenen Informationen, Verfahren und Darstellungen wurden nach bestem Wissen zusammengestellt und mit Sorgfalt getestet. Dennoch sind Fehler nicht ganz auszuschließen. Aus diesem Grund sind die im vorliegenden Buch enthaltenen Informationen mit keiner Verpflichtung oder Garantie irgendeiner Art verbunden. Autor und Verlag übernehmen infolgedessen keine juristische Verantwortung und werden keine daraus folgende oder sonstige Haftung übernehmen, die auf irgendeine Art aus der Benutzung dieser Informationen – oder Teilen davon – entsteht.

Ebenso übernehmen Autor und Verlag keine Gewähr dafür, dass beschriebene Verfahren usw. frei von Schutzrechten Dritter sind. Die Wiedergabe von Gebrauchsnamen, Handelsnamen, Warenbezeichnungen usw. in diesem Buch berechtigt deshalb auch ohne besondere Kennzeichnung nicht zu der Annahme, dass solche Namen im Sinne der Warenzeichen­ und Markenschutz­Gesetzgebung als frei zu betrachten wären und daher von jedermann benutzt werden dürften.

Bibliografische Information der Deutschen Nationalbibliothek: Die Deutsche Nationalbibliothek verzeichnet diese Publikation in der Deutschen Nationalbibliografie; detaillierte bibliografische Daten sind im Internet über http://dnb.d-nb.de abrufbar.

Dieses Werk ist urheberrechtlich geschützt. Alle Rechte, auch die der Übersetzung, des Nachdruckes und der Vervielfältigung des Buches, oder Teilen daraus, vorbehalten. Kein Teil des Werkes darf ohne schriftliche Genehmigung des Verlages in irgendeiner Form (Fotokopie, Mikrofilm oder ein anderes Verfahren) – auch nicht für Zwecke der Unterrichtsgestaltung – reproduziert oder unter Verwendung elektronischer Systeme verarbeitet, vervielfältigt oder verbreitet werden.

Lektorat: Sylvia Hasselbach
Copyediting: Walter Saumweber, Ratingen
Herstellung: Irene Weilhart
Umschlagdesign: Marc Müller-Bremer, München, www.rebranding.de
Umschlagrealisation: Stephan Rönigk

Print-ISBN 978-3-446-45072-1
E-Pub-ISBN 978-3-446-45353-1

Verwendete Schriften: SourceSansPro und SourceCodePro (Lizenz)
CSS-Version: 1.0

Font License Zurück zum Impressum

Copyright 2010, 2012, 2014 Adobe Systems Incorporated (http://www.adobe.com/), with Reserved Font Name 'Source'. All Rights Reserved. Source is a trademark of Adobe Systems Incorporated in the United States and/or other countries. This Font Software is licensed under the SIL Open Font License, Version 1.1. This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL ----------------------------------------------------------- SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 ----------------------------------------------------------- PREAMBLE The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others. The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives. DEFINITIONS "Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation. "Reserved Font Name" refers to any names specified as such after the copyright statement(s). "Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s). "Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment. "Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software. PERMISSION & CONDITIONS Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions: 1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself. 2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user. 3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users. 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission. 5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software. TERMINATION This license becomes null and void if any of the above conditions are not met. DISCLAIMER THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.

Widmung

Für Ela

‒ auf das was war, und das, was sein wird.

1Die Programmiersprache Swift