Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
About This eBook
Title Page
Copyright Page
Deitel® Series Page
Deitel® Developer Series
How To Program Series
Simply Series
CourseSmart Web Books
LiveLessons Video Learning Products
Dedication Page
Contents
Preface
Swift Programming Language
Software Used in Swift for Programmers
Swift Fundamentals: Parts I, II and III LiveLessons Video Training
Explosive Growth of the iPhone and iPad Is Creating Opportunity for Developers
Our Research Sources
Teaching Approach
Acknowledgments
Deitel Team
Pearson Education Team
Reviewers
A Special Thank You to Reviewer Charles Brown
Keeping in Touch with the Authors
About the Authors
About Deitel® & Associates, Inc.
Before You Begin
Conventions
Font and Naming
Conventions for Referencing Menu Items in a Menu
Software Used in this Book
A Note Regarding the Xcode 6 Toolbar Icons
Becoming a Registered Apple Developer
Fee-Based iOS Developer Programs
iOS Developer Program
iOS Developer Enterprise Program
iOS Developer University Program
Adding Your Paid iOS Developer Program Account to Xcode
Obtaining the Code Examples
Xcode Playgrounds and Projects for the Code Examples
Use Playgrounds for Learning
Viewing Output in a Playground
Playground and Project Naming Conventions
Configuring Xcode to Display Line Numbers
1. Introduction to Swift and Xcode 6
1.1 Introduction
1.2 Apple’s OS X® and iOS® Operating Systems: A Brief History
1.3 Objective-C
1.4 Swift: Apple’s Programming Language of the Future
1.4.1 Key Features of Many Popular Languages
1.4.2 Performance
1.4.3 Error Prevention
1.4.4 Swift Standard Library
1.4.5 Swift Apps and the Cocoa® and Cocoa Touch® Frameworks
1.4.6 Swift and Objective-C Interoperability
1.4.7 Other Apple Swift Resources
1.5 Can I Use Swift Exclusively?
1.5.1 Objective-C Programmers Who Are Developing New iOS and OS X Apps
1.5.2 Objective-C Programmers Who Are Enhancing Existing iOS and OS X Apps
1.5.3 Java, C++ and C# Programmers Who Are New to iOS and OS X App Development
1.5.4 Significant Language Changes Expected
1.5.5 A Mixture of Swift and Objective-C
1.6 Xcode 6 Integrated Development Environment
1.7 Creating Swift Apps with Xcode 6
1.8 Web Resources
2. Introduction to Swift Programming
2.1 Introduction
2.2 A First Swift Program: Printing a Line of Text
2.3 Modifying Your First Program
2.4 Composing Larger Strings with String Interpolation
2.5 Another Application: Adding Integers
2.6 Arithmetic
2.6.1 Automatic Arithmetic Overflow Checking
2.6.2 Operator Precedence
2.7 Decision Making: The if Conditional Statement and the Comparative Operators
2.8 Wrap-Up
3. Introduction to Classes, Objects, Methods and Functions
3.1 Introduction
3.2 Account Class
3.2.1 Defining a Class
3.2.2 Defining a Class Attribute as a Stored Property
3.2.3 Defining a public Stored Property with a private Setter
3.2.4 Initializing a Class’s Properties with init
3.2.5 Defining a Class’s Behaviors as Methods
3.3 Creating and Using Account Objects
3.3.1 Importing the Foundation Framework
3.3.2 Creating and Configuring an NSNumberFormatter to Format Currency Values
3.3.3 Defining a Function—formatAccountString
3.3.4 Creating Objects and Calling an Initializer
3.3.5 Calling Methods on Objects—Depositing into Account Objects
3.3.6 Calling Methods on Objects—Withdrawing from Account Objects
3.4 Value Types vs. Reference Types
3.5 Software Engineering with Access Modifiers
3.6 Wrap-Up
4. Control Statements; Assignment, Increment and Logical Operators
4.1 Introduction
4.2 Control Statements
4.3 if Conditional Statement
4.4 if...else Conditional Statement
4.5 Compound Assignment Operators
4.6 Increment and Decrement Operators
4.7 switch Conditional Statement
4.7.1 Using a switch Statement to Convert Numeric Grades to Letter Grades
4.7.2 Specifying Grade Ranges with the Closed-Range Operator (...)
4.7.3 The default Case
4.7.4 Other Patterns in the case Label
4.7.5 No Automatic Fall Through as in Other C-Based Languages
4.8 while Loop Statement
4.9 do...while Loop Statement
4.10 for...in Loop Statement and the Range Operators
4.10.1 Iterating Over Collections of Values with Closed Ranges, Half-Open Ranges and the Global stride Function
4.10.2 Compound-Interest Calculations with for...in
4.10.3 Formatting Strings with Field Widths and Justification
4.10.4 Performing the Interest Calculations
4.10.5 A Warning about Displaying Rounded Values
4.11 for Loop Statement
4.11.1 General Format of a for Statement
4.11.2 Scope of a for Statement’s Control Variable
4.11.3 Expressions in a for Statement’s Header Are Optional
4.12 break and continue Statements
4.12.1 break Statement Example
4.12.2 continue Statement Example
4.13 Logical Operators
4.13.1 Logical AND (&&) Operator
4.13.2 Logical OR (||) Operator
4.13.3 Short-Circuit Evaluation of Complex Conditions
4.13.4 Logical NOT (!) Operator
4.14 Wrap-Up
5. Functions and Methods: A Deeper Look; enums and Tuples
5.1 Introduction
5.2 Modules in Swift
5.3 Darwin Module—Using Predefined C Functions
5.4 Multiple-Parameter Function Definition
5.5 Random-Number Generation
5.6 Introducing Enumerations and Tuples
5.6.1 Introducing Enumeration (enum) Types
5.6.2 Tuples and Multiple Function Return Values
5.6.3 Tuples as Function Arguments
5.6.4 Accessing the Raw Value of an enum Constant
5.7 Scope of Declarations
5.8 Function and Method Overloading
5.9 External Parameter Names
5.10 Default Parameter Values
5.11 Passing Arguments by Value or by Reference
5.12 Recursion
5.13 Nested Functions
5.14 Wrap-Up
6. Arrays and an Introduction to Closures
6.1 Introduction
6.2 Arrays
6.3 Creating and Initializing Arrays
6.4 Iterating through Arrays
6.5 Adding and Removing Array Elements
6.6 Subscript Expressions with Ranges
6.7 Sorting Arrays; Introduction to Closures
6.7.1 Closures and Closure Expressions
6.7.2 Array Methods sort and sorted
6.7.3 Sorting with Function ascendingOrder
6.7.4 Using a Fully Typed Closure Expression
6.7.5 Using a Closure Expression with Inferred Types
6.7.6 Using a Closure Expression with Inferred Types and an Implicit return
6.7.7 Using a Closure Expression with Shorthand Argument Names
6.7.8 Using an Operator Function as a Closure Expression
6.7.9 Reversing an Array’s Elements
6.8 Array Methods filter, map and reduce
6.8.1 Filtering an Array
6.8.2 Mapping an Array’s Elements to New Values
6.8.3 Reducing an Array’s Elements to a Single Value
6.8.4 Combining Filtering, Mapping and Reducing
6.9 Card Shuffling and Dealing Simulation; Computed Properties; Optionals
6.9.1 Class Card
6.9.2 Class DeckOfCards
6.9.3 DeckOfCards Initializer
6.9.4 DeckOfCards Method shuffle
6.9.5 DeckOfCards Method dealCard and Optional Return Values
6.9.6 Shuffling and Dealing Cards
6.9.7 Unwrapping Optional Values with Optional Binding and the if or while Statements
6.10 Passing Arrays to Functions
6.10.1 Passing an Entire Array By Value
6.10.2 Passing One Array Element By Value
6.10.3 Passing an Entire Array By Reference
6.10.4 Passing One Array Element By Reference
6.11 Notes on Pass-By-Value and Pass-By-Reference
6.12 Multidimensional Arrays
6.13 Variadic Parameters
6.14 Wrap-Up
7. Dictionary
7.1 Introduction
7.1.1 What Is a Dictionary?
7.1.2 Dictionary Examples
7.1.3 Dictionary is a Generic Type
7.1.4 Dictionary Is a Value Type
7.1.5 Dictionary Is Implemented as a Hash Table
7.1.6 Dictionary Is Type Safe
7.2 Declaring a Dictionary: Key–Value Pairs and Dictionary Literals
7.2.1 Dictionary Key–Value Pairs and Dictionary Literals
7.2.2 Declaring a Dictionary with Generics and Explicit Typing
7.2.3 Declaring a Dictionary with Type Inference
7.2.4 Invoking Dictionary’s description Property Explicitly and Implicitly
7.3 Declaring and Printing Empty Dictionary Objects
7.4 Iterating through a Dictionary with for...in
7.5 General-Purpose Generic Dictionary Printing Function
7.6 Dictionary Equality Operators == and !=
7.7 Dictionary count and isEmpty Properties
7.8 Dictionary Whose Values Are Arrays
7.9 Dictionary’s keys and values Properties
7.10 Inserting, Modifying and Removing Key–Value Pairs with Subscripting
7.10.1 Updating the Value of an Existing Key–Value Pair
7.10.2 Adding a New Key–Value Pair
7.10.3 Removing a Key–Value Pair
7.10.4 Subscripting Returns an Optional Value
7.10.5 Processing an Optional Value
7.10.6 Inserting a New Key–Value Pair in an Empty Dictionary
7.11 Inserting, Removing and Modifying Key–Value Pairs
7.11.1 Inserting a Key–Value Pair with Dictionary Method updateValue
7.11.2 Updating a Key–Value Pair with Dictionary Method updateValue
7.11.3 Removing a Key–Value Pair with Dictionary Method removeValueForKey
7.11.4 Attempting to Remove a Nonexistent Key–Value Pair with Method removeValueForKey
7.11.5 Emptying a Dictionary with Method removeAll
7.12 Building a Dictionary Dynamically: Word Counts in a String
7.13 Bridging Between Dictionary and Foundation Classes
7.14 Hash Tables and Hashing
7.15 Wrap-Up
8. Classes: A Deeper Look and Extensions
8.1 Introduction
8.2 Time Class: Default Initializers and Property Observers
8.2.1 Stored Property Initialization and the Default Initializer
8.2.2 willSet and didSet Property Observers for Stored Properties
8.2.3 Computed Read-Only Properties universalDescription and description
8.2.4 Using Class Time
8.3 Designated and Convenience Initializers in Class Time
8.3.1 Class Time with Overloaded Initializers
8.3.2 Designated Initializers
8.3.3 Convenience Initializers and Initializer Delegation with self
8.3.4 Using Class Time’s Designated and Convenience Initializers
8.4 Failable Initializers in Class Time
8.4.1 Failable Designated Initializers
8.4.2 Failable Convenience Initializers
8.4.3 Implicitly Unwrapped Failable Initializers
8.4.4 Invoking Failable Initializers
8.5 Extensions to Class Time
8.5.1 Class Time with Extensions
8.5.2 Testing Class Time’s Extensions
8.5.3 Extensions and Access Modifiers
8.6 Read-Write Computed Properties
8.7 Composition
8.7.1 Class Employee
8.7.2 Testing Class Employee
8.8 Automatic Reference Counting, Strong References and Weak References
8.9 Deinitializers
8.10 Using NSDecimalNumber for Precise Monetary Calculations
8.11 Type Properties and Type Methods
8.11.1 Type Scope
8.11.2 Motivating Type Properties
8.11.3 Creating Type Properties and Type Methods in Classes
8.11.4 Using Type Properties and Type Methods
8.12 Lazy Stored Properties and Delayed Initialization
8.13 Wrap-Up
9. Structures, Enumerations and Nested Types
9.1 Introduction
9.2 Structure Definitions
9.2.1 Time struct Definition with Default and Memberwise Initializers
9.2.2 Custom Initializers extension to struct Time
9.2.3 Computed Properties extension to struct Time
9.2.4 Mutating Methods extension to struct Time
9.2.5 Testing the Time struct
9.3 Enumerations and Nested Types
9.3.1 Card struct with Nested Suit and Face enum Types
9.3.2 DeckOfCards struct
9.3.3 Testing the struct Types Card and DeckOfCards, and the enum Types Suit and Face
9.4 Choosing Among Structures, Enumerations and Classes in Your Apps
9.5 Associated Values for enums
9.6 Wrap-Up
10. Inheritance, Polymorphism and Protocols
10.1 Introduction
10.1.1 Superclasses and Subclasses
10.1.2 Polymorphism
10.1.3 Implementing for Extensibility
10.1.4 Programming in the Specific
10.1.5 Protocols
10.2 Superclasses and Subclasses
10.3 An Inheritance Hierarchy: CommunityMembers
10.4 Case Study: Using Inheritance to Create Related Employee Types
10.4.1 Superclass CommissionEmployee
10.4.2 Subclass BasePlusCommissionEmployee
10.4.3 Testing the Class Hierarchy
10.5 Access Modifiers in Inheritance Hierarchies
10.6 Introduction to Polymorphism: A Polymorphic Video Game Discussion
10.7 Case Study: Payroll System Class Hierarchy Using Polymorphism
10.7.1 Base Class Employee
10.7.2 Subclass SalariedEmployee
10.7.3 Subclass CommissionEmployee
10.7.4 Indirect Subclass BasePlusCommissionEmployee
10.7.5 Polymorphic Processing
10.8 Case Study: Creating and Using Custom Protocols
10.8.1 Protocol Capabilities Must Be Defined in Each Conforming Type
10.8.2 Protocols and Is-a Relationships
10.8.3 Relating Disparate Types Via Protocols
10.8.4 Accounts-Payable Application
10.8.5 Developing a Payable Hierarchy
10.8.6 Declaring Protocol Payable
10.8.7 Creating Class Invoice
10.8.8 Using extensions to Add Printable and Payable Protocol Conformance to Class Employee
10.8.9 Using Protocol Payable to Process Invoices and Employees Polymorphically
10.9 Additional Protocol Features
10.9.1 Protocol Inheritance
10.9.2 Class-Only Protocols
10.9.3 Optional Capabilities in Protocols
10.9.4 Protocol Composition
10.9.5 Common Protocols in Swift
10.10 Using final to Prevent Method Overriding and Inheritance
10.11 Initialization and Deinitialization in Class Hierarchies
10.11.1 Basic Class-Instance Initialization
10.11.2 Initialization in Class Hierarchies
10.11.3 Initialization of a BasePlusCommissionEmployee Object
10.11.4 Overriding Initializers and Required Initializers
10.11.5 Deinitialization in Class Hierarchies
10.12 Wrap-Up
11. Generics
11.1 Introduction
11.2 Motivation for Generic Functions
11.3 Generic Functions: Implementation and Specialization
11.4 Type Parameters with Type Constraints
11.5 Overloading Generic Functions
11.6 Generic Types
11.7 Note About Associated Types for Protocols
11.8 Wrap-Up
12. Operator Overloading and Subscripts
12.1 Introduction
12.2 String Operators and Methods
12.2.1 String Variables and Constants
12.2.2 String Comparative Operators
12.2.3 Custom String Unary Prefix Operator !
12.2.4 String Concatenation with Operators + and +=
12.2.5 String Subscript ([]) Operator for Creating Substrings
12.2.6 Other String Methods
12.3 Custom Complex Numeric Type with Overloaded Arithmetic Operators
12.3.1 Overloaded Operator Functions +, - and *
12.3.2 Overloading the Arithmetic Assignment Operator +=
12.3.3 Performing Arithmetic with Complex Numbers
12.4 Overloading Arithmetic Operators for Class NSDecimalNumber
12.4.1 Overloading the Multiplication Operator (*)
12.4.2 Overloading the Addition Operator (+)
12.4.3 Using the Overloaded Operators
12.4.4 Overloading the *= Multiplication Assignment Operator
12.5 Overloading Unary Operators: ++ and --
12.5.1 Overloading Unary Prefix Operators That Modify Their Operands
12.5.2 Overloading Unary Postfix Operators That Modify Their Operands
12.5.3 Swift’s AnyObject Type—Bridging Between Objective-C and Swift
12.6 Overloading Subscripts
12.6.1 Box Type with Custom Subscripts
12.6.2 Subscript Syntax
12.6.3 Type Box’s Int Subscript and the precondition Function
12.6.4 Type Box’s String Subscript
12.6.5 Using Type Box’s Subscripts
12.7 Custom Operators
12.7.1 Precedence and Associativity
12.7.2 Symbols Used in Custom Operators
12.7.3 Defining a Custom Exponentiation Operator for Type Int
12.8 Custom Generic Operators
12.9 Wrap-Up
13. iOS 8 App Development: Welcome App
13.1 Introduction
13.2 Technologies Overview
13.2.1 Xcode and Interface Builder
13.2.2 Labels and Image Views
13.2.3 Asset Catalogs and Image Sets
13.2.4 Running the App
13.2.5 Accessibility
13.2.6 Internationalization
13.3 Creating a Universal App Project with Xcode
13.3.1 Xcode Projects and App Templates
13.3.2 Creating and Configuring a Project
13.4 Xcode Workspace Window
13.4.1 Navigator Area
13.4.2 Editor Area
13.4.3 Utilities Area and Inspectors
13.4.4 Debug Area
13.4.5 Xcode Toolbar
13.4.6 Project Navigator
13.4.7 Keyboard Shortcuts
13.5 Storyboarding the Welcome App’s UI
13.5.1 Configuring the App for Portrait and Landscape Orientations
13.5.2 Providing an App Icon
13.5.3 Creating an Image Set for the App’s Image
13.5.4 Overview of the Storyboard and the Xcode Utilities Area
13.5.5 Adding an Image View to the UI
13.5.6 Using Inspectors to Configure the Image View
13.5.7 Adding and Configuring the Label
13.5.8 Using Auto Layout to Support Different Screen Sizes and Orientations
13.6 Running the Welcome App
13.6.1 Testing on the iOS Simulator
13.6.2 Testing on a Device (for Paid Apple iOS Developer Program Members Only)
13.7 Making Your App Accessible
13.7.1 Enabling Accessibility for the Image View
13.7.2 Confirming Accessibility Text with the Simulator’s Accessibility Inspector a
13.8 Internationalizing Your App
13.8.1 Locking Your UI During Translation
13.8.2 Exporting Your UI’s String Resources
13.8.3 Translating the String Resources
13.8.4 Importing the Translated String Resources
13.8.5 Testing the App in Spanish
13.9 Wrap-Up
14. iOS 8 App Development: Tip Calculator App
14.1 Introduction
14.2 Test-Driving the Tip Calculator App in the iPhone and iPad Simulators
14.3 Technologies Overview
14.3.1 Swift Programming
14.3.2 Swift Apps and the Cocoa Touch® Frameworks
14.3.3 Using the UIKit and Foundation Frameworks in Swift Code
14.3.4 Creating Labels, a Text Field and a Slider with Interface Builder
14.3.5 View Controllers
14.3.6 Linking UI Components to Your Swift Code
14.3.7 Performing Tasks After a View Loads
14.3.8 Bridging Between Swift and Objective-C Types
14.4 Building the App’s UI
14.4.1 Creating the Project
14.4.2 Configuring the Size Classes for Designing a Portrait Orientation iPhone App
14.4.3 Adding the UI Components
14.4.4 Adding the Auto Layout Constraints
14.5 Creating Outlets with Interface Builder
14.6 Creating Actions with Interface Builder
14.7 Class ViewController
14.7.1 import Declarations
14.7.2 ViewController Class Definition
14.7.3 ViewController’s @IBOutlet Properties
14.7.4 Other ViewController Properties
14.7.5 Overridden UIViewController method viewDidLoad
14.7.6 ViewController Action Method calculateTip
14.7.7 Global Utility Functions Defined in ViewController.swift
14.8 Wrap-Up
A. Keywords
B. Operator Precedence Chart
C. Labeled break and continue Statements
C.1 Introduction
C.2 Labeled break Statement
C.3 Labeled continue Statement
Index
Code Snippets
← Prev
Back
Next →
← Prev
Back
Next →