100% found this document useful (4 votes)
23 views

Beginning iPhone Development with Swift 4: Exploring the iOS SDK 4th Edition Molly K. Maskrey 2024 Scribd Download

SDK

Uploaded by

kesegouys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
23 views

Beginning iPhone Development with Swift 4: Exploring the iOS SDK 4th Edition Molly K. Maskrey 2024 Scribd Download

SDK

Uploaded by

kesegouys
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Get ebook downloads in full at ebookmeta.

com

Beginning iPhone Development with Swift 4:


Exploring the iOS SDK 4th Edition Molly K. Maskrey

https://ebookmeta.com/product/beginning-iphone-development-
with-swift-4-exploring-the-ios-sdk-4th-edition-molly-k-
maskrey/

OR CLICK BUTTON

DOWNLOAD NOW

Explore and download more ebook at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Beginning iPhone Development with Swift 5, 5th Edition:


Exploring the iOS SDK Wallace Wang

https://ebookmeta.com/product/beginning-iphone-development-with-
swift-5-5th-edition-exploring-the-ios-sdk-wallace-wang/

ebookmeta.com

Beginning iPhone Development with SwiftUI: Exploring the


iOS SDK 6th Edition Wally Wang

https://ebookmeta.com/product/beginning-iphone-development-with-
swiftui-exploring-the-ios-sdk-6th-edition-wally-wang/

ebookmeta.com

Beginning iPhone Development with SwiftUI: Exploring the


iOS SDK (7th Edition) Wallace Wang

https://ebookmeta.com/product/beginning-iphone-development-with-
swiftui-exploring-the-ios-sdk-7th-edition-wallace-wang/

ebookmeta.com

Advanced R Solutions 1st Edition Malte Grosser

https://ebookmeta.com/product/advanced-r-solutions-1st-edition-malte-
grosser-2/

ebookmeta.com
Alternative Pathways to Sustainable Development Lessons
from Latin America 9 International Development Policy
Gilles Carbonnier
https://ebookmeta.com/product/alternative-pathways-to-sustainable-
development-lessons-from-latin-america-9-international-development-
policy-gilles-carbonnier/
ebookmeta.com

Foundations of Comparative Politics Democracies of the


Modern World 4th Edition Kenneth Newton

https://ebookmeta.com/product/foundations-of-comparative-politics-
democracies-of-the-modern-world-4th-edition-kenneth-newton/

ebookmeta.com

Prehistoric Hunter Gatherers of the High Plains and


Rockies 3rd Edition Marcel Kornfeld George C Frison Mary
Lou Larson
https://ebookmeta.com/product/prehistoric-hunter-gatherers-of-the-
high-plains-and-rockies-3rd-edition-marcel-kornfeld-george-c-frison-
mary-lou-larson/
ebookmeta.com

Youngblood 1st Edition Sasha Laurens

https://ebookmeta.com/product/youngblood-1st-edition-sasha-laurens/

ebookmeta.com

JFK vs Allen Dulles 1st Edition Greg Poulgrain

https://ebookmeta.com/product/jfk-vs-allen-dulles-1st-edition-greg-
poulgrain/

ebookmeta.com
There Was a Young Rabbi A Hanukkah Tale Suzanne Wolfe

https://ebookmeta.com/product/there-was-a-young-rabbi-a-hanukkah-tale-
suzanne-wolfe/

ebookmeta.com
Beginning iPhone
Development
with Swift 4
Exploring the iOS SDK

Fourth Edition

Molly K. Maskrey

www.allitebooks.com
Beginning iPhone
Development with
Swift 4
Exploring the iOS SDK

Fourth Edition

Molly K. Maskrey

www.allitebooks.com
Beginning iPhone Development with Swift 4: Exploring the iOS SDK
Molly K. Maskrey
Parker, Colorado, USA
ISBN-13 (pbk): 978-1-4842-3071-8 ISBN-13 (electronic): 978-1-4842-3072-5
https://doi.org/10.1007/978-1-4842-3072-5
Library of Congress Control Number: 2017957132
Copyright © 2017 by Molly K. Maskrey
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material
is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter
developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly
analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for
exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under
the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must
always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright
Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material
contained herein.
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Technical Reviewer: Bruce Wade
Coordinating Editor: Jessica Vakili
Copy Editor: Kim Wimpsett
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springer.com. Apress Media, LLC is a California LLC and
the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM
Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional
use. eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to
readers at www.apress.com. For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code/.
Printed on acid-free paper

www.allitebooks.com
Another year, another revision of this book, as well as one of my own personal tribe.
Still around after more than two years of ups and downs, KP stuck beside me during my
most difficult times. No matter what I did, her sanity and silliness kept me going. She was,
for a long while, my muse…my inspiration to write and to keep it as fun as possible…never
taking myself too seriously. Now, as our paths slightly diverge, the qualities she’s left permeate
my days and make me a better self.
Mellie has been my rock. We’ve been in and out of being friends, and I hope she knows
I consider her the closest friend I’ve ever had. I probably wouldn’t be doing this if it weren’t
for the love she’s shown me.
Tonya came into my life recently and has been such a godsend to help me become more
confident in all I say and do, and I can’t thank her enough.
She probably has no clue she’s in here, but Lauren kept me going for the past year.
When I would’ve preferred to lay in the dark and sleep and zone out, without any
aggressiveness Lauren would give me something in her words to keep to going…to
not give up. She—I won’t say forced, but she was very persuasive—got me into a
program that is, this very day, putting me onto a better path for myself and my future and
the future of anyone alongside me for my journey.
Finally, to Jennifer…our lives have radically changed over the past year. But, as partners
in business and advocates for each other’s happy life, our relationship bond, while admittedly
very tumultuous, has solidified into something we both see as pretty damn good.
In my personal struggles over the preceding year, these friends kept me from falling into
an abyss so deep I might never have returned. Writing can be a lonely thing, and having
a support system such as these beautiful, wonderful women are the only reason this endeavor
was a success. A special friend told me last year that some friends are only in your life for
a season. I pray that these women are friends for a lifetime.
—MM, August 2017

www.allitebooks.com
Contents at a Glance

About the Author���������������������������������������������������������������������������������������������������xvii


About the Technical Reviewer��������������������������������������������������������������������������������xix
Acknowledgments��������������������������������������������������������������������������������������������������xxi


■Chapter 1: Getting to Know the iOS Landscape����������������������������������������������������� 1

■Chapter 2: Writing Your First App������������������������������������������������������������������������ 13

■Chapter 3: Basic User Interactions���������������������������������������������������������������������� 51

■Chapter 4: Adding Intermediate-Level User Interactions������������������������������������ 87

■Chapter 5: Working with Device Rotations�������������������������������������������������������� 131

■Chapter 6: Creating a Multiview Application����������������������������������������������������� 179

■Chapter 7: Using Tab Bars and Pickers������������������������������������������������������������� 209

■Chapter 8: Introducing Table Views������������������������������������������������������������������� 255

■Chapter 9: Adding Navigation Controllers to Table Views��������������������������������� 309

■Chapter 10: Collection Views����������������������������������������������������������������������������� 343

■Chapter 11: Split Views and Popovers for iPad Apps���������������������������������������� 357

■Chapter 12: App Customization with Settings and Defaults������������������������������ 383

■Chapter 13: Persistence: Saving Data Between App Launches������������������������� 419

■Chapter 14: Graphics and Drawing�������������������������������������������������������������������� 465

■Appendix A: An Introduction to Swift���������������������������������������������������������������� 491

Index��������������������������������������������������������������������������������������������������������������������� 547

www.allitebooks.com
Contents

About the Author���������������������������������������������������������������������������������������������������xvii


About the Technical Reviewer��������������������������������������������������������������������������������xix
Acknowledgments��������������������������������������������������������������������������������������������������xxi


■Chapter 1: Getting to Know the iOS Landscape����������������������������������������������������� 1
About the Book����������������������������������������������������������������������������������������������������������������� 2
Things You’ll Need������������������������������������������������������������������������������������������������������������ 2
Your Options as a Developer������������������������������������������������������������������������������������������������������������������� 4
Things You Should Know������������������������������������������������������������������������������������������������������������������������ 6
Some Unique Aspects About Working in iOS������������������������������������������������������������������������������������������ 6

What’s in This Book�������������������������������������������������������������������������������������������������������� 10


What’s New in This Update?����������������������������������������������������������������������������������������������������������������� 11
Swift and Xcode Versions��������������������������������������������������������������������������������������������������������������������� 11

Let’s Get Started������������������������������������������������������������������������������������������������������������� 12



■Chapter 2: Writing Your First App������������������������������������������������������������������������ 13
Creating the Hello World Project������������������������������������������������������������������������������������ 14
Taking a Look at the Xcode Project Window����������������������������������������������������������������������������������������� 18
Taking a Closer Look at the Hello World Project����������������������������������������������������������������������������������� 30

Introducing Xcode’s Interface Builder���������������������������������������������������������������������������� 31


Introducing File Formats����������������������������������������������������������������������������������������������������������������������� 32
Exploring the Storyboard���������������������������������������������������������������������������������������������������������������������� 32
Exploring the Utilities Area������������������������������������������������������������������������������������������������������������������� 34
Adding a Label to the View������������������������������������������������������������������������������������������������������������������� 36
Changing Attributes������������������������������������������������������������������������������������������������������������������������������ 38

vii

www.allitebooks.com
■ Contents

Adding the Finishing Touches����������������������������������������������������������������������������������������� 40


Exploring the Launch Screen����������������������������������������������������������������������������������������� 44
Running the Application on a Device������������������������������������������������������������������������������ 46
Summary������������������������������������������������������������������������������������������������������������������������ 50

■Chapter 3: Basic User Interactions���������������������������������������������������������������������� 51
Understanding the MVC Paradigm��������������������������������������������������������������������������������� 52
Creating the ButtonFun App������������������������������������������������������������������������������������������� 52
Understanding the ViewController���������������������������������������������������������������������������������� 53
Understanding Outlets and Actions������������������������������������������������������������������������������������������������������ 55
Simplifying the View Controller������������������������������������������������������������������������������������������������������������ 57
Designing the User Interface���������������������������������������������������������������������������������������������������������������� 57
Testing the ButtonFun App�������������������������������������������������������������������������������������������������������������������� 68
Previewing Layout�������������������������������������������������������������������������������������������������������������������������������� 80
Changing the Text Style������������������������������������������������������������������������������������������������������������������������ 82

Examining the Application Delegate������������������������������������������������������������������������������� 83


Summary������������������������������������������������������������������������������������������������������������������������ 86

■Chapter 4: Adding Intermediate-Level User Interactions������������������������������������ 87
Understanding Active, Static, and Passive Controls������������������������������������������������������� 92
Creating the ControlFun Application������������������������������������������������������������������������������� 93
Implementing the Image View and Text Fields��������������������������������������������������������������� 93
Adding the Image View������������������������������������������������������������������������������������������������������������������������� 94
Resizing the Image View���������������������������������������������������������������������������������������������������������������������� 96
Setting View Attributes������������������������������������������������������������������������������������������������������������������������� 98
Using the Mode Attribute���������������������������������������������������������������������������������������������������������������������� 98
Using the Semantic Attribute���������������������������������������������������������������������������������������������������������������� 98
Using Tag���������������������������������������������������������������������������������������������������������������������������������������������� 99
Using Interaction Check Boxes������������������������������������������������������������������������������������������������������������� 99
Using the Alpha Value��������������������������������������������������������������������������������������������������������������������������� 99
Using Background�������������������������������������������������������������������������������������������������������������������������������� 99
Using Tint���������������������������������������������������������������������������������������������������������������������������������������������� 99

viii

www.allitebooks.com
■ Contents

Drawing Check Boxes������������������������������������������������������������������������������������������������������������������������� 100


Stretching������������������������������������������������������������������������������������������������������������������������������������������� 100
Adding the Text Fields������������������������������������������������������������������������������������������������������������������������� 100
Using Text Field Inspector Settings����������������������������������������������������������������������������������������������������� 106
Setting the Attributes for the Second Text Field��������������������������������������������������������������������������������� 107
Adding Constraints����������������������������������������������������������������������������������������������������������������������������� 107
Creating and Connecting Outlets�������������������������������������������������������������������������������������������������������� 108
Closing the Keyboard�������������������������������������������������������������������������������������������������������������������������� 110
Closing the Keyboard When Done Is Tapped��������������������������������������������������������������������������������������� 111
Touching the Background to Close the Keyboard������������������������������������������������������������������������������� 112
Adding the Slider and Label��������������������������������������������������������������������������������������������������������������� 114
Creating and Connecting the Actions and Outlets������������������������������������������������������������������������������ 116
Implementing the Action Method�������������������������������������������������������������������������������������������������������� 117
Implementing the Switches, Button, and Segmented Control������������������������������������������������������������ 117
Adding Two Labeled Switches������������������������������������������������������������������������������������������������������������ 119
Connecting and Creating Outlets and Actions������������������������������������������������������������������������������������ 120
Implementing the Switch Actions������������������������������������������������������������������������������������������������������� 120
Adding the Button������������������������������������������������������������������������������������������������������������������������������� 120
Adding an Image to the Button����������������������������������������������������������������������������������������������������������� 122
Using Stretchable Images������������������������������������������������������������������������������������������������������������������� 122
Using Control States��������������������������������������������������������������������������������������������������������������������������� 123
Connecting and Creating the Button Outlets and Actions������������������������������������������������������������������� 124
Implementing the Segmented Control Action������������������������������������������������������������������������������������� 124
Implementing the Action Sheet and Alert������������������������������������������������������������������������������������������� 125
Displaying an Action Sheet����������������������������������������������������������������������������������������������������������������� 126
Presenting an Alert����������������������������������������������������������������������������������������������������������������������������� 129

Summary���������������������������������������������������������������������������������������������������������������������� 130

■Chapter 5: Working with Device Rotations�������������������������������������������������������� 131
Understanding the Mechanics of Rotation������������������������������������������������������������������� 132
Understanding Points, Pixels, and the Retina Display������������������������������������������������������������������������ 132
Handling Rotation������������������������������������������������������������������������������������������������������������������������������� 133

ix

www.allitebooks.com
■ Contents

Creating Your Orientations Project������������������������������������������������������������������������������� 133


Understanding Supported Orientations at the App Level�������������������������������������������������������������������� 133
Understanding Per-Controller Rotation Support��������������������������������������������������������������������������������� 136

Creating Your Layout Project���������������������������������������������������������������������������������������� 137


Overriding Default Constraints����������������������������������������������������������������������������������������������������������� 143
Using Full-Width Labels���������������������������������������������������������������������������������������������������������������������� 144

Creating Adaptive Layouts�������������������������������������������������������������������������������������������� 147


Creating the Restructure Application�������������������������������������������������������������������������������������������������� 147
Setting the iPhone Landscape (wC hC) Configuration������������������������������������������������������������������������ 156
Setting the iPad (iPhone Plus Landscape) (wR hR) Configurations���������������������������������������������������� 169

Summary���������������������������������������������������������������������������������������������������������������������� 178

■Chapter 6: Creating a Multiview Application����������������������������������������������������� 179
Looking at Common Types of Multiview Apps�������������������������������������������������������������� 179
Looking at the Architecture of a Multiview Application������������������������������������������������ 185
Understanding the Root Controller����������������������������������������������������������������������������������������������������� 188
Content View Anatomy������������������������������������������������������������������������������������������������������������������������ 188

Creating the View Switcher Application����������������������������������������������������������������������� 188


Renaming the View Controller������������������������������������������������������������������������������������������������������������ 189
Adding the Content View Controllers�������������������������������������������������������������������������������������������������� 191
Modifying SwitchingViewController.swift������������������������������������������������������������������������������������������� 192
Building a View with a Toolbar������������������������������������������������������������������������������������������������������������ 192
Linking the Toolbar Button to the View Controller������������������������������������������������������������������������������ 195
Writing the Root View Controller Implementation������������������������������������������������������������������������������� 196
Implementing the Content Views�������������������������������������������������������������������������������������������������������� 201
Animating the Transition��������������������������������������������������������������������������������������������������������������������� 205

Summary���������������������������������������������������������������������������������������������������������������������� 207

■Chapter 7: Using Tab Bars and Pickers������������������������������������������������������������� 209
The Pickers Application������������������������������������������������������������������������������������������������ 210
Delegates and Data Sources���������������������������������������������������������������������������������������� 216

www.allitebooks.com
■ Contents

Creating the Pickers Application���������������������������������������������������������������������������������� 216


Creating the View Controllers������������������������������������������������������������������������������������������������������������� 216
Creating the Tab Bar Controller����������������������������������������������������������������������������������������������������������� 217
Initial Simulator Test��������������������������������������������������������������������������������������������������������������������������� 221
Implementing the Date Picker������������������������������������������������������������������������������������������������������������ 222

Implementing the Single-Component Picker���������������������������������������������������������������� 226


Building the View�������������������������������������������������������������������������������������������������������������������������������� 226
Implementing the Data Source and Delegate������������������������������������������������������������������������������������� 230

Implementing a Multicomponent Picker���������������������������������������������������������������������� 233


Building the View�������������������������������������������������������������������������������������������������������������������������������� 233
Implementing the Controller��������������������������������������������������������������������������������������������������������������� 233
Implementing Dependent Components���������������������������������������������������������������������������������������������� 236

Creating a Simple Game with a Custom Picker������������������������������������������������������������ 244


Preparing the View Controller������������������������������������������������������������������������������������������������������������� 244
Building the View�������������������������������������������������������������������������������������������������������������������������������� 244
Implementing the Controller��������������������������������������������������������������������������������������������������������������� 245
Additional Details for Your Game�������������������������������������������������������������������������������������������������������� 249

Summary���������������������������������������������������������������������������������������������������������������������� 253

■Chapter 8: Introducing Table Views������������������������������������������������������������������� 255
Understanding Table View Basics��������������������������������������������������������������������������������� 256
Using Table Views and Table View Cells��������������������������������������������������������������������������������������������� 256
Understanding Grouped and Plain Tables������������������������������������������������������������������������������������������� 257

Implementing a Simple Table��������������������������������������������������������������������������������������� 258


Designing the View����������������������������������������������������������������������������������������������������������������������������� 258
Implementing the Controller��������������������������������������������������������������������������������������������������������������� 261
Adding an Image��������������������������������������������������������������������������������������������������������������������������������� 265
Using Table View Cell Styles��������������������������������������������������������������������������������������������������������������� 267
Setting the Indent Level���������������������������������������������������������������������������������������������������������������������� 271
Handling Row Selection���������������������������������������������������������������������������������������������������������������������� 272
Changing the Font Size and Row Height�������������������������������������������������������������������������������������������� 274

xi

www.allitebooks.com
■ Contents

Customizing Table View Cells��������������������������������������������������������������������������������������� 276


Adding Subviews to the Table View Cell��������������������������������������������������������������������������������������������� 277

Implementing a Custom Table Views Application��������������������������������������������������������� 277


Creating a UITableViewCell Subclass�������������������������������������������������������������������������������������������������� 278
Loading a UITableViewCell from a XIB File����������������������������������������������������������������������������������������� 282

Using Grouped and Indexed Sections��������������������������������������������������������������������������� 290


Building the View�������������������������������������������������������������������������������������������������������������������������������� 290
Importing the Data������������������������������������������������������������������������������������������������������������������������������ 291
Implementing the Controller��������������������������������������������������������������������������������������������������������������� 292
Adding an Index���������������������������������������������������������������������������������������������������������������������������������� 296
Adding a Search Bar��������������������������������������������������������������������������������������������������������������������������� 297
Using View Debugging������������������������������������������������������������������������������������������������������������������������ 305

Summary���������������������������������������������������������������������������������������������������������������������� 307

■Chapter 9: Adding Navigation Controllers to Table Views��������������������������������� 309
Understanding Navigation Controller Basics���������������������������������������������������������������� 309
Using Stacks��������������������������������������������������������������������������������������������������������������������������������������� 310
Using a Stack of Controllers��������������������������������������������������������������������������������������������������������������� 311

Fonts: Creating a Simple Font Browser������������������������������������������������������������������������ 312


Seeing the Subcontrollers of the Fonts App��������������������������������������������������������������������������������������� 313
Seeing the Fonts Application’s Skeleton�������������������������������������������������������������������������������������������� 315
Creating the Root View Controller������������������������������������������������������������������������������������������������������� 319
Doing the Initial Storyboard Setup������������������������������������������������������������������������������������������������������ 322
First Subcontroller: Creating the Font List View��������������������������������������������������������������������������������� 323
Creating the Font List Storyboard������������������������������������������������������������������������������������������������������� 325

Creating the Font Sizes View Controller����������������������������������������������������������������������� 328


Creating the Font Sizes View Controller Storyboard��������������������������������������������������������������������������� 330
Implementing the Font Sizes View Controller Prepare for Segue������������������������������������������������������� 330
Creating the Font Info View Controller������������������������������������������������������������������������������������������������ 331
Creating the Font Info View Controller Storyboard����������������������������������������������������������������������������� 332

xii
■ Contents

Adapting the Font List View Controller for Multiple Segues��������������������������������������������������������������� 337
Creating My Favorite Fonts����������������������������������������������������������������������������������������������������������������� 337
Adding Features��������������������������������������������������������������������������������������������������������������������������������� 338
Implementing Swipe-to-Delete���������������������������������������������������������������������������������������������������������� 338
Implementing Drag-to-Reorder���������������������������������������������������������������������������������������������������������� 340

Summary���������������������������������������������������������������������������������������������������������������������� 342

■Chapter 10: Collection Views����������������������������������������������������������������������������� 343
Creating the DialogViewer Project�������������������������������������������������������������������������������� 343
Defining Custom Cells������������������������������������������������������������������������������������������������������������������������ 345
Configuring the View Controller���������������������������������������������������������������������������������������������������������� 348
Providing Content Cells���������������������������������������������������������������������������������������������������������������������� 349
Creating the Layout Flow�������������������������������������������������������������������������������������������������������������������� 351
Implementing the Header Views��������������������������������������������������������������������������������������������������������� 353

Summary���������������������������������������������������������������������������������������������������������������������� 355

■Chapter 11: Split Views and Popovers for iPad Apps���������������������������������������� 357
Building Master-Detail Applications with UISplitViewController����������������������������������� 359
Understanding How the Storyboard Defines the Structure���������������������������������������������������������������� 361
Understanding How Code Defines the Functionality�������������������������������������������������������������������������� 363
Understanding How the Master-Detail Template Application Works��������������������������������������������������� 367
Adding the President Data������������������������������������������������������������������������������������������������������������������ 369
Creating Your Own Popover���������������������������������������������������������������������������������������������������������������� 375
Summary���������������������������������������������������������������������������������������������������������������������� 381

■Chapter 12: App Customization with Settings and Defaults������������������������������ 383
Exploring the Settings Bundle�������������������������������������������������������������������������������������� 383
Creating the Bridge Control Application����������������������������������������������������������������������� 385
Creating the Bridge Control Project���������������������������������������������������������������������������������������������������� 390
Working with the Settings Bundle������������������������������������������������������������������������������������������������������ 391
Reading Settings in Your Application�������������������������������������������������������������������������������������������������� 408
Changing Defaults from Your Application������������������������������������������������������������������������������������������� 412

xiii
■ Contents

Registering Default Values������������������������������������������������������������������������������������������������������������������ 414


Keeping It Real����������������������������������������������������������������������������������������������������������������������������������� 415
Switching to the Settings Application������������������������������������������������������������������������������������������������� 417

Summary���������������������������������������������������������������������������������������������������������������������� 418

■Chapter 13: Persistence: Saving Data Between App Launches������������������������� 419
Your Application’s Sandbox������������������������������������������������������������������������������������������ 419
Getting the Documents and Library Directories���������������������������������������������������������������������������������� 423
Getting the tmp Directory������������������������������������������������������������������������������������������������������������������� 424

File-Saving Strategies�������������������������������������������������������������������������������������������������� 425


Single-File Persistence����������������������������������������������������������������������������������������������������������������������� 425
Multiple-File Persistence�������������������������������������������������������������������������������������������������������������������� 425

Using Property Lists����������������������������������������������������������������������������������������������������� 425


Property List Serialization������������������������������������������������������������������������������������������������������������������� 426
Creating the First Version of a Persistence Application���������������������������������������������������������������������� 427

Archiving Model Objects����������������������������������������������������������������������������������������������� 433


Conforming to NSCoding�������������������������������������������������������������������������������������������������������������������� 433
Implementing NSCopying������������������������������������������������������������������������������������������������������������������� 434
Archiving and Unarchiving Data Objects�������������������������������������������������������������������������������������������� 435
The Archiving Application������������������������������������������������������������������������������������������������������������������� 436
Using iOS’s Embedded SQLite3���������������������������������������������������������������������������������������������������������� 438
Creating or Opening the Database������������������������������������������������������������������������������������������������������ 439
Using Bind Variables��������������������������������������������������������������������������������������������������������������������������� 440

Creating the SQLite3 Application���������������������������������������������������������������������������������� 441


Linking to the SQLite3 Library������������������������������������������������������������������������������������������������������������ 441

Using Core Data������������������������������������������������������������������������������������������������������������ 447


Entities and Managed Objects������������������������������������������������������������������������������������������������������������ 448
The Core Data Application������������������������������������������������������������������������������������������������������������������ 451
Modifying the AppDelegate.swift File������������������������������������������������������������������������������������������������� 456

Summary���������������������������������������������������������������������������������������������������������������������� 463

xiv
■ Contents


■Chapter 14: Graphics and Drawing�������������������������������������������������������������������� 465
Quartz 2D��������������������������������������������������������������������������������������������������������������������� 466
The Quartz 2D Approach to Drawing���������������������������������������������������������������������������� 466
Quartz 2D’s Graphics Contexts����������������������������������������������������������������������������������������������������������� 466
The Coordinate System���������������������������������������������������������������������������������������������������������������������� 467
Specifying Colors�������������������������������������������������������������������������������������������������������������������������������� 470
Drawing Images in Context����������������������������������������������������������������������������������������������������������������� 471
Drawing Shapes: Polygons, Lines, and Curves����������������������������������������������������������������������������������� 472
Quartz 2D Tool Sampler: Patterns, Gradients, and Dash Patterns������������������������������������������������������� 472

The QuartzFun Application������������������������������������������������������������������������������������������� 474


Creating the QuartzFun Application���������������������������������������������������������������������������������������������������� 474
Adding Quartz 2D Drawing Code�������������������������������������������������������������������������������������������������������� 482
Optimizing the QuartzFun Application������������������������������������������������������������������������������������������������ 487

Summary���������������������������������������������������������������������������������������������������������������������� 490

■Appendix A: An Introduction to Swift���������������������������������������������������������������� 491
Swift Basics����������������������������������������������������������������������������������������������������������������� 491
Playgrounds, Comments, Variables, and Constants���������������������������������������������������������������������������� 493
Predefined Types, Operators, and Control Statements����������������������������������������������������������������������� 496
Arrays, Ranges, and Dictionaries�������������������������������������������������������������������������������������������������������� 507
Optionals��������������������������������������������������������������������������������������������������������������������������������������������� 512
Control Statements����������������������������������������������������������������������������������������������������������������������������� 517
Functions and Closures���������������������������������������������������������������������������������������������������������������������� 522
Error Handling������������������������������������������������������������������������������������������������������������������������������������� 527
Classes and Structures����������������������������������������������������������������������������������������������������������������������� 533
Structures������������������������������������������������������������������������������������������������������������������������������������������� 533
Classes����������������������������������������������������������������������������������������������������������������������������������������������� 535
Properties������������������������������������������������������������������������������������������������������������������������������������������� 536
Methods���������������������������������������������������������������������������������������������������������������������������������������������� 539
Optional Chaining������������������������������������������������������������������������������������������������������������������������������� 540

xv
■ Contents

Subclassing and Inheritance�������������������������������������������������������������������������������������������������������������� 541


Protocols��������������������������������������������������������������������������������������������������������������������������������������������� 544
Extensions������������������������������������������������������������������������������������������������������������������������������������������ 545

Summary���������������������������������������������������������������������������������������������������������������������� 546

Index��������������������������������������������������������������������������������������������������������������������� 547

xvi
Other documents randomly have
different content
KARAVAANI

Tien lapsia olemme, matkaan syntyneitä, karavaani-heimoa


retkellä elämän. Ja ei tule koskaan loppua matkallemme,
vaelsimmepa auringon alla tai keskellä pimeän.

Yli ikuisuuden hietikon valkenevat


monenkaltaiset päivät, paahtein ja keitahin;
me nauramme, juomme ja suutelemme — taikka
pedot tuskan saartavat meitä hampain nälkäisin.

Mut eteenpäin, ohi kaikkien tuokioiden


karavaania vie iankaikkinen kaipaus.
Ei, ei tule koskaan loppua matkallemme,
ja sen määränä lienee vain jokin kangastus.

Tosin loppuvat kerran askelet vaeltajilta,


kukin vuorostaan on nukkuva syliin maan.
Mut katso: kaikki he kuitenkin mukana ovat,
nekin, joiden tomu on uupunut vaeltamaan.

Mikä parhainta on, mikä syntyi rakkaudesta,


mikä meiss' oli elämästä ja elämää,
tomustamme se erkanee ja ikuisesti
karavaani-heimon sielussa hengittää.

Näin uusia polvia entiset saattelevat —


tie, etsintä niillä on kaikilla yhteinen,
näyt yhteiset, sama kaipaus kauniimmasta
ja matkasauvana Kohtalo ikuinen.

Tien lapsia olemme, matkaan syntyneitä, karavaani-


heimoa, sieluissa kaipaus, ja onnellisin on, kuka kauneinta
uskoo, polunnäyttäjänä kellä on kangastus.

(Erästä juhlaa varten).

DE PROFUNDIS
NÄKY KADULTA

He tulevat vastaani iltaruskon aikaan,


he tulevat kovasta työstä, he tulevat tehtaistaan.
Noki niinkuin naamio kasvoilla; siinä on vakoja hien.
Ja kaikkien selkä on painunut kumaraan.

Niin, kumarassa he käyvät, niinkuin kaikilla heillä


jokin näkymätön, raskas taakka ois.
Ja heillä on suuret, riippuvat, känsäiset kädet.
Ne nyt ovat tyhjät, ne antoivat voimansa pois.

Ja heillä on kaikilla huulet tiukasti kiinni.


Ja silmät hiipuvat, hiipuvat liedessään;
savun, työpajan sauhun viipyvän näissä vielä
kuin himmeän, liikkumattoman kaihen nään.

He tulevat vastaani iltaruskon aikaan,


he tulevat työstä, he menevät jonnekin, pois —
ja kumarassa he käyvät, niinkuin kaikilla heillä
jokin näkymätön, raskas taakka ois.

Ja nyt syvään tunnen sen: olen yksi heistä,


lain alla ankaran: voimanne uupukoon!
— Työn lasten heimon aikojen alusta loppuun
kivin kuormitettuna kulkevan nähnyt oon.
ONNELLINEN

Oli kadulla ajettu lapsen yli. Hänen ruumiinsa virui vielä


maassa avosilmin kuollein ja huulilla verta — soma poikanen
mekossa ruudukkaassa.

Oli kuin olis hiljaa värähdellyt


tuo hento nilkka ja kalvas ranne.
Katukivillä, vieressä ruhjotun rinnan
oli pieni sauva ja leikkivanne.

Tuli eräs nainen ja pyörtyi — äiti.


Me kaikki katsoimme hiljaisina.
Joku kyynel vierähti katukiviin —
ne kiilsivät sateesta niljaisina.

Menin pois ja nyyhkytin: "Onnellinen!


Hän on lähtenyt puhtaalla lapsenmiellä
sukupolvien Via Appialta."
— Oman mustan varjoni näin mä tiellä.
PAENNUT LINTU

Paennut lintu, povessain se asui kauan, pesässään. Ei sitä


nähnyt yksikään, sen tunsin minä vain.

Se etäisille retkilleen
läks siivin kuulumattomin,
niin häikäisevän valkoisin —
ja palas pesälleen.

Ja takaa meren kymmenen,


kedoilta Onnellisten Maan
toi hunajaa se nokassaan
ja mettä kukkien.

Jok' ainut kerta, päällä maan


kun janosin ja nälkää näin,
se ruokki minun sydäntäin
kuin emo poikastaan.

Ja uuvuttaissa tuskan muun


se kantoi lehvää öljypuun.
Se lensi eikä levännyt. —
Mut missä on se nyt?
— Paennut: kerran retkeltään
se palas luokse sydämen
ja — liudat haaskalintujen
se näki pesällään.

Ja hiljaisesti valittain
lens valkosiipi povestain,
se lensi eikä levännyt —
ja poissa on se nyt.
HÄMÄHÄKKI

On taloss' elämäni synkkä takapiha.


Siell' asustavat yököt himojen
ja mato epäilyn ja rupikonna, viha.
Ja öin ne piirittävät sydämen.

Tän' iltanakin ilon mettä juodaksensa


on mielen nurmelle se, perho, lentänyt.
Ja muistain ihanuutta maan ja sisartensa
se silmät ummisti ja — kuinkas kävi nyt:

kas paha ajatus jo hiipi kätköstänsä


— tuo musta hämähäkki ristiselkäinen —
ja tahmeaa ja lujaa surmanseittiänsä
se kutoi, kutoi yli sydämen.

Sen kietoi väistämätön tuhon piiri,


jost' ihme ainoastaan voisi pelastaa.
Tää pimeys on kissa, sydän hiiri.
On lohduttavaa ajatella kuolemaa —
RUNOILIJA

(Dan Anderssonin mukaan)

Näin runoilija olla, kadull' asustella, kun edes päätöstä ei


taistelulle näy, se on, on totta totisesti painiskella kanss' itse
saatanan — ties taivas, miten käy.

Se kysyy uhmaa, verta, kyyneleitä,


käyn nyrkkisille nyt, tuo aamu kuoleman,
mut koskaan murhett' ei, ei naisen-kyyneleitä —
en laps, en nainen ole, mies mä olenhan.

Täm' on kuin kalastaisi kaiken yötä


eik' einettäkään pyydykseensä sais,
kuin kulkis ulapalla yössä, myrskyn myötä,
ja maat' ei koskaan, koskaan pilkoittais.

Mut kerran kylmä ruumis olen ikuisesti, ja silloin minut tyyni


uni palkitkoon — oon silloin mies, mi painiskelun kesti, ja
mulla oikeus on olla, mikä oon.
NÄLKÄ

Syön ruumistas, sinun vertas juon, ja sen atrian ääreen on


ihana tulla. Joka päivä sun pöytääsi nälkäni tuon — joka
päivä on kuitenkin nälkä mulla.

Ja kun syön, yhä nälkäni kasvaa vain —


ja sun astias ei ole koskaan vajaa.
Oves luo, oves luota kuumeissain
käyn, hoippuen hornan ja taivaan rajaa.

Verijuotikas oon, jota liekki syö, olen sisältä suuri ja verinen


haava. Tule kerran vieläkin, syntien yö — ja sitten on tuomion
aamu saava!
AUTIO PELTO

Olen autio pelto. Ja autioks jään.


Lien tähkinyt toiveita, uskallusta.
Mut joka korren ja tähkäpään
söi niittymatojen liuta musta.

Olen autio pelto — ei: hautausmaa.


Luurangot nostavat turhaan kättään,
ne kuolivat kaikki syntymättään.
Olen täynnä huuhkajan ulvontaa.
DE PROFUNDIS

Viel' äsken uskoin: nyt on tieni avoin, ja vuoret odottavat


huipuin sinisin — Sisyphos, veljeni, sun varjos tavoin taas
suistuin rotkoihin.

Siis vietän elävältä peijaiseni,


ja, madot, atrialle olkaa kutsutut!
Te syökää, syökää, kunnes ydinteni
on mehu kuivunut!

— Mut sitten, aurinko, kun ylle pääni, tään rotkon ylle sytyt,
silmäs näkee mun, niin huutaa lahonneiden luiden ääni: Täss'
olen — poikas sun!
ERÄS IN MEMORIAM

Kuin pimeä kaivo sammakkoineen tahi armoton napapiirin yö,


joka aivoja painaa ja sielua syö, elo ympäröi sinut
kohtaloineen.

Öin korppeina, päivällä mustin ratsain


vihamieliset voimat sieluas
kovin piiritti. Eikä hän, odottamas,
avuks astunut pilven-, ei tulenpatsain.

Maass' ei ilon kyllyyttä juonut suus,


et tyhjissä taivaissa autuutta peri.
Sanat huulilles hyytyivät niinkuin veri.
Lumenvalkoiset koht' ovat hiekalla luus.

Näky viimeinen tämä silmihis piirtyi:


näit laulujen ruumiit paareillaan
ylen hentoina, valkeissa liinoissaan.
— Napinvalajan kauhaan sielus siirtyi.
PRONSSIA
SALTO MORTALE

On rakkaus ihmislasten kuin, pilvet, leikkinne ikuinen. Hekin


kulkevat kohtaloonsa sokeasti vyöryen.

Avaruuden äärillä vaikka


he kaukana toisistansa lie
kuin ukkospilveä kaksi,
niin kaipaus yhteen vie.

He tuskin tietävät, miksi,


mut toisiansa lähestyin
he syöksyvät vastatusten
tulenpolttavin syleilyin.

He leikkivät salamalla, ja heistä jäljelle tuhka jää. Mut


elämän korkein hetki on kuolemanleikki tää.
PITKÄPERJANTAI

Karitsa uhrataan meiss' — elon lunastaja — omaa syytä


vailla. Ja yli kaiken maan veremme sataa aamukasteen lailla.

Me oman sydämen revimme irti, ristille sen kohotamme


kuin käärmeen vaskisen, he että eläisivät — kuoltuamme.
PRONSSIA

Olin ihminen ennen, verta ja luuta ja lihaa. Sydämelläni ruokin


rakkautta ja vihaa.

Elon silmillä nähdä


saatoin, ja korvilla kuulla,
käsin tarttua käteen,
nauraa ja itkeä suulla —

kunis Kohtalo kerran sauvalla koski — — ja siksi ovat


kasvoni tulleet iäti-paatisiksi.

Kova, ankara pronssi


sieluni muotoa peittää.
Nyi en yltäni koskaan
voi sitä kaapua heittää.

Kuva en ole kuollut:


uumenissani sykkää
sydän liekehtiväinen —
mutta sen tuska on mykkää.
Erämaass' olen sfinksi; luonani leijonat huhuu. Tuhat vuotta
menköön — ja sitten suu kuvan pronssisen puhuu.
YKSINÄINEN RATSASTAJA

Sa yksinäisten polkua ratsastat


pois heidän luotaan, jotk' ovat parvissaan.
Sun erillesi, irti täytyy
yhteisen hulluuden valtateistä.

Kas, mikään liitä ei sua ihmisiin:


sä lihaa heidän et ole lihastaan,
et luuta ole heidän luustaan,
sukua toista on sielu sulla —

ah, yksinäisten pilvien sukua


ja petolintuin, vauhkojen ratsujen —
ei siinä sukupuussa kukaan
veljeä tunne, ei kantajaansa.

Ei sulla ole maata, ei taivasta,


vain yksinäisten tie, jota nelistää
suin verisin ja huuruavin
ratsusi, kuolema kavioissaan.

Et rakkautta tunne, sen tallasit.


On suudelmia sullakin, tietäkööt:
kas, yksinäinen ratsastaja
suutelee hurmeisen miekan terää.

Ja sinä näet, kuinka he kuolevat


mies miehen jälkeen, ihmiset parvissaan
he ojentavat käsiänsä
huutaen taivasten Aaveen nimen.

Sä tietämisen puust' otit hedelmän,


jota kateellinen enkeli vartioi:
nyt olet vapaa etkä kuole,
jalkasi polkivat käärmeen päätä.

Ja yli ajan, kuoleman hartiain


jo nelistää sun ratsusi pilviin päin,
ja panssarisi kultaa nuolee
auringon silmien kuuma liekki.

You might also like