HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
The professional programmer’s Deitel® guide to Apple’s new Swift programming language for the iOS® and OS X® platforms
Written for programmers with a background in object-oriented programming in a C-based language like Objective-C, Java, C# or C++, this book applies the Deitel signature live-code approach with scores of complete, working, real-world programs to explore the new Swift language in depth. The code examples feature syntax shading, code highlighting, rich commenting, line-by-line code walkthroughs and live program outputs. The book features thousands of lines of proven Swift code, and tips that will help you build robust applications.
Start with an introduction to Swift using an early classes and objects approach, then rapidly move on to more advanced topics. When you master the material, you’ll be ready to build industrial-strength object-oriented Swift applications.
About This Book
The Swift™ programming language was arguably the most significant announcement at Apple’s 2014 Worldwide Developers Conference. Although apps can still be developed in Objective-C®, Apple says that Swift is its applications programming and systems programming language of the future.
Swift is a contemporary language with simpler syntax than Objective-C. Because Swift is new, its designers were able to include popular programming language features from languages such as Objective-C, Java™, C#, Ruby, Python® and many others. These features include automatic reference counting (ARC), type inference, optionals, String interpolation, tuples, closures (lambdas), extensions, generics, operator overloading, functions with multiple return values, switch statement enhancements and more. We’ve been able to develop apps more quickly in Swift than with Objective-C and the code is shorter, clearer and runs faster on today’s multi-core architectures.
Swift also eliminates the possibility of many errors common in other languages, making your code more robust and secure. Some of these error-prevention features include no implicit conversions, ARC, no pointers, required braces around every control statement’s body, assignment operators that do not return values, requiring initialization of all variables and constants before they’re used, array bounds checking, automatic checking for overflow of integer calculations, and more. You can combine Swift and Objective-C in the same app to enhance existing Objective-C apps without having to rewrite all the code. Your apps will easily be able to interact with the Cocoa®/Cocoa Touch® frameworks, which are largely written in Objective-C.
You can also use the new Xcode playgrounds with Swift. A playground is an Xcode window in which you can enter Swift code that compiles and executes as you type it. This allows you to see and hear your code’s results as you write it, quickly find and fix errors, and conveniently experiment with features of Swift and the Cocoa/Cocoa Touch frameworks.
Practical, Example-Rich Coverage of:
Visit www.deitel.com
Introduction to Classes, Objects, Methods and Functions in Swift
Preface xix
Before You Begin xxvii
Chapter 1: Introduction to Swift and Xcode 6 1
1.1 Introduction 2
1.2 Apple’s OS X® and iOS® Operating Systems: A Brief History 3
1.3 Objective-C 3
1.4 Swift: Apple’s Programming Language of the Future 4
1.5 Can I Use Swift Exclusively? 9
1.6 Xcode 6 Integrated Development Environment 10
1.7 Creating Swift Apps with Xcode 6 13
1.8 Web Resources 18
Chapter 2: Introduction to Swift Programming 20
2.1 Introduction 21
2.2 A First Swift Program: Printing a Line of Text 21
2.3 Modifying Your First Program 23
2.4 Composing Larger Strings with String Interpolation 25
2.5 Another Application: Adding Integers 27
2.6 Arithmetic 28
2.7 Decision Making: The if Conditional Statement and the Comparative Operators 29
2.8 Wrap-Up 32
Chapter 3: Introduction to Classes, Objects, Methods and Functions 33
3.1 Introduction 34
3.2 Account Class 35
3.3 Creating and Using Account Objects 40
3.4 Value Types vs. Reference Types 45
3.5 Software Engineering with Access Modifiers 46
3.6 Wrap-Up 47
Chapter 4: Control Statements; Assignment, Increment and Logical Operators 48
4.1 Introduction 49
4.2 Control Statements 49
4.3 if Conditional Statement 50
4.4 if…else Conditional Statement 50
4.5 Compound Assignment Operators 52
4.6 Increment and Decrement Operators 53
4.7 switch Conditional Statement 55
4.8 while Loop Statement 57
4.9 do…while Loop Statement 58
4.10 for…in Loop Statement and the Range Operators 58
4.11 for Loop Statement 63
4.12 break and continue Statements 64
4.13 Logical Operators 66
4.14 Wrap-Up 69
Chapter 5: Functions and Methods: A Deeper Look; enums and Tuples 70
5.1 Introduction 71
5.2