Beginning Swift - Rob Kerr - E-Book

Beginning Swift E-Book

Rob Kerr

0,0
23,92 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Swift greatly simplifies the process of developing applications for Apple devices. This book provides you with the essential skills to help you get started with developing applications using Swift.




Key Features



  • Teaches you how to correctly structure and architect software using Swift


  • Uses real-world examples to connect the theory to a professional setting


  • Imparts expertise in the core Swift standard library





Book Description



Take your first foray into programming for Apple devices with Swift.Swift is fundamentally different from Objective-C, as it is a protocol-oriented language. While you can still write normal object-oriented code in Swift, it requires a new way of thinking to take advantage of its powerful features and a solid understanding of the basics to become productive.




What you will learn



  • Explore the fundamental Swift programming concepts, language structure, and the Swift programming syntax


  • Learn how Swift compares to other computer languages and how to transform your thinking to leverage new concepts such as optionals and protocols


  • Master how to use key language elements, such as strings and collections


  • Grasp how Swift supports modern application development using advanced features, such as built-in Unicode support and higher-order functions



Who this book is for



If you are seeking fundamental Swift programming skills, in preparation for learning to develop native applications for iOS or macOS, this book is the best for you. You don't need to have any prior Swift knowledge; however, object-oriented programming experience is desired.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 200

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Beginning Swift
Why Subscribe?
PacktPub.com
Contributors
About the Authors
Packt is Searching for Authors Like You
Preface
What This Book Covers
What You Need for This Book
Who This Book is for
Conventions
Reader Feedback
Customer Support
Downloading the Example Code
Errata
Piracy
Questions
1. Swift Basics
Lesson objectives
Swift Program Structure
Hello, World!
Swift Variables and Constants
Declaring Swift Variables
Variables Versus Constants
Type Inference
Variable Naming
Working with Variables
Tuples
Creating a Tuple
Optionals
Declaring an Optional
Working with Optionals
Optional nil Values
Accessing Optional Values
Force Unwrapping an Optional
Conditionally Unwrapping Optionals
Using Optionals
The Swift guard Statement
Activity B: Variable Summary
Swift Data Types
Numeric Data Types
Int on 64-Bit Versus 32-Bit Platforms
Built-In Numeric Data Types
Choosing the Appropriate Numeric Data Type
Declaring and Assigning Integer Variables
Declaring and Assigning Floating Point Numbers
Numeric Literal Grouping
Numeric Type Conversions
Using Numeric Types
Boolean
Character
Assigning a Character
Constructing a Character Literal
String
Instantiating a String
String Concatenation
Extracting Characters
String Length
Activity C: Data Type Summary
Enums
Basic Enum Syntax
Enum with Raw Values
Activity D: Using Swift Enums
Summary
2. Swift Operators and Control Flow
Lesson objectives
Swift Operators
Assignment Operator
Arithmetic Operators
Standard Arithmetic Operators
Remainder Operator
Unary minus Operator
Compound Assignment Operators
Comparison Operators
Equality
Inequality
Comparison between Two Values
Ternary Conditional Operator
Logical Operators
Bitwise Operators
Nil-Coalescing Operator
Range Operators
Closed Range Operator
Half-Open Range Operator
One-Sided Range Operator
Activity A: Operators
Branching
The if Statement
Condition Lists
Optional Unwrapping with if
The switch Statement
switch Statement Rules
The break Keyword
The fallthrough Keyword
Matching Non-Scalar Values
Multiple Patterns in a Single Case
Using the where Statement within case
Evaluating Optionals with a switch Statement
Activity B: Converting Code from if to switch
Loops
The for…in Statement
Iterating over Objects
Iterating over Array Objects with index
The for Loop where Clause
The break Control Transfer Statement
The continue Control Transfer Statement
The while Loop
The repeat…while Loop
Activity C: Implementing Loops
Summary
3. Functions, Classes, and Structs
Lesson Objectives
Functions
Defining a Function
Argument Labels
Excluding Argument Labels
Parameter Default Values
Activity A: Implementing a Function
Returning Values from Functions
Using @discardableResult
Function Attributes
Variadic Parameters
inout Parameters
Recursion
Functions as Parameters
Closures
Creating a Function to Receive Content from an Asynchronous Web Service Call
Error Handling
The do…catch Statement
Multiple catch Blocks
Using do without catch
The guard Statement
Activity B: Exception Handling
Object-Oriented Features
Object-Oriented Principles
Classes Versus Structs
Illustration
Defining Classes and Structures
Activity C: Creating a Customer Struct and Class
Summary
Challenge
4. Collections
Lesson Objectives
Arrays
Working with Arrays
Index
Utilizing Common Operations with Index
ArraySlice
Creating Slices
Creating Slices Using Range Operators
Activity A: Working with Arrays
Sets
Working with Sets
Combining Sets
Comparing Sets
Activity B: Removing Duplicates from a Sequence
Dictionaries
Working with Dictionaries
Activity C: Using Dictionaries
Summary
5. Strings
Lesson Objectives
String Fundamentals
Character
Collection
Index
Working with String Index
Debugging
Activity A: All Indices of a Character
Using Strings
Creating Strings
Common Operations
Implementing Extra Text Operations on a String
Activity B-1: All Ranges of a Substring
Activity B-2: Counting Words, Sentences, and Paragraphs
Substring
Creating Substrings
Parsing Strings
Converting NSRange to Range
Activity C: CamelCase
Summary
6. Functional Programming and Lazy Operations
Lesson Objectives
Function Type
Functional Methods
filter
Using the filter Method
map
Using the map Function
flatMap
Using the flatMap Function
reduce
Using the reduce Function
Activity A: Implementing Functional Programming
Lazy Operations
Lazy Sequences
Sequence Internals
Creating Lazy Operations
sequence(first:next:)
sequence(state:next:)
Activity B: Implementing a Lazy Version of a Method
Swifty Code
Naming
Organizing Code
Miscellaneous
Writing Swifty Code
Summary
Further Study
Challenge
Index

Beginning Swift

Beginning Swift

Copyright © 2018 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Acquisition Editor: Aditya Date

Content Development Editor: Taabish Khan

Production Coordinator: Vishal Pawar

First published: May 2018

Production reference: 1310518

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78953-431-3

www.packtpub.com

mapt.io

Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.

Why Subscribe?

Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionalsLearn better with Skill Plans built especially for youGet a free eBook or video every monthMapt is fully searchableCopy and paste, print, and bookmark content

PacktPub.com

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

Contributors

About the Authors

Rob Kerr is a mobile software architect based in United States. He has been working professionally with Swift since its introduction, delivering applications to the public App Store and through enterprise distribution. His current focus is developing state-of-the-art iOS applications using Swift in the IoT space.

Kåre Morstøl is an independent software developer from Norway, with a bachelor's degree in software development. He has programmed almost exclusively in Swift since it was announced. He thinks it's a great language that is continually getting better.

Packt is Searching for Authors Like You

If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.

Preface

Swift is a multi-paradigm language. It has expressive features familiar to those used to working with modern functional languages, whilst also keeping the object-oriented features of Objective-C. Swift vastly streamlines the developer experience, and Apple's Xcode playground is a real game-changer.

The book begins by teaching you the basic syntax and structure of Swift, and how to correctly structure and architect software using Swift. It then builds expertise in the core Swift standard library you will need to understand to complete real-world Swift programming projects. We will work through concepts such as operators, branching and loop structures, functions, classes, structs, collections, and strings. We end the book with a brief look at functional programming and lazy operations.

After reading and understanding this book, you will be well-prepared to begin developing native end-user applications for iOS or macOS, or to develop server-side (backend) application and web services using Swift on Linux.

What This Book Covers

Lesson 1, Swift Basics, covers the fundamentals of using the Swift programming language. In this lesson, you'll learn basic Swift syntax and program structure. You'll also learn how to use Swift built-in data types and enums, and how to declare and use Swift variables and constants.

Lesson 2, Swift Operators and Control Flow, shows you how to use the fundamental flow control structures and language elements that form the building blocks of Swift programs. We will specifically cover operators, branching statements, and loops in this lesson.

Lesson 3, Functions, Classes, and Structs, teaches you how to develop fully featured Swift functions, catch unexpected errors, and use asynchronous programming paradigms. You'll learn how to create your own data types, and create object-oriented applications using classes and structs.

Lesson 4, Collections, shows you how to work with Swift's collections, such as arrays, sets, and dictionaries.

Lesson 5, Strings, covers Swift strings in detail. We will create and use strings and substrings, and see the various common operations available for strings.

Lesson 6, Functional Programming and Lazy Operations, ventures at functional programming and explains what lazy operations are. We will end this lesson with an important but often overlooked topic—writing Swifty code.

What You Need for This Book

This book will require the following hardware:

A Mac computer capable of running macOS Sierra 10.12.6+An internet connection

Please ensure you have the following software installed on your machine:

Operating system: macOS Sierra 10.12.6+ Xcode 9.1Safari browser

Who This Book is for

This book is ideal for developers seeking fundamental Swift programming skills, in preparation for learning to develop native applications for iOS or macOS. No prior Swift knowledge is expected but object-oriented programming experience is desirable.

You should have basic working knowledge of computer programming in a procedural/object-oriented language, such as Objective-C, BASIC, C++, Python, Java, or JavaScript.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.

Chapter 1. Swift Basics

Swift is a relatively new programming language designed by Apple Inc., and was initially made available to Apple developers in 2014—primarily intended as a replacement for the aging Objective-C language that was the foundation of OS X and iOS software development at the time.

Unlike many object-oriented languages, which are based on older procedural languages—for example, C++ and Objective-C are based on C—Swift was designed from the ground up as a new, modern, object-oriented language that makes programming faster and easier, and helps developers produce expressive code that's less prone to errors than many languages.

While not based on an older language, Swift, in the words of its chief architect, Chris Lattner, "was inspired by drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list." (Chris Lattner home page: http://nondot.org/sabre/).

Swift was initially a proprietary language, but was made open source software in December 2015 as of its version 2.2. While Swift remains primarily used by developers targeting the Apple macOS and iOS platforms, Swift is also fully supported on Linux, and there are unofficial ports under development for Windows as well.

The objective of this lesson is to learn the fundamentals of using the Swift programming language. In this lesson, you'll learn basic Swift syntax and program structure. You'll also learn how to use Swift built-in data types and enums, and how to declare and use Swift variables and constants. Let's get started.

Lesson objectives

By the end of this lesson, you will be able to:

Explain the program structure and syntax of Swift programsDeclare and use Swift variables and constantsUse the various built-in Swift data typesUse the Swift enum language syntax