0% found this document useful (0 votes)
3 views83 pages

Beginning Xcode Swift 3 Edition Edition Knott Download

The document is a promotional and introductory overview of the book 'Beginning Xcode Swift 3 Edition' by Matthew Knott, which aims to teach readers how to develop applications using Xcode and the Swift programming language. It outlines the contents of the book, including chapters on getting acquainted with Xcode, diving deeper into its features, and preparing applications for release. The book is designed for those with some familiarity with Cocoa Touch and object-oriented programming, and it emphasizes the power and utility of Xcode as a development tool for iOS and OS X applications.

Uploaded by

kumkelutch5y
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
0% found this document useful (0 votes)
3 views83 pages

Beginning Xcode Swift 3 Edition Edition Knott Download

The document is a promotional and introductory overview of the book 'Beginning Xcode Swift 3 Edition' by Matthew Knott, which aims to teach readers how to develop applications using Xcode and the Swift programming language. It outlines the contents of the book, including chapters on getting acquainted with Xcode, diving deeper into its features, and preparing applications for release. The book is designed for those with some familiarity with Cocoa Touch and object-oriented programming, and it emphasizes the power and utility of Xcode as a development tool for iOS and OS X applications.

Uploaded by

kumkelutch5y
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/ 83

Beginning Xcode Swift 3 Edition Edition Knott

download

https://ebookgate.com/product/beginning-xcode-swift-3-edition-
edition-knott/

Get Instant Ebook Downloads – Browse at https://ebookgate.com


Get Your Digital Files Instantly: PDF, ePub, MOBI and More
Quick Digital Downloads: PDF, ePub, MOBI and Other Formats

Beginning Xcode Swift Edition 2nd Edition Matthew Knott

https://ebookgate.com/product/beginning-xcode-swift-edition-2nd-
edition-matthew-knott/

Beginning Xcode 1st Edition Bucanek

https://ebookgate.com/product/beginning-xcode-1st-edition-
bucanek/

Beginning IOS Storyboarding with Xcode Lewis

https://ebookgate.com/product/beginning-ios-storyboarding-with-
xcode-lewis/

Learning Swift 3 Early release 3rd Edition Jonathan


Manning

https://ebookgate.com/product/learning-swift-3-early-release-3rd-
edition-jonathan-manning/
iOS 12 App Development Essentials Learn to Develop iOS
12 Apps with Xcode 10 and Swift 4 Neil Smyth

https://ebookgate.com/product/ios-12-app-development-essentials-
learn-to-develop-ios-12-apps-with-xcode-10-and-swift-4-neil-
smyth/

Beginning iPhone Development With Swift 2 Exploring the


iOS SDK 2nd Edition David Mark

https://ebookgate.com/product/beginning-iphone-development-with-
swift-2-exploring-the-ios-sdk-2nd-edition-david-mark/

Beginning WordPress 3 1st Edition Stephanie Leary

https://ebookgate.com/product/beginning-wordpress-3-1st-edition-
stephanie-leary/

Beginning Google Maps API 3 1st Edition Gabriel


Svennerberg

https://ebookgate.com/product/beginning-google-maps-api-3-1st-
edition-gabriel-svennerberg/

Beginning C 3 0 an introduction to object oriented


programming 1st Edition Jack Purdum

https://ebookgate.com/product/beginning-c-3-0-an-introduction-to-
object-oriented-programming-1st-edition-jack-purdum/
I nc
Xc
Learn to build powerful iPhone and iPad apps

lu
de
od
using Xcode 6 and Swift

s
e
6
Beginning

Xcode SWIFT EDITION


Matthew Knott
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
About the Author ................................................................................................................ xv
About the Technical Reviewer .......................................................................................... xvii
Acknowledgments ............................................................................................................. xix
Introduction ....................................................................................................................... xxi

N Part 1: Getting Acquainted ........................................................................... 1


N Chapter 1: Welcome to Xcode............................................................................................3
N Chapter 2: Diving Right In ...............................................................................................17
N Chapter 3: Project Templates and Getting Around...........................................................43
N Chapter 4: Building Interfaces .........................................................................................75
N Chapter 5: Getting Help and Code Completion ................................................................... 119
N Chapter 6: Constraints ...................................................................................................143

N Part 2: Diving Deeper ................................................................................ 179


N Chapter 7: Storyboards..................................................................................................181
N Chapter 8: Table and Collection Views ..........................................................................225
N Chapter 9: Frameworks, Libraries, and Targets.............................................................287

v
vi Contents at a Glance

N Chapter 10: Advanced Editing .......................................................................................315


N Chapter 11: Debugging and Analysis ............................................................................343

N Part 3: Final Preparations and Releasing ................................................. 377


N Chapter 12: Version Control with Git .............................................................................379
N Chapter 13: Localization ................................................................................................413
N Chapter 14: Devices and the Organizer .........................................................................455
N Chapter 15: Building, Sharing, and Distributing Applications .......................................483

Index .................................................................................................................................525
Introduction
Welcome to Beginning Xcode, the book that aims to give you all the knowledge to start writing
applications using what is probably the most powerful integrated development environment (IDE)
ever. And that IDE is free.
As with many Apple products, Xcode has simplicity and ease of use in abundance, but don’t be
fooled: the shiny exterior masks a workhorse of a tool, incredibly powerful and with an extensive set
of integrated tools for every eventuality. Xcode is the development environment that all other IDEs
have a poster of pinned to their bedroom walls.
Xcode hasn’t always been this shining Rock God of awesomeness; it used to be a sorry band of
ragtag applications. When I first picked up Xcode 3 in 2007, I remember going through a multitude
of different applications to perform varying tasks, such as the very basic Interface Builder, and
finding out how to adapt my knowledge of C into Objective-C. Back then, what I really wanted was
something that showed me how to get the most out of Xcode and could give me the understanding I
needed to get going with the hundreds of app ideas I had in my head.
Fast-forward seven years, and both Xcode and I have come a long way. I feel as if I’ve gone from
being a kid, bumping my leaky paddleboat aimlessly around a lake, to being a handsome sea
captain at the prow of my vessel, gazing forth as I slice through choppy waves with grace and ease.
Well, aside from the handsome part, the analogy is a good one. Xcode 6is more complete and
powerful than ever before: it’s an integrated product that puts in your hands the same power used by
the developers at Apple who write the apps found in iOS and Mac OS X.
I’ve always had a mixed experience with development books and have often been sick of building
the same old apps again and again. In this book I’ve aimed to take you through almost every facet of
Xcode, helping you to understand the capabilities of each of the key areas as you build a number of
cool and exciting projects along the way and come to grips with the new Swift programing language.
By the end of the book, you should be ready to turn the ideas in your head into reality—and I can’t
wait to see what that looks like.

xxi
Part 1
Getting Acquainted
Chapter 1
Welcome to Xcode
Apple provides Xcode to developers to help them create applications for Macs, iPhones, and
iPads (OS X and iOS). Xcode was used to create many of your favorite iOS and OS X applications.
Arguably, without such a powerful, refined, integrated development environment (IDE), the thriving
ecosystem that is the App Store would not exist as it does today.

What Is Xcode?
All developers, regardless of the platform for which they’re developing, use an array of tools to take
an application from an idea to something that is readily available to millions of people. Apple is no
exception, and it provides a cultured, powerful, and polished set of development tools. These tools are
brought together within one application called Xcode. Xcode provides everything you need to create,
test, deploy, and distribute applications for iOS and OS X. With the release of iOS 8 and Xcode 6,
Apple has radically overhauled its toolset and created powerful new technologies that aid developers
in making the process of creating an application fun and rewarding; in addition to that, and for the first
time since Xcode was released, it now supports a brand-new proprietary programming language
called Swift.
The purpose of this book is to guide you in becoming familiar with Xcode 6, in the hope that you’ll
become more confident and embrace it to create amazing, innovative new applications for iOS and
OS X. As in many other technical books, as you progress through each chapter, you build on your
knowledge and systematically create a number of iOS applications.
Although Xcode was created primarily for developers working on iOS and OS X applications, it’s also
great if you work with other languages such as C, Java, and C++, among others. Xcode has a long,
interesting history of releases, some having a very good reception and some less so. First released in
2003, Xcode has had six major releases and seen a couple of major interface overhauls. After more
than 10 years of active development, it’s safe to say that Xcode is incredibly powerful and a leading
professional set of development tools. What’s more, Xcode is available to developers at absolutely
no cost; all you need is an iTunes account, and you’re good to go.

3
4 CHAPTER 1: Welcome to Xcode

What Is Swift?
Before WWDC 2014, Apple’s World Wide Developer conference, if you created a project in Xcode,
the code that was added behind the scenes was written in a programming language called
Objective-C. At WWDC14, Apple unveiled a brand-new and highly streamlined programming
language called Swift. This new language greatly reduced the amount of code needed to achieve
equivalent outcomes in Objective-C. Swift is far less rigid than Objective-C and is very similar in
structure to Python, a move that will no doubt encourage more developers into what is already one
of the most accessible development ecosystems in existence today.
To make Swift even more appealing, Apple also introduced a new piece of software called Swift
Playground with Xcode 6. Playground gives you the ability to experiment with snippets of code
to see the result without putting them into your application, meaning you have a code sandbox in
which to try things such as loops or regular expressions and instantly see the result.
The purpose of this book is to teach you how to create apps using Swift in Xcode 6—it isn’t a definitive
Swift language guide. Apple, however, has released a free book that is a definitive guide to the Swift
programming language and has made it available via iBooks. Search iBooks for Swift programming, or
visit https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11.

Why Choose Xcode?


If you have experience developing for other platforms, then you probably want to know what makes
Xcode so great. The main thing is that there’s simply no other IDE like it. It’s unique in the sense
that Apple has created it to be simple, yet at the same time it masks a powerful interior. You have
the ability to work with a range of technologies, and you also have a phenomenal developer toolkit
at your disposal. Xcode contains everything you could need: an intuitive code editor, advanced
debugging, seamless interface-editing features, and the benefit of being constantly updated and
maintained by Apple.
In addition, using Xcode is arguably the only practical way to develop applications for iOS and
OS X that can truly be called native. Xcode is what Apple itself uses to produce its own innovative
software, which is used by millions of people.
Aside from Xcode, it’s hard to find a commendable alternative if you’d like to develop native iOS or
OS X applications. Of course, there are third-party services and tools; but when using these you may
often find yourself battling inconsistencies and a lack of compatibility rather than focusing on what’s
really important: creating great apps (and enjoying doing so). The purpose of Xcode isn’t to simply
be an IDE: it also helps and guides you on your quest to create something that has the potential to
reach a staggeringly large audience. For that reason, Xcode is a fantastic choice.

Prior Assumptions
Before you dive in and start reading this book, it’s assumed that you have at least some familiarity
with developing for Cocoa Touch and are familiar with the concepts of object-oriented programming.
This book is geared toward those developing for iOS; however, it’s possible to get a lot out of this
book if you’re developing OS X applications, because many of the principles presented can be
applied to either platform.
CHAPTER 1: Welcome to Xcode 5

It’s assumed that you are using a Mac and are preferably running the latest version of OS X. Unlike
the Objective-C based equivalent of this title, it’s absolutely necessary that you run the latest version
of Xcode. This book is written specifically for Xcode 6 and Swift, and the technology doesn’t exist
in previous versions of the IDE. There is a common misconception that you need the greatest and
latest “souped-up” Mac, but many previous-generation iMacs, MacBooks, Mac Minis and Mac Pros
will work just fine.
It’s also assumed that you know how to operate your Mac and how to use OS X. For example,
you need to know how to use the Finder, save files, and so forth—all the basics. Finally, a couple
of the chapters present scenarios in which an active Internet connection is required, and some
features of Xcode perform better when you’re connected. Additionally, some later chapters require a
physical device and a paid developer account to complete, but for the most part you can use an iOS
simulator to run your apps.
It’s also worth mentioning that the purpose of this book is not to teach you how to create
applications for iOS or teach you how to program in Swift or Objective-C; the purpose of this book
is to get you up and running with Xcode so you can apply your current knowledge of Swift and OS
X/iOS development and use the latest version of Xcode to its full potential to enable you to work
more productively and create fantastic applications.
A final note, this book was written at a time of transition, when OS X 10.10 Yosemite was not
released. All screenshots are taken using OS X 10.9. Despite being the same version of Xcode, there
are differences in icons between the versions, and when this happens I call it out.

What’s Covered in This Book


Part 1: Getting Acquainted
Chapter 1: This chapter starts you on your journey into the world of Xcode and
explains how to get Xcode onto your machine and prepare it for first use. You
are shown how to sign up as an Apple developer, and you get a look at the
wealth of resources provided by Apple to iOS and OS X developers.
Chapter 2: Here, you start a project and get the ball rolling in terms of becoming
familiar with Xcode. You learn the basics of how to create projects and build
applications, along with how to get around in Xcode.
Chapter 3: Next, the focus shifts to how to choose from Xcode’s different project
templates. You also get a guided tour around Xcode’s interface along with an
introduction to many of the menus, inspectors, and panels you should use to
work efficiently.
Chapter 4: This chapter focuses solely on how to design your interfaces using
Xcode’s built-in interface editor, Interface Builder. It gives you an in-depth look at
the libraries and inspectors available.
6 CHAPTER 1: Welcome to Xcode

Chapter 5: Next, you’re shown how to access the invaluable help resources that
are built right in to Xcode and also how to make the most of its intelligent
code-completion feature.
Chapter 6: Building on Chapter 4, you see the Auto Layout system and learn
how it works with constraints and the new size classes introduced in Xcode 6 to
create a single layout for any device.

Part 2: Diving Deeper


Chapter 7: This chapter shows you how to use a key feature for rapid
development in Xcode: Storyboards. You see how it can add a certain degree of
logic to how you display and push views in your application.
Chapter 8: This chapter explains how Xcode makes it easy to populate and
create table and collection views, with the addition of how to customize their
appearance and functionality.
Chapter 9: Here you learn how to add features to your application by adding
frameworks and libraries. You also learn how to create a different version of your
application in the same project with targets.
Chapter 10: This chapter shows you how to add your own personal touches to
Xcode in terms of editing code. In particular, the code editor is the focus of this
chapter, and you see how to work more productively and how to customize its
appearance and behavior to suit your tastes and requirements.
Chapter 11: This chapter presents the idea of making your application run more
efficiently and faster. This is done by looking at the range of different tools and
methods included in Xcode: for example, using breakpoints to step through your
code systematically. You also learn about the Swift Playground for prototyping
and testing your code.

Part 3: Final Preparations and Releasing


Chapter 12: Here you learn how you can protect your code and work effectively
as a team by using Git, Xcode’s integrated version control software.
Chapter 13: This chapter examines the idea of localization and how to use
Xcode to accurately support multiple languages in your app.
Chapter 14: This chapter looks at the Organizer, what it’s for, how to navigate
around in it, and how to keep your developer assets in good standing order.
Chapter 15: To conclude, you make final touches to your application, build it for
release, and then share it either as an IPA file or via the App Store using either
Application Loader or the Organizer.
CHAPTER 1: Welcome to Xcode 7

Getting and Installing Xcode


Before you can download Xcode, there are a couple of things you need to do. You need an iTunes
account (or an Apple ID) that allows you to download content from the Mac App Store; then you’re
good to go. If you don’t have an Apple ID, you can sign up for one at no cost at http://appleid.
apple.com. This book is written for Xcode 6, and to run it you also need a Mac that’s running the
latest version of OS X or at least OS X 10.9.3.
Once you’re equipped with an Apple ID and a Mac running OS X 10.9.3+, you can begin
downloading Xcode. As with many other Mac apps, you simply download it from the Mac App Store
at no additional cost. Open the App Store on your Mac, select Categories from the top bar of the
window, and then click the Developer Tools category. Usually, you can find Xcode right away either
at the top of the window or in the sidebar on the right displaying the top free apps. Alternatively,
you can use the Search bar at top right and enter “xcode”. Xcode’s icon is a hammer over an “A”
blueprint, as shown in Figure 1-1.

Figure 1-1. Xcode in the Mac App Store

Note If you don’t have access to the latest version of OS X or are running an older version that isn’t
supported, you can download previous versions of Xcode from the iOS Dev Center, but for this you need
to have a registered Apple developer account. This is explained later. However, this book covers the latest
version of Xcode (which is 6.0 at the time of writing).

Select the icon, and you’re taken to Xcode’s App Store page. Here you can view all the features of
Xcode along with the latest additions to the current version of Xcode (at the time of writing, this is
6.0) and also preview some screenshots of Xcode. To download Xcode, click the gray Free button
and enter your Apple ID e-mail address and password, and your download will commence. Xcode
is about 2.4 GB, so you can go and make some coffee while you wait for the download to finish, as
shown in Figure 1-2.
8 CHAPTER 1: Welcome to Xcode

Figure 1-2. Xcode in the Mac App Store, ready to be downloaded

With Xcode downloaded, open it from your Applications folder. You’re prompted to install some
additional packages: click Install, and enter your user password. This installation should take a
matter of seconds, as shown in Figure 1-3.
CHAPTER 1: Welcome to Xcode 9

Figure 1-3. Installing additional tools required by Xcode

Firing Up Xcode
Once you’ve successfully downloaded and installed the additional components, you can begin using
Xcode. In Chapter 2, you create your first project and become familiar with the basic areas of Xcode;
but for now, just make sure everything is in good order so you don’t encounter any problems later.
When you first launch Xcode, you’re presented with a Welcome splash screen. From here you can
create a new project, connect to an external repository, open documentation, visit Apple’s developer
portal, and also browse a list of your recent projects. For some, this screen causes irritation—you
can prevent it from appearing each time you open Xcode by simply checking or unchecking the
Show This Window When Xcode Launches box, as shown in Figure 1-4.
10 CHAPTER 1: Welcome to Xcode

Figure 1-4. Xcode’s Welcome window, which is displayed optionally each time you open Xcode

To create a new project, you can click the Create A New Xcode Project button on the Welcome
screen or navigate to File ° New ° Project, where you’re presented with a range of templates
provided by default by Xcode.
If you have gotten to this point, it’s safe to assume that you’ve successfully installed Xcode and that
you’re ready to start creating projects. However, let’s save this for a deeper explanation in Chapter 2
and for now look at the variety of resources provided to developers by Apple.

Apple’s Resources for Developers


At this point, you have Xcode downloaded to your machine, and you’ve fired it up to make sure
it runs. If there’s one thing that makes Apple stand out from its competitors, it’s the wealth of
knowledge, resources, and tools that are made just for developers. There are thousands of
documents, thousands of samples to download, and dozens upon dozens of videos you can watch.
Currently you have Xcode installed, but that alone isn’t going to make you a great developer of iOS
and OS X applications. You also need to use the vast library provided by Apple. To gain access to
Apple’s resources, I urge you to sign up as a registered Apple developer. To do this, all you need is
an Apple ID; you can create a new one or use the same ID you use to download content from iTunes
or the App Store.
CHAPTER 1: Welcome to Xcode 11

First, head over to http://developer.apple.com. This is the central web site for Apple developers.
On the home page of the site, click iOS Dev Center. The iOS Dev Center is the central location
for all the resources provided to those who create iPhone, iPad, and iPod Touch applications, as
shown in Figure 1-5.

Figure 1-5. The iOS Dev Center—home of Apple resources for iOS developers

You aren’t required to sign up in order to gain access to many of the resources, including the
Developer Library, an overwhelming wealth of example source code, release notes, and many more
things. You can happily browse through the iOS Dev Center right now.
However, there are great advantages to signing up as a registered Apple developer, and it will
become essential at some point if you’re planning to distribute applications via the App Store.
Therefore, it’s a good idea to sign up right from the start. To begin the process of signing up,
click the Register For Free text just below the Sign In button; alternatively, you can visit
http://developer.apple.com/programs/register/. In order to sign up, you need a valid Apple
ID; if you don’t have one or would like to dedicate an Apple ID to your developer account, create
a new one (don’t worry, none of your purchases or downloaded content from the App Store or
iTunes Store will be affected if you use your current one).
Once you’re happy with your Apple ID, go to http://developer.apple.com/programs/register/
and sign up for an account. In order to complete the process of signing up, you need to create a
personal and professional profile; you can change these at any time if you need to.
12 CHAPTER 1: Welcome to Xcode

You’re required to complete your professional profile by telling Apple any previous platforms
you’ve developed for along with your primary markets and experience with Apple’s platforms.
This information is used by Apple to get an idea of the spectrum of people who are signing up as
developers. Again, once you have completed this, click the Next button. Also, it’s important to note
that what you select when updating your professional profile doesn’t bind you to anything, and that
you’re able to develop and release applications to any of the App Store’s markets. Furthermore, you
can, if needed, make any amendments to your professional profile (and personal profile, for that
matter) after you’ve signed up, as shown in Figure 1-6.

Figure 1-6. Completing your developer professional profile

Finally, you reach the tiresome agreement that comes with many of Apple’s products; read it, click
to agree, and then continue with the process. To finish, all you need to do is verify that the e-mail
address supplied is valid; you do this by opening the e-mail sent to you by Apple and entering the
verification code contained within.
CHAPTER 1: Welcome to Xcode 13

The Dev Center


As mentioned previously, Apple really does like to take care of its developers. As a developer, your
first port of call is the Developer Library, because it houses most of the resources provided by
Apple. If you select the iOS Developer Library link under Documentation And Videos, you’re taken
to an invaluable section of Apple’s developer web site. The Developer Library is a simple and
straightforward site: simply use the links on the left to navigate around and to filter the results.
You can either search for specific keywords or sort the results using one of the column titles, as
shown in Figure 1-7.

Figure 1-7. The iOS Developer Library

In addition to the iOS Developer Library, you also have access to an array of getting-started
videos that explain core Objective-C and Cocoa Touch concepts. You’re also given access to
a direct link to the latest version of Xcode on the Mac App Store and the ability to download
previous versions of Xcode if you’re not running the latest version of OS X or would like to target
older versions of iOS.
14 CHAPTER 1: Welcome to Xcode

Your Developer Account


Currently, your level of membership is that of a free account, meaning you have access to a
staggeringly vast amount of resources but not to all the resources you need if you’re planning to
release applications to the App Store. Although this isn’t necessary at this point, it’s a good idea
to sign up as a paid developer, because doing so gives you access to the Apple developer forums,
prerelease versions of iOS before they’re available to the public, prerelease versions of Xcode, the
ability to test your applications on your iOS devices, and, of course, the ability to submit applications
to the iOS App Store. The cost of signing up at the time of the writing of this book is $99 per year,
and it’s required for some of the concepts presented toward the end of the book.
As mentioned previously, it isn’t necessary to sign up this instant, but it’s recommended that you do so at
some point. To sign up for a paid account, visit https://developer.apple.com/programs/ios/ and click
the Enroll Now button. You’re then guided through the process of signing up; it’s straightforward if
you follow the steps onscreen, as shown in Figure 1-8.

Figure 1-8. Choosing between an individual or a company developer account


CHAPTER 1: Welcome to Xcode 15

It’s useful to note that when you’re prompted to choose between an individual or company account,
if you’re planning to operate under a name other than your own, you have to register as an official
company (this is verified by Apple) and then acquire what’s called a Data Universal Numbering
System (DUNS) number that uniquely identifies your company; this takes around 7 days to process,
so plan ahead. If selling applications under your own name suffices, then go for the simpler option of
signing up as an individual; both accounts are essentially equal in terms of the resources you’re able
to access. This choice mainly determines the name with which you operate under on the App Store.
Don’t worry if you’re not ready to do this right now—it’s covered in detail later in the book when it
becomes essential if you’re planning to release applications on the App Store (free or paid) or want
to test your apps on an iOS device. Chapter 14 looks at using Provisioning Profiles and deployment
onto actual iOS devices as opposed to the virtual iOS Simulator, so you then need access to a paid
developer account.

Source Code
I strongly recommend that one of the first things you do is to go to the Apress web site for this
book and download the entire source code. Either search for the book at www.apress.com or go
directly to www.apress.com/9781430250043. When you get to the page for this book, scroll down
until you see the section of the web site with four tabs, the third of which is Source
Code/Downloads, as shown in Figure 1-9.

Figure 1-9. The source code download page for this book

Additional Resources
In addition to Apple’s own resources, an extensive amount of third-party resources are available if
you ever have a burning question or get stuck somewhere:

Forums: Forums are a great way to ask questions, learn from other people’s
questions, and help other people. In particular, Stack Overflow
(http://stackoverflow.com/) has been invaluable to the entire developer
spectrum for years and has a vibrant, active collection of iOS developers.
There are also the Apple developer forums, which are available to those
with a paid Apple developer account.
16 CHAPTER 1: Welcome to Xcode

Mailing lists: There’s a handy Xcode users mailing list that I’d recommend you
subscribe to and periodically check. Many other developers, including myself,
participate in answering questions relating to Xcode. You can subscribe at
https://lists.apple.com/mailman/listinfo/xcode-users.
Xcode Overview: Apple provides a handy user’s guide that’s always being updated
to accompany the latest release of Xcode, so it’s a good idea to refer to it when
there’s a new update or if you’d like to follow up on something. It’s available at
https://developer.apple.com/library/ios/documentation/ToolsLanguages/
Conceptual/Xcode_Overview. Similarly, it’s also handy to glance over the latest
release notes when Xcode is updated. These are available at https://developer.
apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode.
Search engines: It’s easy to underestimate the power of a simple Google search
(and it’s apparent many people on online forums don’t have access to them).
It can save you a lot of time, because someone, somewhere, at some point has
undoubtedly had the same question you do—all you need to do is find where they
asked it!
Videos: If you type “Xcode” into iTunes U search, you’ll find a couple of good
university courses that focus not only on Xcode but also on iOS development in
general. Similarly, type “Xcode” into YouTube search, and you’ll be amazed at
what you can learn from the short screencasts that have been uploaded.
Contact me: I am happy to field questions via email at [email protected] or via
my blog at www.mattknott.com.

Summary
In this chapter, you have:
Successfully downloaded and installed Xcode
Had a look around the iOS Dev Center and also looked at the resources
provided by Apple to aid developers
Signed up and registered as an Apple developer and become aware of the
option of signing up for a paid developer account
Chapter 2 explains how to create your first project and helps you become more familiar with Xcode’s
interface and basic concepts.
Chapter 2
Diving Right In
In Chapter 1, you downloaded Xcode, made sure it was correctly configured, signed up for a
developer account, and explored the wealth of resources provided by Apple to help you get started
with not only Xcode but also some of its fantastic new technologies. This chapter explains how
to create a working application using Xcode’s visual interface building tool (aptly named Interface
Builder) and its built-in code editor and then run the app on your machine.
As mentioned, as you progress through this book, the ultimate goal is not only to get a grip on the
latest and greatest version of Xcode but also, by the end of the book, to have walked you through
building a series of varied applications that give you many of the essential skills needed to go out
and start writing your own applications. The application you build in this chapter familiarizes yourself
with Xcode as a development environment before you start looking at sharing data between pieces
of your application in Chapter 3. For now, you develop a very simple application that has a custom
background color and a label, and you programmatically update the text in the label.
Be forewarned that in this chapter, a lot of the concepts are new and therefore require more
explanation to do them justice. As a result, on several occasions you’re told that later chapters revisit
many of the concepts presented. This is because the main goal of this chapter isn’t to turn you into
an Xcode pro, but rather to get you started and give you the confidence to believe that Xcode isn’t
as overwhelming as it may first appear. In Figure 2-1, you get a glimpse of the example application;
although simple, it will make you at least a little familiar with the workings of Xcode and help you to
understand that Xcode can help you produce a working application in next to no time.

17
18 CHAPTER 2: Diving Right In

Figure 2-1. The application you create in this chapter

Creating Your First Xcode Project


First you need to bring this project into existence. To do this, start by running the Xcode application,
and then click Create A New Xcode Project on the Welcome Screen or choose File ° New ° Project
(Ɖ+Shift+N). You’re presented with a new window asking what kind of project you’d like to create.
Apple provides, by default, a variety of different project templates for both OS X and iOS, each of
which is useful for getting started on different types of projects; Chapter 3 covers each of them in
more detail. Continue as follows:
1. Because you’re creating a basic one-view application, it seems appropriate
to choose Single View Application, which can be found in the Application
category under iOS on the left side of the dialog.
2. Once you’ve selected the Single View Application project template, click the
Next arrow in the bottom-right corner. Figure 2-2 shows the template screen.
CHAPTER 2: Diving Right In 19

Figure 2-2. The variety of different templates provided by Apple to help you get started with creating your app quickly

You need to specify a couple of things before you can actually get started. Once you select your
project template, a screen identical to that in Figure 2-3 is displayed. Following is a brief overview of
each of the values required to proceed—bear in mind that you learn more about the significance of
some of the values you enter here as the book progresses:
Product Name: What you would like to call your application. For example, if you
wanted to create an application called Chocolate Recipes, you’d specify the
Product Name to be something along the line of Chocolate Recipes. Although
not required, it’s generally good practice to omit any spaces and instead
capitalize each new word. The Product Name can be amended during the
development of your application, so you’re not obliged to stick with what you
specify; but regardless, the Product Name is a rather important detail that you’re
required to specify at this stage.
Organization Name: Required whether you’re working independently or you’re
part of a software development company. For now, your own name is adequate.
If you’re looking to submit an application to the App Store, it’s in your best
interest to specify the correct name; although not required, it’s recommended.
When you create a new file, your Organization Name automatically appears
along with copyright details at the top; that’s something to bear in mind if you
plan to work on a team or hand your project off to someone else.
Organization Identifier: Only really required if you’re planning to distribute your
application in some capacity. For example, to distribute an app via the App
Store, you’re required to specify an App ID along with a Bundle Identifier, which is
created by Xcode depending on what you input as your Organization Identifier. The
company identifier is written in the style of reverse domain name notation; my web
site, for example, is mattknott.com, so my Organization Identifier is com.mattknott.
20 CHAPTER 2: Diving Right In

Bundle Identifier: By default, a combination of the Organization Identifier and the


Product Name, to avoid confusion (I won’t focus on this too much right now).
You can’t edit this.
Language: Swift or Objective-C. For the first time, in Xcode 6, you get to choose
between two possible languages. This book covers the Swift programming
language, so please ensure that you select Swift for each example project.
Devices: The device you’d like your application to run on. This is possibly the
most straightforward part of getting up and running with your project. You have
three choices: iPhone, iPad, and Universal. The iPhone and iPad choices are
self-explanatory. A Universal application is one that is compatible with both the
iPhone (and iPod Touch) and iPad. Your selection here isn’t final, but it’s good to
make the right choice.
Use Core Data: Core Data is a large framework designed by Apple to simplify
and unify the methods for storing data in iOS. For example, if you wanted to
create a database for storing relational information in your application, you might
want to set up an SQLLite database. Core Data does all this for you and gives
you a simple interface to set up the tables, fields, and relationships.

Figure 2-3. Specifying the project’s details


CHAPTER 2: Diving Right In 21

Note If you’ve been using an older version of Xcode, you may have an annoying feeling that a field is
missing. In version 6 of Xcode, Apple decided to remove the ability to specify a Class Prefix value. The Class
Prefix was added to the start of every new class you created to help differentiate your classes from other
class files that might be imported into the project.

3. Now that you vaguely know what these values are for and what they
correspond to, you’re probably wondering what you should input to create
this project. As shown in Figure 2-3, type in HelloWorld as the Product
Name; input your own first and last name as your Organization Name;
use com.LASTNAME as your Organization Identifier (obviously change
LASTNAME to your actual last name), set the Language value to Swift if it
isn’t already, specify iPhone as the Device, and, finally, ensure that Use Core
Data isn’t selected.
4. Once you’ve made sure all your values are correct, click Next. You’re required
to save your project to disk.
5. When prompted to, use the familiar OS X dialog to find a location. Make sure
the box next to Source Control For This Project is unchecked, and then click
Create.

Note Git is a popular system used for version control and source-code management. You can integrate a
local Git repository with a web site such as GitHub or Bitbucket if you want to back up or share your code
online. If none of these things are familiar to you, Chapter 12 explains.

So, you’ve given Xcode all the relevant details and specified what kind of project you’re looking to
create. As a result, Xcode conveniently creates a basic, functioning application for you to use as a
starting point. The code that Xcode creates for you is just enough to get the application to run; it’s a
working, if slightly pointless, app that you can run right now if you like.
Choose Product ° Run (Ɖ+R), and you’ll find the application builds successfully and the iOS
Simulator pops up with the app running, as shown in Figure 2-4. It’s nothing spectacular, nor will it
reach the top 25 in the App Store anytime soon, but it’s a functioning application created by Xcode
with very little input from you. Return to Xcode, and click the Stop button in the top-left corner or
choose Product ° Stop (Ɖ+.).
22 CHAPTER 2: Diving Right In

Figure 2-4. The initial application created by Xcode

Tip With the high resolution of modern iOS devices, unless you have a top-of-the-range Mac, the simulator
may be too large for your computer screen. If this is the case, then with the simulator selected, choose
Window ° Scale ° 50% or use the key shortcut Ɖ+3. The Scale menu also gives you the option to go to
100% or 75% scale using Ɖ+1 or Ɖ+2, respectively.
CHAPTER 2: Diving Right In 23

The Project
In order to make the app a little more interesting than a simple white screen, you need to open some
files that Xcode created. As with previous versions of Xcode, the way in which it organizes your
project’s file is somewhat strange. Upon returning to Xcode, if you look to the left of the interface,
you should see what appears to be an arrangement of folders and files. These are the files that make
up your project (see Figure 2-5). This part of Xcode is called the Project Navigator. If you’re unable
to find it, choose View ° Navigators ° Show Project Navigator (Ɖ+ 1). It’s important to note that
when you create a folder in the Project Navigator, it doesn’t correspond to the structure in which the
files are saved in the Finder. The folders and organization of the Project Navigator are purely to help
you locate files in Xcode. If you create a folder in the Navigator, the same folder isn’t present in your
project when you browse in Finder. I revisit this when you add a file to your project later
in this chapter.

Figure 2-5. The Project Navigator


24 CHAPTER 2: Diving Right In

If you’ve used Xcode with Objective-C in the past, then you’ve almost certainly noted that when
using the Swift language, there are far fewer files in the project. The reason is that in Objective-C,
each class file is created with a header (.h) file and an implementation (.m) file. Swift combines all
class information into a single file.
With that in mind, select Main.storyboard from the Project Navigator. Xcode opens its built-in
graphical user interface (GUI) design tool, usually referred to as Interface Builder. Xcode 4.0
represented a major overhaul of Apple’s developer tools: Interface Builder, which was previously a
separate application, was conveniently integrated into Xcode, making it easy to switch between the
built-in code editor and interface design tool in a single application, as shown in Figure 2-6. One
warning is worth mentioning: the more you become familiar with Xcode, the more you may wish
for a larger screen!

Figure 2-6. Xcode’s built-in graphical interface designer


CHAPTER 2: Diving Right In 25

Designing the Interface


As noted previously, the app’s interface is neither exceptional nor revolutionary—in this chapter you
create something simple and straightforward. However, the basics presented here are echoed later
in the book in much more detail, and they’re the building blocks behind almost any app you could
want to build. To begin with, let’s look at the Attributes Inspector. You can find this by selecting the
fourth tab in the sidebar on the right side of Xcode’s interface; alternatively, you can choose
View ° Utilities ° Show Attributes Inspector (Ɖ+ƃ+4). The Attributes Inspector plays an important
role when it comes to layout and fine-tuning interface elements. Now follow these steps:
1. To change the background color of the application, first make sure the view is
selected by clicking the white area with an arrow pointing to it in Figure 2-6.
2. Under the View heading of the Attributes Inspector, select the color-picking
option for the Background attribute. Then use OS X’s default color picker
to choose a background color, as shown in Figure 2-7. In this example, I’ve
used the RGB sliders and chosen a background of Red: 181, Green: 218,
and Blue: 225, but you’re free to choose whichever colors you wish.

Figure 2-7. Changing the view’s background using the Attributes Inspector
26 CHAPTER 2: Diving Right In

3. Add a label to your view. To do this, open the Object Library (shown in
Figure 2-8), and drag a label object to your view. Generally, the Object
Library is right below the Attributes Inspector and accessible by selecting the
third tab; you can also access it via View ° Utilities ° Show Object Library
(Ƃ+Ɖ+ƃ+3).

Figure 2-8. The Object Library, filtered for “label”

4. With the library open, use the small search bar to search for “label”.

5. Once you’ve found the label object, drag it to your interface at the top of
the view, as shown in Figure 2-9. As you position the label near the top, the
Guides shown in Figure 2-9 appear, and the label snaps into place.
CHAPTER 2: Diving Right In 27

Figure 2-9. Dragging the label onto the view

Note Regardless of whether you’ve used Xcode before, you may wonder why you’re only seeing what
appears to be the top portion of the device’s screen. This is because of a new Auto Layout feature that Apple
introduced with Xcode 6 and iOS 8, called Size Classes. I explain in greater detail later in the book.

6. Select your new label, and use the handles to extend its size so it fills the
width of the View. Then double its height to accommodate a larger font size.
7. Set the Alignment attribute in the Attributes Inspector to Center.
8. Click the T symbol in the Font attribute to alter the font. Set Font to Custom,
Family to Avenir, and Size to 32, as shown in Figure 2-10.
28 CHAPTER 2: Diving Right In

Figure 2-10. The Attributes Inspector’s Font property

9. Because you have Size Classes enabled for this view, it’s essential to set a
couple of parameters called constraints that tell iOS how to position the label
in the view. I cover this in detail later in the book, but for now, locate the pin
icon in the bottom-right corner of the design area and click it. A popover
appears.
10. At the top of the pin popover, you see a square with an I bar on each side
followed by a numeric value. Click the top, left, and right I bars to highlight
them red, as shown in Figure 2-11. Then click the Add 3 Constraints button.

Figure 2-11. Setting some constraints for the label


CHAPTER 2: Diving Right In 29

At this stage, although you can use the Attributes Inspector to specify the text to be displayed in the
label, it’s important to realize that Xcode isn’t just about creating graphical interfaces. It also houses
a very powerful code editor. So, as you progress through this chapter, you update the contents of
your label programmatically as opposed to graphically.

Making Connections
Before you leave Interface Builder and move on to focus on Xcode’s code editor, let’s look at a
powerful feature that allows you to use both simultaneously. Open the Assistant Editor by selecting
the shirt-and-bowtie icon in the top-right corner of Xcode, as shown in Figure 2-12, or by selecting
View ° Assistant Editor ° Show Assistant Editor (Ɖ+ƃ+Return).

Figure 2-12. The button to select the Assistant Editor looks like a shirt with a bowtie

Opening the Assistant Editor splits your screen, with Interface Builder occupying the left frame and
the code editor occupying the right (unless you’ve customized this appearance, as I show you later
in the book). Before you continue, you need to make sure Xcode has opened the correct file. You
should be looking at a file called ViewController.swift: you can verify this by looking at the jump
bar just above the code, as shown in Figure 2-13. Continue as follows:
1. With both Interface Builder and the code displayed using the Assistant Editor,
click the label you added to your view in Interface Builder to highlight it.

Figure 2-13. The jump bar in the Assistant Editor shows which file is open
30 CHAPTER 2: Diving Right In

Now you’re going to create a variable called an outlet (IBOutlet) to make the label accessible
through your code. In older versions of Xcode, the process of creating an outlet and then wiring it
into Interface Builder was quite long-winded, but Apple has simplified this greatly over the past few
versions of Xcode by allowing you to drag connections directly from Interface Builder into the code.
2. Holding down the Control key, click the label and drag a connection to the
ViewController.swift file. Position the cursor in the class scope, just
below the line class ViewController: UIViewController {, as
shown in Figure 2-14.

Figure 2-14. Creating an outlet using the Assistant Editor

When you release the mouse button, a Connection dialog appears, asking for a number of values
(see Figure 2-15). The key option you need to be aware of here is the Name text field. If the object
you’re connecting to code can be tapped or trigger an event, you can choose one of two options for
your connection: Outlet or Action. But in this instance Xcode intelligently knows that this label isn’t
interactive and therefore restricts your choices.

Figure 2-15. Creating an outlet for your label


CHAPTER 2: Diving Right In 31

3. The Name text field value determines how you refer to your label in code. For
now, type in lblOutput and click Connect.
If everything’s gone according to plan, the first few lines of code should look like this:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var lblOutput: UILabel!

4. You’re finished with the Assistant Editor for now, so switch back to the
Standard Editor by selecting the icon from the toolbar with three lines in a
box, to the left of the Assistant Editor icon (see Figure 2-12).
You’ve finished with Interface Builder for this project. Now you need to write some code to
manipulate your label. Go to the Project Navigator and select ViewController.swift. The View
Controller’s class file opens in the code editor, as shown in Figure 2-16. This chapter touches on
many areas and concepts I explain throughout the book; but at this point, you’re going to start using
Xcode’s powerful code editor and see some of the intuitive features that make Xcode one of the best
IDEs ever.

Figure 2-16. Xcode’s built-in code editor with the ViewController.swift file open
32 CHAPTER 2: Diving Right In

With the class file open, notice that it has a number of lines of code by default. This boilerplate code
gives the application a starting point you can build on.
In the code of the class file, look for a line that begins with override func viewDidLoad(): this is the
start of the viewDidLoad function. To complete the very simple code for this application, you need to
tell the View Controller that when the view loads, it should set the label’s text to “Bonjour!”. Add the
highlighted code to the viewDidLoad function, as shown:

override func viewDidLoad() {


super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

lblOutput.text = "Bonjour!"
}

Here you can see Xcode’s powerful code-completion feature in action; it can assist you in writing
code much more efficiently. As you type lbl, a pop-up appears that says UILabel! lblOutput; when
this is highlighted, press the Tab or Return key to complete the word lblOutput. Next type .te. Again
Xcode’s code completion snaps into action and shows a number of options, but the first one in the
list is the one you want: String! text. With that item selected, press Return and continue typing
the code. You can easily see from this example how Xcode’s code completion helps you become a
really efficient programmer, as well as how it helps cut down on errors.
To recap, what you’ve done here is to declare a variable that is linked to the UILabel you added to
the view in Interface Builder using the Assistant Editor. You then added a single line of code to the
viewDidLoad function to set the text of the label programmatically—well done!

Running and Testing


It’s hard to stress enough how important it is to test your application thoroughly before even thinking
about submitting it to the App Store. There are many reasons for this. First, the App Store review
process is very thorough—if your app isn’t up to par, Apple isn’t afraid to let you know in the form of
a rejection. So testing means you reduce your chances of being rejected by Apple. When you submit
your app, if you’re rejected, you have to make the amendments and then resubmit your application,
all of which is time consuming—time that could otherwise have been used to sell your app. Second,
when someone downloads your application, they’re parting with their money and expect a certain
standard. When they purchase and download an app, it’s disappointing to find that it’s slow and hard
to use. Finally, testing makes you a better developer. Smoothing out the creases in your applications
now helps you build good habits, and you carry these on until they become second nature. Testing
can save you a lot of time when working on larger, more demanding projects.
Now that your application is ready to be run, the quickest way to check if it will build successfully
without crashing is to choose Product ° Build (Ɖ+B). If everything’s in order, you should see a small
dialog stating that the build has completed successfully. It’s time to run your application: choose
Product ° Run (Ɖ+R), and Xcode will build and then run the application using the target specified,
which is (by default at this stage) the iOS Simulator.
The iOS Simulator is invaluable when you need to test your application quickly or test a feature
that you’ve recently implemented. However, it’s important to note that testing your app using the
iOS Simulator isn’t the same as testing it on an iOS device—that is, an actual iPhone/iPod Touch
CHAPTER 2: Diving Right In 33

or an iPad. Applications may not perform the same on a device as they do on iOS Simulator,
because the simulator doesn’t simulate all software and hardware functionality. To change the type
of device you’d like your application to be tested on via the iOS Simulator, go back to Xcode and
click the Stop button in the top-left corner. With the application no longer running, go back to the
iOS Simulator and choose Hardware ° Device, and then select from the list of devices available.
Figure 2-17 shows the application running in the iOS Simulator.

Figure 2-17. The iOS Simulator running the app using the iPhone 5s as the simulated hardware
34 CHAPTER 2: Diving Right In

Additionally, in the iOS Simulator you can change the orientation of the device, the scale at which
the device displays, and an array of other options covered later in this book. What’s recommended
now is that you browse through the menus of the iOS Simulator while your app is running and play
around with the options to get a little familiar with the different features.

Adding Files
One final thing worth mentioning at this early stage is how to add your own files to your project.
Applications can be made up of literally hundreds upon hundreds of files, ranging from images to
sounds. Let’s add some images to the example application: let’s change the app’s icon without
writing any code and then add a background image to the main view.
Before you add an icon file, you either need to create one or download the source code for this
book from the Apress web site and use the included files. Because this isn’t a book on iOS design
or even iOS development, I won’t digress about how to create perfect iOS app icons. Instead, I’ll
just state that you need to create a PNG file that, in this instance, I’m calling icon120.png, with
dimensions 120 px by 120 px. Include whatever you like as the graphic, making sure it conforms to
these specifications. I created a file with a basic gradient and a speech bubble saying “Hi!” in the
middle. To set the application icon, you work with a feature that Apple introduced in Xcode 5, called
Asset Catalogs; these are covered in more detail later in the book, but suffice to say they make the
headache of managing retina and standard-resolution images far easier than having a folder with lots
of different-sized images. Here are the steps:
1. From the Project Navigator, select Images.xcassets.
2. You’re presented with two items in the left column of the Asset Catalog. Click
AppIcon.
3. Bring the Finder window with the icon file in it over the top of Xcode, and
then drag the icon file to the box labeled iPhone App Icon iOS 7 60pt, as
shown in Figure 2-18.

Figure 2-18. The Images Asset Catalog where you set the application icon
CHAPTER 2: Diving Right In 35

Run the application to see the icon in action. Once the app is running, choose Hardware ° Home
(Ɖ+ Shift+H). If you’ve done everything right, you should see something like the image in Figure 2-19.

Figure 2-19. The application’s new icon in the iOS Simulator

Note As you’ll learn as you progress through the book, Apple requires you to create icons in numerous sizes
for the App Store, currently including up to 1,024 pixels square. It’s good to get into the habit early of adding
the resolution to the filename to help you keep track of your assets.

With the icon successfully set, let’s look at another, more traditional way of adding files to the
project. (You expand your Asset Catalog knowledge later in the book.)
They say there’s more than one way to skin a cat, and the same can be said for accessing Xcode’s
Add Files dialog. First, you can choose File ° Add Files to “HelloWorld” (Ɖ+ƃ+A), or you can right-
click in the Project Navigator area and choose Add Files to “HelloWorld”. But the method I want you
to use is to click the plus icon in the bottom-left corner of the Project Navigator and then click Add
Files to “HelloWorld”, as shown in Figure 2-20.

Figure 2-20. The Add Files dialog available from the Project Navigator
36 CHAPTER 2: Diving Right In

The Add Files dialog will be instantly familiar to any user of OS X. Now you need to locate an image
file you would like to use as the application background. In this example, I have downloaded an
image from the fantastic website www.unsplash.com, which has a collection of images licensed under
the Creative Commons license (they’re public domain). The image is included with the downloadable
resources for this chapter.
Once you’ve located your file, select it, and then make sure Copy Items If Needed is checked, as
shown in Figure 2-21. Click Add.

Figure 2-21. The Add Files dialog. Note that Copy Items If Needed is checked

At this point you return to Xcode. Look at the Project Navigator, where you see that your file has
been added to the project’s file structure. When I asked you to add the file, I also asked you to
ensure that Copy Items If Needed was checked. The reason is that if you don’t check this option,
the file appears in the project structure as it does now, but the file itself isn’t copied into the project.
Hence, if you were to send the project to someone or to archive it, the image would be omitted.
CHAPTER 2: Diving Right In 37

Organizing Files in Xcode


Before you proceed and make this image appear in your view, let’s talk about organizing files. As
I mentioned earlier, Xcode gives the illusion of organization: a kind of faux folder structure that in
Xcode is called Groups. Take the file you just added, and move it to the Supporting Files group by
clicking it and dragging it until Supporting Files is highlighted. Figure 2-22 shows a comparison of
the structure of the files in Finder compared to the structure of the files in Xcode.

Figure 2-22. The structure of the files in the Project Navigator in Xcode (left) compared to those in Finder (right)

As you can see, there are some similarities, but there are items that are visible in one view and yet
hidden in the other. This peek behind the scenes teaches you that what you see in Xcode may or
may not physically exist on your file system.
You’ve done the hard work of adding the file to Xcode. Now let’s do the fun part—adding the image
to the View Controller using the Storyboard. Start by selecting Main.storyboard from the Project
Navigator. Now, from the Object Library, select an image view (UIImageView) object, and drag it to
your View Controller. If you’re having trouble finding it in the list, remember that you can filter the list
by typing “image” in the search field. Resize the Image View so it fills the entire view. Your screen
should look something like Figure 2-23.
38 CHAPTER 2: Diving Right In

Figure 2-23. Adding the image view and resizing it to fill the view

Tip Remember, if you can’t see the Object Library, you can access it via View ° Utilities ° Show Object
Library (Ƃ+Ɖ+ƃ+3).

With the image view positioned, it’s time to specify which image to use:
1. With the image view selected, go to the Attributes Inspector.
2. From the Image drop-down, select the file you added. If you’re using the
image from the download, then this is the file starting with jNgy.
3. The image fills the image view, but it may have been distorted in doing so.
You want the photo to fill the image view but maintain its ratio. To achieve
this, click the Mode drop-down list, and change it from the default Scale To
Fill option to Aspect Fill. Your image still fills the image view, but the ratio is
maintained, preserving the original look of the image.
CHAPTER 2: Diving Right In 39

4. You once again need to apply constraints, this time to the image view.
Ensuring that the image view is still selected, click the Pin button at the
bottom of the design area. This time, all four values should say 0; if they
don’t, change the values to zero and then click all four I bars as shown in
Figure 2-24. Click the Add 4 Constraints button.

Figure 2-24. Applying constraints to the image view

So you’re finished, right? Not quite. With the image view filling all the available space, you can’t
see the label you added at the start of the project! This is because of the way these two items
are ordered, or rather how they’re layered: the image view is rendered in a layer above the label,
obscuring it. To resolve this, let’s look at the Document Outline. If you can’t see the Document
Outline (the column between the design area and the Project Navigator), click the Show Document
Outline button in the bottom-left corner of the Storyboard design area, or choose Editor ° Show
Document Outline.
Expand all the items in the Document Outline. Beneath View, you should have your label, followed by
the image view. Drag the image view carefully to move it above the label, as shown in Figure 2-25.
Because of the hierarchy of the objects in the view, the image view is now rendered beneath the label,
although you may need to tweak the color of your label to make sure it’s visible against the image
background.
40 CHAPTER 2: Diving Right In

Figure 2-25. The Document Outline after rearranging the order of the elements in the view

You’re ready to run your app! Figure 2-26 presents the finished product.
CHAPTER 2: Diving Right In 41

Figure 2-26. The finished app


Discovering Diverse Content Through
Random Scribd Documents
“Now make an apology to the orderly,” said the ensign
sternly.

“I’m sorry, Grubb, I’m awful sorry the rifle went off
—’cause I’ve got a big scolding from papa and the
admiral, too. But you hadn’t any business touching the
flag; you know you hadn’t. Come around next Saturday
morning and I’ll give you my half-dollar.”

“Thanky, sir,” answered the orderly, “but my feelin’s is


too much hurt for to take money from you.”

“Well, then,” said Young Brydell promptly, “I’ll ask you to 24


my birthday party instead. I’m going to have a birthday
next week. I’ll be nine years old; and I’m to ask
anybody I like, and I’ll ask you and Capps, the
watchman, and some other fellows. Will that help your
feelin’s?”

“Course it will, sir,” answered Grubb again; “and sailors


and marines is so fond o’ one another.” Capps was a
retired boatswain who was a watchman at the yard, and
as Grubb said this he slightly closed his left eye.

On that understanding they parted. It was Young


Brydell’s proud privilege on his birthday to ask his own
guests, and he had before included Capps, who was
until the advent of Grubb his most intimate friend.

On this Saturday, therefore, there was a table set on the


broad back piazza of the ensign’s quarters. Aunt
Emeline disapproved of the whole thing, but Cunliffe’s
mother, who was a kindly woman, saw that the cake
was there with nine candles in it, and Young Brydell sat
at the head of the table. All the members of Company
C, including Micky O’Toole, first sergeant, were present,
and Capps, a bronzed old seaman, and Grubb, who was
almost as handsome as the ensign, Young Brydell’s
father. His ear still had a red scar, but over a bowl of
lemonade Grubb and Young Brydell swore eternal
friendship, and the friendship lasted until the end came.

The ensign’s quarters were just back of the admiral’s 25


great roomy house, where he dwelt in solitary
magnificence; and Admiral Beaumont, sometimes
finding the house lonely and silent,—as houses are
where there are no women and children,—would look
from his back piazza and often see a lonely little boy,
too, in the ensign’s quarters. For Young Brydell was
never made to go to school as regularly as the other
boys, and was, unluckily, allowed his own way entirely
too much—all because he had no mother.

The admiral, feeling sorry for the child and finding a


kind of odd and pleasant companionship with him,
would send Grubb over with the request that Master
Dick be allowed to come over to luncheon, and even
Aunt Emeline could not ignore that request. So Young
Brydell would go off quite joyfully with Grubb and soon
be seated opposite the admiral at the round table in the
big dining-room. The two would then exchange
reminiscences—Young Brydell pumping the admiral
industriously about “When you were on the old
Potomac, sir,” or “That time you were in the siege of
Vera Cruz.”

Behind the admiral’s chair stood Billy Bowline, once 26


captain of the maintop but retired because of deafness.
This was a sore point to Billy, who always protested: “I
kin hear everything I wants to, and I never missed a call
from the day I j’ined the sarvice, and I kin hear the
[1]
admiral a sight better ’n Grubb, the jirene.” The
admiral, though, always roared at Billy so loud that
everybody in the yard could hear him bawling.

It was of course agreed that but one career was


possible for Young Brydell, and that was the navy. The
ensign thought so, and so did the admiral and Grubb
and Billy Bowline and Capps, the watchman, who was a
chum of Billy’s as well as of Young Brydell’s.

One day, though, a strange thing happened about


Capps. Young Brydell, coming along from school,
whistling the bugle call, saw Capps sitting in his usual
place on the bench in the shade by the ordnance
building. Young Brydell called out as usual:—

“Hello, Capps!”

But Capps did not move. His eyes were closed, and 27
Young Brydell, after playfully prodding him with a slate
pencil, went his way. Presently he met Cunliffe, who
also saw the old sailor sitting so still upon the bench.

“Let’s have some fun with old Capps,” cried Cunliffe.

“No, you sha’n’t,” answered Young Brydell stoutly.


“Capps is a friend of mine and I won’t have him teased.”

Words followed this, and it ended by Young Brydell


giving his young friend a kick on the shin, by way of
testifying his loyalty to his old friend. Just then Grubb
came along and asked the cause of the difficulty. Young
Brydell pointed to Capps. Grubb went up to him,
touched him, and then came back to the two boys,
looking rather strange.

“You young gentlemen go along now; I know the


admiral’ll want you to go along, and I’ll tell you all about
it after a while,” he said hurriedly.

The boys walked away, but from the window in Young 28


Brydell’s room they saw Grubb and another marine take
Capps up, who appeared to be quite limp, and carry him
off to the dispensary, and an hour or two afterward they
met Lucy, the apple-cheeked maid at the admiral’s
house, with her apron to her eyes; she, too, had been a
friend of the ex-boatswain.

“Mr. Capps is dead!” cried Lucy with a fresh burst of


tears, “and ain’t it too dreadful?—oh, dear, oh, dear!”

The two boys each turned a little pale. This was their
first knowledge of that unknown thing called Death.
Next day Capps was buried. Ensign Brydell and one or
two other officers walked in the old boatswain’s funeral
procession. He had always said he wanted “a rale lively
funeral, like as a sailor man is got a right to,” and he
was gratified. The plain coffin rested on a caisson, and a
squad of sailors and marines marched behind it with the
band playing.

As the little procession moved slowly out of the navy


yard gate in the hot sunshine, a company of seven small
boys fell into line behind the last squad. It was C
company, with Young Brydell at its head. The boy’s
sunburned face was blistered with tears, but he was too
much of a soldier to wipe them away, while marching—
for he had been fond of old Capps and had felt lonely
ever since Capps had died.

Nobody attempted to stop C company. They marched 29


along in good order, their small legs being equal to the
slow pace of the funeral procession. It was a long way
to the sailors’ cemetery and the day was hot, but C
company stood up to the work like men. Whether by
design or not they were cut off from a good view of the
grave when poor old Capps was let down into it, and
the next moment the band struck up “Garryowen,” and
to its rattling music the sailors and marines stepped out
at a lively rate.

So did C company. But after ten minutes the pace was


too much for it. First Cunliffe lagged behind, then one
by one, even to Young Brydell, they gave out, and it
was a good twenty minutes after the sailors and
marines had turned in the great gate to the navy yard
that C company, consisting of seven very hot and tired
small boys, straggled through. But as soon as they
appeared, the corporal of the guard sang out “Turn out
the guard!” and the next minute the marine guard stood
at “present arms” as the boys marched through.

“For it’s the honor you did poor old Capps,” said Grubb 30
to Young Brydell.

The boy had the usual habit of asking questions, after


the manner of his kind, and one day when he and
Grubb had got to be very good friends, he suddenly
asked:—

“Grubb, are you married?”

“I’m a widower,” said Grubb.

“So is papa,” answered Young Brydell. “The other


fellows tease me and say papa will give me a
stepmother some day, but I don’t believe it.”

“A stepmother’s a deal better’n no mother at all,”


announced Grubb.
“And have you any children?” continued Young Brydell.

“A boy about your size, but he ain’t here.”

Young Brydell felt so surprised and also so hurt at


Grubb’s want of confidence in keeping these important
facts to himself that he could only stare at him. Grubb
laughed rather grimly.

“You see, my wife belonged to better folks than I. Her 31


folks said she oughtn’t to marry a jirene, as they called
me. Her father was a master mechanic, and when she
died, poor thing! they took the boy, saying they could
do a better part by him than I could; a marine don’t git
much pay, you know; and, like a fool, I give him up.
Now, in some way, the boy don’t seem like my child.
He’s got schooling, more ’n I ever had, and he goes to
school with fellers whose fathers I waits on, and he’s
ashamed o’ this here uniform I wear. So when I seen
how it was, a year or two back, I kinder let the thing go.
I send him half my pay every month, and it don’t pay
for the clothes he wears, they dress him so fine, and it
seems to me I oughtn’t to bring him here, just to
associate with Micky O’Toole and the rest o’ the men’s
children.”

“But I ’sociate with Micky O’Toole,” put in Young Brydell.

“That’s different. Micky knows how you are goin’ to be


an officer and as how if ever he gits in the navy, ’twill
be as a ’prentice boy, and Micky ain’t no sort o’ a
aspiring fellow. He don’t want to be no gentleman. But
my boy does. And my boy’s too good for me, that’s a
fact.”

“He oughtn’t to be,” said Young Brydell stoutly. “You’re a 32


good fellow; everybody says so, and you’re a handsome
fellow, and papa says he never saw a better set-up
fellow, and you’ll be promoted.”

“No, sir,” answered Grubb, shaking his head, “I ain’t


eddicated. I know my business, but it takes book
learnin’ to make a sergeant or even a corporal. I can
read and write and cipher some, but my boy could beat
me at it before he was eight years old. It seems to me
like the boy was mine and yet he ain’t mine; but
yonder’s the admiral comin’ and I ain’t been to the
postoffice yet.” So Grubb strode off, leaving Young
Brydell considerably mystified about the marine’s boy.

33
CHAPTER III.
BRYDELL’S FIRST FAILURE.

Just six years after the May day that Young Brydell had
nearly shot Grubb’s ear off, on a day as bright, he sat
with a number of other young fellows about his own
age around a long table, answering the questions of
three professors who were examining them. Each had a
great stake in this examination, as it was for an
appointment to the naval academy at Annapolis.

Young Brydell had ceased to be Young Brydell then,


being quite fifteen years old. He has experienced a good
many changes in those six years. Much of the time his
father, now a lieutenant, had been at sea, but unluckily,
whether his father were at sea or on shore, Brydell was
still allowed to have his own way, and a good deal more
of the lieutenant’s pay than was good for a boy.

The old tenderness and sympathy still encompassed him 34


—he had no mother. Therefore whenever Brydell found
himself dissatisfied at school a complaining letter to his
father would result in his going somewhere else. When
his teachers represented that Brydell, although an
extremely bright fellow and fond of reading, yet
neglected his recitations for athletics, Brydell would
write a most convincing letter to his father explaining
how impossible it was for him to do more at his books
when his duties as captain of the football eleven were
taken into consideration, and his letters were so bright
and well written that his father, as foolishly fond in his
way as poor Grubb, would persuade himself that the
boy would come out all right.

He had even been sent to Switzerland to school, but like


the other schools this one did not suit Brydell, and six
months after he was home again. Fortunately Brydell
possessed certain strong traits of character that are
difficult to spoil. He was perfectly truthful, brave, and
had naturally a good address.

Nothing could have been prettier than the devotion 35


between him and the lieutenant. As Brydell said: “Dear
dad, fatherly respect is out of the question. When you
got married at twenty, you took the chances of having a
boy in the field before you were ready to quit it yourself.
I’ll agree to treat you as an elder brother, but we’ve
been chums too long for you to come the stern father
over me.” And this would be said with such an
affectionate hug that the lieutenant could only make
believe to growl.

And so Brydell grew up without any of the wholesome


restraints and self-denial of more fortunate boys. He
was not a conceited boy, but he realized that whenever
he had failed it was because he had not really exerted
himself, and he had a naturally optimistic way of looking
at life, which so far had not been rudely contradicted.

The determination to go into the navy had grown with


his growth and strengthened with his strength, and no
other plan of life had ever occurred to him. He knew the
difficulties of getting an appointment, but like most
happy young fellows of his age and inexperience, he
thought all difficulties existed for other people; his own
way would be easy enough.

His father had carefully retained a legal residence in his 36


native town, expressly for Brydell’s sake, so he could be
eligible for appointment from that district. But Brydell,
having concluded to try private tutors for a while,—
which were changed as often as the schools were,—had
lived for nearly a year and a half with his Aunt Emeline
in a town outside of his own congressional district.

One morning, picking up a paper, he had read that a


competitive examination would be held for an
appointment to Annapolis, open to all boys who had
lived twelve months in the district.

“That suits me to a dot,” cried Brydell, and from then


until the day of the examination he really worked hard,
never doubting for a moment his ultimate success.

Aunt Emeline, it is true, croaked like a raven, but Aunt


Emeline always croaked. Brydell had already in his own
mind composed the letter announcing his success to his
father and another one to the admiral, who had
continued to be his fast friend, and another one to
Grubb, his old chum, the marine. On the morning of the
examination he therefore presented himself and was
duly accepted in the competition.

Next him at the table sat a handsome young fellow 37


about his own age. Something in the boy’s fresh,
regular features and lithe young figure reminded Brydell
of Grubb. Of all his early friends Brydell loved the kindly
marine, with his manliness and truth and bad grammar,
better than any of them. Although Grubb had done his
share of sea duty, he and Brydell had met many times in
all those years, and always Brydell felt as if he were a
little lad again.

Once, Brydell remembered, Grubb, being about going to


sea again, had paid the expenses of a long journey out
of his small pay to see him, and Brydell suspected that
Grubb’s ticket had taken about all his spare cash, and
that he had lived on hard tack and a can of smoked
beef most of the way, which was hard on a big fellow
like the marine.

It suddenly flashed upon Brydell that this handsome 38


fellow might be Grubb’s son; he was about the right
age. Brydell at this pricked up his ears, but in a few
minutes one of the professors, happening to address
the young man, called him “Mr. Esdaile.” Then he was
not Grubb’s boy, and Brydell lost all interest in him,
except that he wished he could write the answers off as
quickly as Esdaile could. For Esdaile never paused a
moment, but with the ease and rapidity of one perfectly
accustomed to his subject he answered every question
put him.

Not so Brydell. He was well up in history and geography,


for he was a great reader. But in mathematics he
stumbled woefully and made something very like a
fiasco.

When at last it was over and the young fellows each


took his way home, Brydell felt a sickening sense of
failure. He had really worked hard in preparing for the
examination, but he forgot that he had never worked in
his life before. His three weeks’ spurt had seemed to
him a tremendous effort that must win success, but it
had not. And then came a terrible apprehension; if he
had failed at this examination, and he felt perfectly sure
he had, he might fail at another. He might even fail in
getting the appointment from his own district, for the
congressman might well hesitate to give it to a boy who
could not hold his own in a preliminary examination.

This thought staggered him and almost broke his heart, 39


for he had dwelt so long on the navy that he could not
think what to do with his life if his ambition in that way
should be balked. He was only kept in suspense a week
or two and then the blow fell. Esdaile had got the
appointment, and Brydell was at the foot of the list.

Only a proud, sensitive, and inexperienced soul could


imagine the pain that Brydell suffered. It was not alone
the mortification of failure; he had allowed his passion
for the navy to take such possession of him, body and
mind, that any serious setback to this cherished hope
seemed to him an appalling misfortune.

In his tempest of disappointment he turned for the first


time in his life, even in his own mind, against his father.

“It is not my fault,” he thought in sullen fury. “I am


bright enough, only I never was made to work. And yet
everybody talks about my advantages. Was it any
advantage that I should never stay at any school more
than a year, and hardly ever more than six months? Was
it any advantage to me to be sent to Europe where I
picked up a smattering of French and came home to
find myself behind every fellow of my age I knew,
except in that one thing? Was it any advantage to me to
have more money than almost any boy I knew, to
squander on athletics and all sorts of rubbish?”

40
This last reflection brought Brydell suddenly to himself.
He remembered poor Grubb’s giving his boy half his
pay. “And my poor old dad—poor young dad, rather—
gave me, I believe, a good deal more than half his pay.”

Brydell had learned something about how money went,


and he stopped, startled at the idea of how much
skimping and saving his father must have done to give
him the money. He fell into a passion of remorse.

“Poor dad—poor dad!” was all he could think, and “dad”


was so young—barely thirty-six, and did not look a day
over thirty. “I dare say,” thought poor Brydell, with the
ghost of a smile, “that’s why it was he never married
again. I was squandering his pay.”

Brydell was too generous a fellow to reproach his father, 41


except to himself in his first angry mood, and knowing
the lieutenant would hear about the examination
anyway, he sat down and wrote his father frankly and
fully, admitting his failure, and his determination, if he
could get another chance, to do better. But the
lieutenant was far away in the Pacific and it would be
months before he could get the letter, and perhaps
other long months before Brydell could get an answer.

Then he wrote the admiral in the same strain. The


admiral, who happened to have shore duty then, got
the letter. He was sitting on the piazza, facing the salt
sea, and when he had finished reading it he brought his
fist down with a thump on the arm of his chair and
shouted:—

“By!”

The admiral always held that expletives were vulgar;


but when much wrought up he took refuge in “By,”
which might mean any and every thing.

“Just like the dog when he was about as big as a


cockchafer, and took the whole blame of cutting up my
turf, when there were six older boys aiding and abetting
him. Bowline! here, sir!” and in a few minutes Billy
Bowline came trotting along the hall.

“Bring me my portfolio and the ink,” said the admiral. 42


“That little scamp of a Brydell has failed in a competitive
examination for an appointment to the naval academy,
and how his father could expect anything else, I can’t
see, taking him to Europe, putting him at school one
day and taking him away the next, and giving the boy
no chance at all, simply because he was too soft-
hearted to say no! And now the young fellow behaves
like a man and shoulders it all. I say, Bowline, we can’t
afford not to have that young fellow in the service.”

“No, sir, we can’t!” said Billy very seriously. “We’re


’bleeged to have him, sir, in the sarvice.”

“And how is it to be done, you old lunkhead?” bawled


the admiral.

“Beg your parding, sir, it’s easy enough,” answered Billy


stoutly. “There ain’t nothin’ in the reg’lations as prevents
a admiral from axin’ the member o’ Congress from Mr.
Brydell’s districk, if he’s got a ’pintment to give away;
and if he rightly understands his duty to a rear-admiral
on the active list, he dasn’t say no, sir.”

“William Bowline,” said the admiral solemnly, “if you


weren’t the biggest ass I ever saw, I’d say you were a
genius. Bring me the navy register quick.”
The admiral glanced at the register and saw there 43
would be a vacancy in that year in Brydell’s district. He
then wrote fourteen pages to the member of Congress,
and sealed it with his big red seal.

“That’ll fetch it,” thought Billy proudly. “It looks like it


comes from the sekertary of the navy.”

As Billy was starting off to the postoffice with the


important letter, the admiral picked up Brydell’s letter
and read it over, half-aloud. “Esdaile, Esdaile; that has a
familiar sound,” he said.

“In course, sir,” answered Billy with a sniff. “That’s the


son o’ Grubb, the jirene. You know, sir, Grubb married a
woman whose folks was ashamed o’ him; and Grubb,
like a great big ass, give the boy to his wife’s people
arter she died, and they stuffed that young ’un up with
false pride until he got ashamed to speak to Grubb; and
Grubb, he was a-sendin’ the boy half his pay straight
along. So then the boy’s grandfather died and left him a
small fortin’ on condition that he changes his name to
his mother’s, Esdaile; and the brat were willin’ enough,
for he thought hisself too good to be named Grubb, and
now he’s goin’ to be a officer.”

Here Billy rumpled his hair up violently to show his 44


contempt for Grubb’s boy, and the admiral again cried:—

“By!”

There was a great running to and fro between the


admiral’s house and the postoffice in those days, and
the admiral and Billy both began to feel anxious about
Brydell’s appointment. The day was fast approaching
when the candidates must present themselves for
examination at Annapolis, and at last, three days before
the time, just long enough for the admiral to write to
Brydell and for Brydell to get to Annapolis, the
appointment came from the member of Congress.

Admiral Beaumont was so happy when he got the letter


that he gave a kind of snort of pleasure, and Billy, who
was standing by, eagerly watching the opening of the
letters, had to go out in the backyard to chuckle. The
admiral sent a dispatch and a letter to Brydell, and Billy
stumped off gleefully with them, and three days
afterward Brydell had presented himself at Annapolis.

45
CHAPTER IV.
BRYDELL’S SECOND FAILURE.

Far back in his babyhood, almost, Brydell remembered


the academic buildings, the green lawns, and bright
river at Annapolis, and when on a lovely May evening he
walked in the great gates and passed the marine on
guard, he felt so happy he could have danced and sung.

The weeks since his failure had been spent in a dull and
hopeless mental lethargy. Aunt Emeline had been grimly
consolatory and had tried to impress on him that he had
made a lucky miss in not getting into the navy, and
named at least a thousand professions and business
ventures in which he could make more money. The
good woman did not see in the least how it was with
the boy—that he was simply born to be a sailor, and
that nothing on earth could charm him then from his
wish.

After that one outburst of generosity in writing to his 46


father and the admiral, he had settled down to a sullen
submission. It would be months before he could hear
from his father, and until then nothing could be done.
Suddenly, like the lifting of a mist by the glorious sun,
came the admiral’s letter and the appointment, and
within twenty-four hours Brydell was on his way to
Annapolis to be examined for admission to the academy.

He had had no time to prepare for the examination,


even if he could. But a boy of Brydell’s temperament
does not learn prudence and caution in a day or a
month, and he was as perfectly sanguine of success in
the coming examinations as if he had not failed before.
He could have hugged the admiral for his goodness, and
had sat up half the night, when he got the treasured
letter, writing his thanks to him and the member of
Congress.

On this lovely May afternoon he walked with a springy


step along the brick walks of the academy grounds
under the giant trees, fresh in their spring livery, and as
he looked at the velvet turf he smiled and thought of
the admiral and the dirt fort and Grubb and that early
time. It was not necessary for him to report until next
morning, so he strolled along, the very happiest fifteen-
year-old fellow in the world.

Presently sauntering along the sea wall and watching 47


the reflection in the water of a steam launch filled with
ladies and officers, he suddenly came directly upon his
old friend Grubb, standing and talking with Esdaile, the
handsome young fellow who had so far outstripped all
the other candidates, himself included. Esdaile started,
and then blushing a fiery red, nodded his head to Grubb
and walked off.

As for Brydell, all the kindness he had ever received as a


little boy from the handsome marine rushed to his mind.
Grubb, as handsome as ever, although a good deal
older, smiled delightedly as Brydell dashed forward, but
seeing how tall the young fellow had grown, Grubb
drew himself up and saluted as he said: “How d’ you do,
Mr. Brydell?”

“Oh, hang the salute, Grubb! shake hands,” cried


Brydell, delighted. “I’m not a cadet yet, so we needn’t
stand on ceremony.” At which Grubb and he sawed the
air for five minutes.

“And are you come down here for to be examined, sir?”


asked Grubb, smiling broadly.

“Yes,” said Brydell, adding shamefacedly, “I had a 48


chance in a competitive examination, but that fellow you
were talking with—Esdaile—got ahead of me.”

At this it was Grubb’s turn to color. He shifted his feet


and said hesitatingly:—

“Mr. Brydell, please don’t go for to tell it, sir, but Mr.
Esdaile—Mr. Esdaile is my son. His grandfather’s left him
some money, if he’d take the same name—Esdaile; and
as the boy didn’t like the name o’ Grubb, nohow, he got
his name changed by law—and I’d ruther—I’d ruther, sir,
the folks here didn’t know it, bein’ as I ain’t nothin’ but
a marine.”

Brydell was so taken aback for a moment that he did


not know what to say, and Grubb with unwonted
fluency continued:—

“I’ve sent in my application for a transfer, sir, ’cause the


boy don’t want—I mean I don’t want—to be stationed
here, a-doin’ guard duty while my boy is in the
academy. I’ve talked it over with one o’ the officers as
I’ve knowed, and who has been a good friend to me,
and he says maybe it will be best all around. And I hope
nobody will know that Cadet William Esdaile is the son
o’ Grubb the marine.”

“You may be right in getting transferred somewhere 49


else,” answered Brydell after a moment, “and if the
officer advised you, I wouldn’t venture to say a word;
but I don’t see why your boy should not want to
recognize”— Here he stopped, not knowing how to keep
on.

“Didn’t I tell you, sir, long years ago as how the boy was
gittin’ above his father?” burst out poor Grubb, his eyes
filling with tears. “He’s ashamed o’ me; he’s ashamed to
be seen a-talkin’ with me, and I can give him half my
pay, and I’d give him all o’ it if he needed it, but I can’t
stand bein’ looked down upon by him.”

“Why, if you were my father, I shouldn’t be in the least


ashamed of you,” cried Brydell hotly. “You haven’t had
the advantage we other fellows have had, but you’re
one of the most honest and respectable men in the
world; so says my father and Admiral Beaumont, too,
and it’s a great deal better to come out and be honest
and above board about these things than to be skulking
and hiding them.”

“That’s true for you, Mr. Brydell,” replied Grubb, who 50


had natural good sense and much more experience than
Brydell. “That’s your natur’. But it ain’t everybody’s
natur’. It ain’t my boy’s natur’; I wish it was. It’s the
easiest way and the best way o’ gittin’ through life, but
it takes all sorts o’ people to make up a world, and
there’s lots o’ people that could no more be aboveboard
than a pig can fly.”
Brydell had not lived long enough to appreciate this
truth, and he parted from Grubb with a mixture of
respect and contempt for him, but with unabated
affection, and a most genuine disgust for Esdaile.
Perhaps it was helped a little by Esdaile’s triumph over
him, but Brydell had always hated a sneak, and he had
very good ground for thinking the accomplished Mr.
Esdaile was constitutionally a sneak.

Next day he reported and the examination began, and


then came a time that in torture far exceeded the sharp
disappointment and sullen despair of the last few
weeks. For, after days of struggle and nights of furious
though ill-directed study, again did Brydell fail, and this
time he thought it was forever.

When he knew it he had but one desire on earth—to get 51


away from the place anywhere—anywhere. But where
was he to go and what was he to do that people would
not find him out? He hated to go back to that dreary
house with Aunt Emeline; his father was completely out
of his reach,—that too kind father,—and Brydell felt sick
at the idea of meeting the admiral again.

Filled with the despair of the very young,—who can see


nothing beyond the narrow horizon of the present,—
Brydell, sitting in his room at the hotel, dropped his
head upon his arms, and wished himself dead. He did
not know how long he had lain thus, only that the sun
was shining brightly in the afternoon when he heard the
dreadful news, and it was quite dusk when he had a
strange feeling that some one was present, and there
stood over him Grubb’s tall figure.

“It’s mortal bad, Mr. Brydell,” said Grubb. Brydell


answered not a word, and in the silence of the twilight
the only sound was the melancholy call of a night bird
heard through the open window.

“Whatever are you goin’ to do now, Mr. Brydell?” asked


Grubb after a while.

“I don’t know,” said Brydell in a voice that he hardly


recognized as his own.

“You’d better ask the admiral, sir,” presently Grubb 52


continued.

Brydell made no reply. Then, after a longer pause than


usual, Grubb kept on:—

“You ain’t had no rale preparation, I reckon.”

“No!” cried Brydell bitterly; “sent from one school to


another, as often as I wanted; allowed twice as much
pocket money as any other boy in school, while my
father was pinching and skimping himself to give it to
me; with no home, no mother, to encourage me and
nobody to govern me; of course I failed. I’ll always fail.”

“Don’t you go for to say that, Mr. Brydell, and it seems


like I ain’t the only foolish father in the world. There’s
others as had eddication and all sorts o’ things that
don’t act no wiser nor poor old Grubb the marine.”

“Don’t say a word against my father!” cried Brydell,


lifting his pale face for the first time.

“I’d be the last person in the world to say a word


against the leftenant, sir, but I say as how ’twas always
said of you when you was a little shaver: ‘Don’t be hard
on him, he ain’t got no mother.’ Well, now it seems to
me they’ve been monstrous hard on you when they
thought they was bein’ easy.”

Brydell said nothing more. He knew Grubb was telling 53


the truth.

“Well, now, sir, let me tell you something. I knows all


about these app’intments. You set down and write the
admiral and ask him if he’ll ask that there congressman
to give you a year to prepare yourself. Tell him as how
you ain’t had half a chance, and give him your word as
a gentleman you’ll pass next year if they’ll let you keep
the app’intment.”

“I’m ashamed to.”

“Good night, Mr. Brydell,” said Grubb. “Them as is


ashamed to ask for another trial when they ain’t had a
good chance, seems to me, ain’t got much sand. It
looks like you warn’t willin’ to work.”

“Sit down, Grubb,” answered Brydell, beginning to


consider this sound advice, and before Grubb left the
room the letter was written to the admiral.

“It won’t do any good; I know it won’t,” said poor


Brydell despairingly. Nevertheless he agreed to remain
at Annapolis long enough to get an answer.

It would take about three days to get an answer, 54


supposing the admiral to be able to see the
congressman at once. Those days Brydell remained shut
up in his room. It was a turning point with him. He
retained only a dim and chaotic memory of what he felt
and suffered in those three days; but at the beginning
he was a boy, and when he came out of the struggle he
was a man.
In the afternoon of the third day a dispatch came:—

Congressman will let this year’s appointment lapse


and will hold vacancy open for you another year,
upon my solemn word of honor that you will qualify
yourself and pass. I rely upon you to make my
promise good.

GEORGE BEAUMONT.

The day was dark and rainy, but no June morning ever
seemed brighter to Brydell when he read that dispatch.
The transition seemed to him like passing from death to
life.

He knew he had never had a chance at preparation, and 55


he knew he had a good mind, capable of learning what
other fellows did. But, above all, he felt suddenly
develop within himself a determination, a strength of
purpose, a power of will that could do great things if he
tried.

This new force was always a part of his character,


although quickly developed by a strange succession of
fierce disappointments. But impetuosity was also a part
of his character, and with this new sense of manliness
and responsibility came a rash determination that he
would prove his sincerity by working for his living while
preparing himself for that other chance a year hence.

Hot with this thought, Brydell wrote his father a brief


but eager letter:—
And as I have known all the disadvantages of
having too much money to spend, all taken, almost
stolen from your pay, dear old man, while you are
doing without everything for me, and I am
determined never to cost you another dollar. I can
find work easy enough,

(sanguine Brydell)

and work won’t interfere with my studying half as


much as play will, and I want to do something—
anything—everything—to earn the admiral’s respect
and my own too. So make yourself easy, dad, about
me. I’ll be at work when you get this, and you
know whatever faults I’ve had I never was a
milksop; and I’m going to behave myself; don’t you
worry about that. So wait until next year and you
won’t be ashamed of your affectionate son and
chum,

RICHARD BRYDELL, Jr.

Brydell ran and posted this letter before he had time to 56


change his mind about sending it. When it was gone he
had a sudden feeling of shock, like a man just under a
shower bath. But his word was passed. He had naturally
the strength of mind to stick to what he said, and one
of the things that had not been neglected with him was
a most faithful regard for his own word. Rash his
resolve might be, but not to be shirked on that account.

When Brydell realized to what he had committed himself


he seemed to grow ten years older in half an hour. He
felt a little afraid, but all these things were working
together to make a man of him.

57
CHAPTER V.
STRIKING OUT FOR HIMSELF.

Next morning, bright and early, Brydell was up and


dressed. He had no one to say farewell to except Grubb,
but he wanted to see his humble friend and avail
himself of Grubb’s excellent common sense about his
future plans. For the marine had seen a good deal of
the world and knew something of it from a working-
man’s point of view. Grubb happened to be off duty that
day, and early in the morning presented himself in
Brydell’s room. Brydell told him the glorious news, and
Grubb, taking off his cap and waving it three times, said
in a half-whisper: “Hooray! hooray! hooray!”

“And now,” said Brydell, “I’ve got to go to work. I have 58


about twenty-five dollars left after paying my hotel bill,
and I can’t go very far on that. Besides, I’d rather stay
near Annapolis. I can keep in touch with it better in
some ways. I have my books, you know, and although I
have only acquired a smattering from them, yet they
are familiar enough to me to study by myself. And I’ve
got an idea about employment.”

“What is it, sir?” asked Grubb.


“Well, you see, I’ve been great on outdoor life—riding
and walking and swimming; and I believe I could stand
an outdoor life better than I could being shut up in a
dingy office. I hear that the farmers about here find
great difficulty in getting hands, even at high wages and
particularly at this season of the year. If I could get
work on a farm, I could get my living too, which I
couldn’t get in a city.”

“Lord, bless the boy!” cried Grubb in great disgust. “The


leftenant’s son, a-talkin’ about bein’ a hired man! Did
ever anybody hear the likes o’ that for a gentleman?”

“I know I am a gentleman, Grubb, and that’s why it is


I’m not afraid of work,” answered Brydell, who could not
help laughing at Grubb’s look.

After Brydell had talked with him half an hour, though, 59


the marine’s ideas changed. Brydell, who had been
thinking hard on the subject all night, reminded him of
how many young fellows walked the streets of towns,
asking for employment, while in the country
employment was waiting for twice as many men as
could be found. “And besides,” said Brydell with a slight
blush, “in the city I might be all the time running up
against people I know, and if they were civil to me I’d
probably lose the time with them I would have in the
evenings for study, and if they didn’t notice me it would
make me feel pretty bad; while in the country I wouldn’t
be likely to meet a soul I ever knew. It always seemed
to me, too, as if a country life was healthier for a young
fellow.”

“It is a sight healthier in every way,” remarked Grubb


with energy.
“And then I can get work right away in the country, and
who knows when I could get it in town?”

“Mr. Brydell,” said Grubb, “the admiral allers said, when


you were a little shaver, as you’d turn right side up, and
I do believe he know’d what he was talking about.”

“The admiral’s the best friend I have in the world except


you,” cried Brydell; “I believe if you were an admiral,
you’d do just as much for me as Admiral Beaumont.”

“Right you are, Mr. Brydell. I ain’t nothin’ but a poor 60


marine, without any book learnin’, but whenever I sees
that motto of the corps, ‘Semper fidelis’ which means
‘Ever faithful,’ I think to myself, Grubb, my man, that
means you ain’t never goin’ back on another feller; and,
come to think of it, it do seem ridicklous that the
leftenant’s son should be a-workin’ like a hired hand.
But I’ve noticed, sir, as how you’ll put two horses to
haulin’ bricks. If one o’ ’em is a scrub, and t’ other one
has a strain o’ good blood in him, you’ll find the scrub
all petered out by the time his work is done. But the
horse with the good blood’ll haul all day, and be as
frisky as a kitten when you take him out; for blood do
tell, Mr. Brydell.”

Grubb said this with a sigh, and Brydell thought the


poor fellow had his own son in mind.

Brydell did not care to say good-by to the few people he 61


knew at Annapolis, so he started out on a round,
leaving his cards marked “P.P.C.” at each acquaintance’s
house and not waiting to see if they were at home. He
could not help laughing as he did this. He imagined he
saw himself at work in the fields in his shirt sleeves, and
thought it would be a good while before he needed any
more visiting cards.

A natural tinge of boyish adventure made him feel as if


he would like to start out on foot to seek his fortune, so
next morning, having packed up his belongings and left
them in Grubb’s care, Brydell set out with his stick and a
small bundle and twenty-five dollars in his pocket.

It was a lovely day, cool for the season, and as Brydell


stepped out at a lively pace, the world did not by any
means look black to him. When he looked back six
months it seemed to him six years. In that time he had
had one of those plunges into real life which turns a boy
into a man in an inconceivably short time. He had had a
pretty complete experience of what life meant, and he
had set himself to work out his own salvation in earnest.

He thought he would walk about twelve miles before


stopping, wishing to be at least that far from Annapolis.
But the beauty of the day, the greenness and freshness
of the country, led him on and on until it was nearly
fifteen miles.

Then the weather suddenly changed. The sky became 62


overcast, the wind sprung up, and the first thing Brydell
knew he was caught in a drenching rain. He had a rain
coat with him and he put it on, meanwhile keeping his
bundle well protected. He was still following the main
road and he determined to stop and ask for shelter at
the first house he saw. And how that spring shower
changed his views of life!

He realized he was wet and hungry, that he was alone,


and far from all his friends, and all at once he began to
feel very young. He pushed on rapidly, and in a little
while saw across the rolling country a large and
comfortable farmhouse. He made straight for it and in a
little while he knocked at the open door.

A little girl in a white dimity sunbonnet came to the


door. She was about ten years old and remarkably
pretty. She did not show the least bit of shyness and
asked Brydell in hospitably. Before he had time to
answer, her father and mother appeared—handsome
country people, looking, as they were, thoroughly
prosperous.

Brydell, whose manners were naturally graceful and 63


polished, introduced himself and asked the privilege of
remaining until the shower was over, and with a secret
determination to ask for work later on. The farmer’s
address was not nearly so elegant as the young fellow’s
who cherished the ambition of becoming his hired man.
He said:—

“My name’s Laurison. Come in and sit down. If you’ve


got any dry clothes in that bundle, my wife’ll show you a
room where you can change ’em.”

Brydell looked at Mrs. Laurison and his heart went out


to her instantly. She was not like the officers’ wives he
had known, educated and traveled women; but she had
a quiet dignity and a self-possession that was equally
good in its way. And she had the softest, kindest eyes in
the world, and her voice was so gentle when she invited
Brydell upstairs to change his clothes that he almost
loved her from the start. In a little while Brydell
appeared with dry shoes and stockings and another pair
of trowsers.

64
The farmer, being compelled to stay indoors, was not
indisposed to talk with the young stranger, and Brydell
had quite a gift of making himself agreeable. They sat
talking in a large, airy, old-fashioned hall, with a dry
rubbed floor; and the little girl Minna was so pleased
with her new acquaintance that she came and perched
herself on the arm of his chair and gazed fearlessly into
his eyes with the grave scrutiny of an innocent girl.

Brydell knew much about country life, and talked so


knowingly about cows and pigs and horses that even
Mr. Laurison grew fluent, and Brydell imagined it would
be easy enough to get work there, and he quickly
determined to ask for it.

“Do you have any trouble getting farm labor?” he asked.

“Heaps of trouble,” answered Mr. Laurison with


emphasis. “The negroes all go off about this time of the
year for berry-picking, just when harvest is coming on
and the corn needs weeding the worst you ever saw.
I’ve got two men I can count on that stay with me the
year round, but I ought to have four on a farm of this
size.”

Here was Brydell’s chance.

“I’m looking for work,” he said diffidently—“Farm work, I


mean.”

“You!” shouted Mr. Laurison. “Why, you never did any


work in your life. Look at them hands!”

“Pretty brown, I think they are,” answered Brydell


complacently, examining his own hands.
“Yes,” said Mr. Laurison; “but they’re brown with the 65
playin’ of tennis and football and such. Any fool can see
by your hands you ain’t done any work.”

“But I want to do some work.”

“For what?”

“For money, for a living.”

“Ain’t you got any friends or family?”

“I have a father. He’s in the navy and away off in the


Pacific. I haven’t any friend that can help me.”

“And has your father thrown you off?”

“Oh, no; but I want to earn my living, and it’s easier to


get work in the country than in town, and besides I
know more about the country.”

Mr. Laurison’s manner underwent an instant change. He


paused a little while and then said:—

“I ain’t got any work for you;” and after another pause:
“I think it’s clearin’ up.”

Brydell rose at once. He felt that in a moment the


attitude of his host was one of suspicion; but Mrs.
Laurison’s kind gaze never changed in the least, and
little Minna came closer to him and caught his hand.

“Are you going away?” she asked. 66

“I must,” said Brydell gently, but feeling as if he would


choke. Mr. Laurison got up very promptly.

“I’ll show you a short cut to the main road,” he said.


Welcome to Our Bookstore - The Ultimate Destination for Book Lovers
Are you passionate about books and eager to explore new worlds of
knowledge? At our website, we offer a vast collection of books that
cater to every interest and age group. From classic literature to
specialized publications, self-help books, and children’s stories, we
have it all! Each book is a gateway to new adventures, helping you
expand your knowledge and nourish your soul
Experience Convenient and Enjoyable Book Shopping Our website is more
than just an online bookstore—it’s a bridge connecting readers to the
timeless values of culture and wisdom. With a sleek and user-friendly
interface and a smart search system, you can find your favorite books
quickly and easily. Enjoy special promotions, fast home delivery, and
a seamless shopping experience that saves you time and enhances your
love for reading.
Let us accompany you on the journey of exploring knowledge and
personal growth!

ebookgate.com

You might also like