Learning iPhone Programming From Xcode to App Store 1st Edition Alasdair Allan instant download
Learning iPhone Programming From Xcode to App Store 1st Edition Alasdair Allan instant download
https://ebookname.com/product/learning-iphone-programming-from-
xcode-to-app-store-1st-edition-alasdair-allan/
https://ebookname.com/product/learning-ios-programming-from-
xcode-to-app-store-third-edition-alasdair-allan/
https://ebookname.com/product/building-iphone-apps-with-html-css-
and-javascript-making-app-store-apps-without-objective-c-or-
cocoa-1st-edition-jonathan-stark/
https://ebookname.com/product/learning-iphone-game-programming-a-
hands-on-guide-to-building-your-first-iphone-game-1st-edition-
michael-daley/
https://ebookname.com/product/straight-a-s-are-not-enough-
learning-with-your-brain-turned-on-1st-edition-judy-fishel/
Basics Architectural Photography 1st Edition Michael
Heinrich
https://ebookname.com/product/basics-architectural-
photography-1st-edition-michael-heinrich/
https://ebookname.com/product/modeling-performance-
measurement-2005th-edition-wade-d-cook/
https://ebookname.com/product/the-concept-of-education-
international-library-of-the-philosophy-of-education-
volume-17-1st-edition-r-s-peters/
https://ebookname.com/product/listening-is-an-act-of-love-a-
celebration-of-american-life-from-the-storycorps-project-1st-
edition-dave-isay/
https://ebookname.com/product/interpreting-and-the-politics-of-
recognition-the-iatis-yearbook-1st-edition-christopher-stone/
Principles of Foundation Engineering Si Edition Braja
M. Das
https://ebookname.com/product/principles-of-foundation-
engineering-si-edition-braja-m-das/
Learning iPhone Programming
Learning iPhone Programming
Alasdair Allan
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or [email protected].
Printing History:
March 2010: First Edition.
O’Reilly and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning iPhone
Programming, the image of a lapwing, and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
TM
ISBN: 978-0-596-80643-9
[M]
1267461377
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
1. Why Go Native? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
The Pros and Cons 1
Why Write Native Applications? 2
The Release Cycle 3
Build It and They Will Come 4
2. Becoming a Developer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Registering As an iPhone Developer 5
Enrolling in the iPhone Developer Program 7
The Apple Developer Connection 8
Installing the iPhone SDK 8
Preparing Your iPhone or iPod touch 11
Creating a Development Certificate 12
Getting the UDID of Your Development Device 14
Creating an App ID 15
Creating a Mobile Provisioning Profile 16
Making Your Device Available for Development 17
v
4. Coding in Objective-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Declaring and Defining Classes 41
Declaring a Class with the Interface 41
Defining a Class with the Implementation 42
Object Typing 43
Properties 44
Synthesizing Properties 45
The Dot Syntax 45
Declaring Methods 45
Calling Methods 46
Calling Methods on nil 47
Memory Management 47
Creating Objects 47
The Autorelease Pool 48
The alloc, retain, copy, and release Cycle 48
The dealloc Method 50
Responding to Memory Warnings 50
Fundamental iPhone Design Patterns 50
The Model-View-Controller Pattern 51
Views and View Controllers 51
The Delegates and DataSource Pattern 52
Conclusion 53
5. Table-View-Based Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Simplifying the Template Classes 55
Creating a Table View 58
Organizing and Navigating Your Source Code 61
Connecting the Outlets 62
Building a Model 65
Adding Images to Your Projects 71
Connecting the Controller to the Model 73
Mocking Up Functionality with Alert Windows 74
Adding Navigation Controls to the Application 75
Adding a City View 79
Edit Mode 85
Deleting a City Entry 89
Adding a City Entry 90
The “Add New City...” Interface 93
Capturing the City Data 100
vi | Table of Contents
Tab Bar Applications 119
Refactoring the Template 120
Adding Another Tab Bar Item 122
Finishing Up 124
Modal View Controllers 125
Modifying the City Guide Application 126
The Image Picker View Controller 133
Adding the Image Picker to the City Guide Application 133
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
Table of Contents | ix
Preface
The arrival of the iPhone changed everything. Or, at the very least, it changed the
direction of software development for mobile platforms, which is a pretty big thing. It
spawned an entire generation of copycat devices and shook an entire multibillion-dollar
industry to its knees. Despite this, it still fits in your pocket.
xi
What’s in This Book?
Here’s a short summary of the chapters in this book and what you’ll find inside:
Chapter 1, Why Go Native?
This chapter discusses the need for native applications and compares building
native applications to building web applications.
Chapter 2, Becoming a Developer
This chapter walks you through the process of registering as an iPhone developer
and setting up your work environment, from installing Xcode and the iPhone SDK
to generating the developer certificates you’ll need to build your applications and
deploy them onto your own iPhone or iPod touch.
Chapter 3, Your First iPhone App
This chapter allows you to get hands-on as quickly as possible and walks you
through building your first Hello World application, including how to deploy and
run the application on your iPhone or iPod touch.
Chapter 4, Coding in Objective-C
This chapter provides a crash course in the basics of the Objective-C language, and
if you’re familiar with another C-derived language (and perhaps with object-
oriented programming), it should be enough to get you up and running with
Objective-C and the Cocoa Touch frameworks.
Chapter 5, Table-View-Based Applications
The UITableView and associated classes are perhaps the most commonly used
classes when building user interfaces for iPhone or iPod touch applications. Due
to the nature of the applications, these classes can be used to solve a large cross
section of problems, and as a result they appear almost everywhere. In this chapter,
we dive fairly deeply into the table view classes.
Chapter 6, Other View Controllers
After discussing the table view controller in detail, we discuss some of the other
view controllers and classes that will become useful when building your applica-
tions: simple two-screen views, single-screen tabbed views, modal view controllers,
and a view controller for selecting video and images.
Chapter 7, Connecting to the Network
This chapter discusses connecting to the Internet, browsing the Web, sending
email, and retrieving information.
Chapter 8, Handling Data
This chapter discusses how to handle data input, both from the application user
and programmatically, and how to parse XML and JSON documents. The chapter
also covers storing data in flat files and storing data with the SQLite database
engine.
xii | Preface
Chapter 9, Distributing Your Application
This chapter talks about how to add some final polish to your application and
walks you through the process of building your application for distribution, either
via ad hoc distribution or for the App Store.
Chapter 10, Using Sensors
This chapter discusses how to determine what hardware is available and illustrates
how to deal with the major sensors on the iPhone and iPod touch: the
accelerometer, magnetometer, camera, and GPS.
Chapter 11, Geolocation and Mapping
This chapter walks you through the process of building applications that make use
of the Core Location and MapKit frameworks.
Chapter 12, Integrating Your Application
This chapter shows you some of the tricks to integrate your application with the
iPhone’s software ecosystem, how to present user preferences with Settings Bun-
dles, and how to use custom URL schemes to launch your application. It also
discusses how to make use of the Media Player and Address Book.
Chapter 13, Other Native Platforms
This chapter deals with the PhoneGap and MonoTouch platforms for building
native applications for the iPhone and iPod touch that can be sold on the App Store.
The chapter then walks you through the installation process and building your first
Hello World application for both platforms.
Chapter 14, Going Further
This chapter provides a collection of pointers to more advanced material on the
topics we covered in the book, and material covering some of those topics that we
didn’t manage to talk about in the book.
Preface | xiii
This icon signifies a tip, suggestion, or general note.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at:
http://oreilly.com/catalog/9780596806439
Supplementary materials are also available at:
http://www.learningiphoneprogramming.com/
xiv | Preface
To comment or ask technical questions about this book, send email to:
[email protected]
For more information about our books, conferences, Resource Centers, and the
O’Reilly Network, see our website at:
http://www.oreilly.com
Acknowledgments
Books do not write themselves, but a book is also not the work of just a single person,
despite what it may say on the front cover. I’d like to thank my editor, Brian Jepson.
His hard work and constant prodding made the book better than it might otherwise
have been. I’d also like to offer more than thanks to my long-suffering wife, Gemma
Hobson. Without her support, encouragement, and willingness to make those small
(and sometimes larger) sacrifices that an author’s spouse has to make, this book
wouldn’t be in your hands today. Thank you. Finally to my son, Alex, who is as yet too
young to do more than chew on the cover, daddy’s home. I can only hope for your sake
that O’Reilly uses tasty paper.
Preface | xv
CHAPTER 1
Why Go Native?
When the iPhone was introduced, there was no native SDK. Apple claimed that one
wasn’t needed and that applications for the device should be built as web applications
using JavaScript, CSS, and HTML. This didn’t go down well with the developer com-
munity; they wanted direct access to the hardware and integration with Apple’s own
applications.
Only a few months after the iPhone’s release, the open source community had accom-
plished something that many thought impossible. Despite Apple locking the device
down, developers had gained access, reverse-engineered the SDK, and gone on to build
a free open source tool chain that allowed them to build native applications for the
device. At one point, it was estimated that more than one-third of the iPhones on the
market had been “jail broken” by their users, allowing them to run these unsanctioned
third-party applications.
This open source development effort is ongoing today, and if you want to know more,
I recommend iPhone Open Application Development, Second Edition by Jonathan
Zdziarski (O’Reilly). However, the book you hold in your hands isn’t about the open
source “hacker” SDK, because in March 2008 Apple publicly changed its mind and
released the first version of the native SDK to a waiting developer community. Whether
this release was in response to this effort, or perhaps because it was (the notoriously
secretive) Apple’s plan all along, we’ll probably never know.
1
It seemed that the users of the applications disagreed. It’s arguable why this is the case,
but it’s very hard to make native-looking web applications that can be reused across
many different platforms, though it is possible. Just as applications on the Mac desktop
that have been ported from Windows tend to stand out like a sore thumb by not quite
working as the user expects, web applications, especially those that are intended to be
used across different platforms, tend to do the same.
If you integrate your application into the iPhone ecosphere, make use of the possibilities
that the phone offers, and optimize your user interface (UI) for the device, the user
experience is much improved. It’s also really hard to write web applications that work
well when you need to design for a smaller screen, implying as it does a simpler UI and
less exposed functionality, without using native controls.
Sometimes it’s not about doing things that can’t be done; it’s about doing things faster,
and doing client-side error handling. For instance, the Apple iTunes and App Store
applications that are provided with the iPhone are actually web applications wrapped
inside native applications. Just like the iTunes Store on the Mac, the main display you
see is a web page, but the surrounding infrastructure is a native application. This means
that while the application can’t do a lot without an Internet connection, it can at least
start up.
But those are extreme examples. A lot of the applications in the App Store combine
remote data and native interfaces. Without access to the network, some of the UI is
generally disabled. However, native applications can be built to degrade gracefully
when the device’s network connection disappears or if it was never present in the first
Before you start writing code, you need to do some housekeeping. First, you’ll need to
install Xcode, Apple’s development environment, as well as the iPhone SDK. Both of
these are available directly from Apple, although you may already have Xcode on your
Mac OS X install DVD. However, before you can install the iPhone SDK, you’ll have
to register with Apple as a developer. If you enroll in one of the developer programs,
you’ll also need to create, download, and install a number of certificates and profiles
to allow you to deploy your applications onto your iPhone or iPod touch. Let’s get these
housekeeping tasks out of the way now so that you can get to the interesting bit—the
code—as quickly as you can.
5
If you are an existing Apple Developer Connection (ADC) member, or
if you have an iTunes or MobileMe account, you can use your existing
Apple ID to register as an iPhone developer. However, if you intend to
sell software commercially, you may want to create a new identity for
use with the program to keep it separate from your existing Apple ID.
You’ll initially be asked to either choose an existing Apple ID or create a new one. If
you create a new one, you’ll be asked for some details (e.g., email and physical ad-
dresses); if you choose an existing Apple ID, you’ll still need to confirm some of these
details, although they should be filled in with the most recent information Apple has.
You’ll also be asked to provide a professional profile, indicating what sort of applica-
tions you’ll be developing and whether you also develop for other mobile platforms.
Finally, you’ll need to agree to the developer license. After you do, a verification code
may be sent to the email address you registered with Apple, although this doesn’t hap-
pen in all cases. However, if this happens to you, the final step of registering as an
iPhone developer will be to verify your email address.
Apple Websites
You’ll use four main websites as part of the iPhone development process:
The iPhone Dev Center
This site is where you can get access to the latest versions of the iPhone SDK, along
with background technical information, API documentation, sample code, and
instructional videos. You need to be a registered iPhone developer to access the site.
The Developer Program Portal
This site is where you can generate and manage the certificates, provisioning pro-
files, approved devices, and other housekeeping tasks necessary to test your ap-
plications on the iPhone and iPod touch and prepare them for distribution. You’ll
need to be both a registered iPhone developer and enrolled in one of the iPhone
Developer Programs to access this site.
The App Store Resource Center
This site provides help and advice on how to distribute your application on the
App Store, including preparing your app for submission, understanding the App
Store approval process, and learning how to manage your apps on the App Store.
You’ll need to be both a registered iPhone developer and enrolled in the iPhone
Developer Standard Program to access this site.
iTunes Connect
This site provides you with the tools to manage your applications on the iTunes
App Store and your contracts with Apple. You’ll need to be both a registered
iPhone developer and enrolled in the iPhone Developer Standard Program to access
this site.
Your iPhone Developer Program membership lasts for 1 year and can
be renewed starting 60 days before the expiration date of your existing
membership. If you do not renew your membership, your ability to dis-
tribute your applications will be curtailed. In addition, your developer
and distribution certificates will be revoked. Finally, any applications
you have on the iTunes App Store will be removed.
You have two options when enrolling in the iPhone Developer Program. Most people
will want to register for the Standard Program, which costs $99 per year. This will allow
you to create free—or, once you’ve filled out some paperwork, commercial—
applications for the iPhone and iPod touch, and distribute them either via the App Store
or via the ad hoc distribution channel where you provide both the application binary
and a provisioning certificate to the end user.
The more expensive Enterprise Program, at $299, is intended for companies with more
than 500 employees that wish to create applications for in-house distribution. While
this program allows you to distribute your applications inside your own company, it
does not allow you to publish them for sale on the App Store. If you’re thinking about
selling your applications to the public, you need the Standard Program.
An iPhone Developer University Program is also available, but this is designed specif-
ically for higher education institutes looking to introduce iPhone development into
their curricula. Unless you’re an academic at such an institute, it’s unlikely that this
program will be applicable to you.
By this time I had grown a great stalwart lad, little above the middle
height, but broad and sinewy. I had made progress in all manly sports
and could fling the hammer almost as far as the Manor blacksmith,
while in leaping and running I had few rivals among lads of my age.
Also I was no bad swordsman, but could stand my own against all the
wiles of Tam Todd, and once even disarmed him to his own
unspeakable disgust. In my studies, which I pursued as diligently as I
could with no teachers and not over-many books, I had made some
little advance, having read through most of the Greek tragedians and
advanced some distance in the study of Plato; while in the Latin
tongue I had become such an adept that I could both read and write
it with ease.
When I had reached the mature age of eighteen, who should
come up into our parts but my famous relative, Master Gilbert Burnet,
the preacher at St. Clement's in London, of whom I have already
spoken. He was making a journey to Edinburgh and had turned out of
his way to revive an old acquaintance. My father was overjoyed to see
him and treated him to the best the house could produce. He stayed
with us two days, and I remember him still as he sat in a great
armchair opposite my father, with his broad velvet cap and grey,
peaked beard, and weighty brows. Yet when he willed, though for
ordinary a silent man, he could talk as gaily and wittily as any town
gallant; so much indeed that my father, who was somewhat hard to
please, declared him the best companion he ever remembered.
Before he left, Master Burnet examined me on my progress in
polite learning, and finding me well advanced, he would have it that I
should be sent forthwith to Glasgow College. He exacted a promise
from my father to see to this, and left behind him, when he departed,
letters of introduction to many of the folk there, for he himself had, at
one time, been professor of divinity in the place. As for myself, I was
nothing loth to go, and see places beyond Tweeddale and add to my
stock of learning; for about this time a great enthusiasm for letters
had seized me (which I suppose happens at some time or other to
most men), and I conceived my proper vocation in life to be that of
the scholar. I have found in an old manuscript book a list of the titles
of imaginary works, editions, poems, treatises, all with my unworthy
name subscribed as the author. So it was settled that I should ride to
Glasgow and take lodgings in the town for the sake of the college
classes.
I set out one November morning, riding Maisie alone, for no
student was allowed to have a servant, nor any one below the degree
of Master of Arts. The air was keen and frosty, and I rode in high
fettle by the towns of Biggar and Lanark to the valley of the Clyde. I
lay all night at Crossford in the house of a distant relative. Thence the
next day I rode to Hamilton and in the evening came to the bridge of
the Clyde at Glasgow. Then I presented myself to the Principal and
Regents of the college and was duly admitted, putting on the red
gown, the badge of the student class, than which I believe there is no
more hideous habiliment.
The college in those days was poor enough, having been well-
nigh ruined by the extortions of Lord Middleton and his drunken crew;
and it had not yet benefited by the rich donations of the Reverend
Zachary Boyd of the Barony Kirk. Still, the standard of learning in the
place was extraordinarily high, especially in dialectic and philosophy—
a standard which had been set by the famous Andrew Melville when
he was a professor in the place. I have heard disputations there in the
evenings between the schoolmen and the new philosophers, the like
of which could scarcely be got from the length and breadth of the
land.
Across the High Street were the college gardens and green
pleasant orchards where the professors were wont to walk and the
scholars to have their games. Through the middle ran the clear
Molendinar Burn, so called by the old Romans, and here I loved to
watch the trout and young salmon leaping. There was a severe rule
against scholars fishing in the stream, so I was fain to content myself
with the sight. For soon a violent fit of home-sickness seized me, and
I longed for the rush of Tweed and the pleasant sweep of Manor; so it
was one of my greatest consolations to look at this water and fancy
myself far away from the town. One other lad who came from
Perthshire used to come and stand with me and tell me great tales of
his fishing exploits; and I did likewise with him till we became great
companions. Many afternoons I spent here, sometimes with a book
and sometimes without one; in the fine weather I would lie on the
grass and dream, and in rough, boisterous winter days I loved to
watch the Molendinar, flooded and angry, fling its red waters against
the old stones of the bridge.
No one of us was permitted to carry arms of any kind, so I had to
sell my sword on my first coming to the town. This was a great
hardship to me, for whereas when I carried a weapon I had some
sense of my own importance, now I felt no better than the rest of the
unarmed crowd about me. Yet it was a wise precaution, for in other
places where scholars are allowed to strut like cavaliers there are
fights and duels all the day long, so that the place looks less like an
abode of the Muses than a disorderly tavern. Nevertheless, there were
many manly exercises to be had, for in the greens in the garden we
had trials of skill at archery and golf and many other games of the
kind. At the first mentioned I soon became a great master, for I had a
keen eye from much living among woods and hills, and soon there
was no one who could come near me at the game. As for golf, I
utterly failed to excel; and indeed it seems to me that golf is like the
divine art of poetry, the gift for which is implanted in man at his birth
or not at all. Be that as it may, I never struck a golf-ball fairly in my
life, and I misdoubt I never shall.
As for my studies, for which I came to the place, I think I made
great progress. For after my first fit of home-sickness was over, I fell
in with the ways of the college, and acquired such a vast liking for the
pursuit of learning that I felt more convinced than ever that
Providence had made me for a scholar. In my classes I won the
commendation of both professors; especially in the class of dialectic,
where an analysis of Aristotle's method was highly praised by Master
Sandeman, the professor. This fine scholar and accomplished
gentleman helped me in many ways, and for nigh two months, when
he was sick of the fever, I lectured to his class in his stead. We were
all obliged to talk in the Latin tongue and at first my speech was stiff
and awkward enough, but by and by I fell into the way of it and
learned to patter it as glibly as a Spanish monk.
It may be of interest to those of my house that I should give
some account of my progress in the several studies, to show that our
family is not wholly a soldiering one. In Greek I studied above others
the works of Plato, delighting especially in his Phaedo, which I had
almost by heart; Aristotle likewise, though I read but little of him in
his own tongue. I completed a translation of the first part of Plato's
Republic into Latin, which Master Sandeman was pleased to say was
nigh as elegant as George Buchanan's. Also I was privileged to
discover certain notable emendations in the text of this work, which I
sent in manuscript to the famous Schookius of Groningen, who
incorporated them in his edition then in preparation, but after the
fashion of Dutchmen sent me no thanks.
As regards philosophy, which I hold the most divine of all studies,
I was in my first year a most earnest Platonic; nay, I went farther
than the master himself, as is the way of all little minds when they
seek to comprehend a great one. In those days I went about in sober
attire and strove in all things to order my life according to the rules of
philosophy, seeking to free myself from all disturbing outside powers
and live the life of pure contemplation. I looked back with unutterable
contempt on my past as a turbid and confused medley, nor did I seek
anything better in life than quiet and leisure for thought and study. In
such a condition I spent the first month of my stay at Glasgow.
Then the Platonic fit left me and I was all for Aristotle and the
Peripatetics. Here, at last, thought I, have I got the siccum lumen,
which Heraclitus spoke of: and his distinct and subtle reasoning
seemed to me to be above doubt. And indeed I have never wondered
at the schoolmen and others who looked upon Aristotle as having
reached the height of human wisdom, for his method is so all-
embracing and satisfying that it breeds wonder in the heart of any
man; and it affords so sure a bottom for thought that men become
Aristotelians.
In the midsummer months I went down to Tweeddale again,
where I astonished my father and all in the place with my new
learning, and also grieved them. For I had no love for fishing or
shooting; I would scarce ride two miles for the pleasure of it; my
father's tales, in which I delighted before, had grown tiresome; and I
had no liking for anything save bending over books. When I went to
Dawyck to see Marjory, she knew not what had come over me, I was
so full of whims and fancies. "O John," she said, "your face is as white
as a woman's, and you have such a horrible cloak. Go and get
another at once, you silly boy, and not shame your friends." Yet even
Marjory had little power over me, for I heeded her not, though
aforetime I would have ridden posthaste to Peebles and got me a new
suit, and painted my face if I had thought that thereby I would
pleasure her.
When the autumn came again I returned to college more inclined
than ever for the life of a scholar. I fell to my studies with renewed
zeal, and would doubtless have killed myself with work had I not been
nearly killed with the fever, which made me more careful of my
health. And now, like the weathercock I was, my beliefs shifted yet
again. For studying the schoolmen, who were the great upholders of
Aristotle, I found in them so many contradictions and phantasies
which they fathered on their master that, after reading the diatribes
of Peter Ramus and others against him, I was almost persuaded that I
had been grievously misled. Then, at last, I saw hat the fault lay not
in Aristotle but in his followers, who sought to find in him things that
were beyond the compass of his thought. So by degrees I came round
toward the new philosophy, which a party in the college upheld. They
swore by the great names of Bacon and Galileo and the other natural
philosophers, but I hesitated to follow them, for they seemed to me
to disdain all mental philosophy, which I hold is the greater study. I
was of this way of thinking when I fell in one day with an English
book, a translation of a work by a Frenchman, one Renatus Descartes,
published in London in the year 1649. It gave an account of the
progress in philosophy of this man, who followed no school, but,
clearing his mind of all presuppositions, instituted a method for
himself. This marked for me the turning point; for I gave in my
allegiance without hesitation to this philosopher, and ever since I have
held by his system with some modifications. It is needless for me to
enter further into my philosophy, for I have by me a written exposition
of the works of this Descartes with my own additions, which I intend,
if God so please, to give soon to the world.
For two years I abode at the college, thinking that I was destined
by nature for a studious life, and harbouring thoughts of going to the
university of Saumur to complete my studies. I thought that my spirit
was chastened to a fit degree, and so no doubt it was, for those who
had feared me at first on account of my heavy fist and straightforward
ways, now openly scoffed at me without fear of punishment. Indeed,
one went so far one day as to jostle me off the causeway, and I made
no return, but went on as if nothing had happened, deeming it
beneath a wise man to be distracted by mundane trifles. Yet, mind
you, in all this there was nothing Christian or like unto the meekness
of our Master, as I have seen in some men; but rather an absurd
attempt to imitate those who would have lived very differently had
their lot been cast in our hot and turbid days.
How all this was changed and I veered round of a sudden to the
opposite I must hasten to tell. One April day, towards the close of my
second year, I was going up the High Street toward the Cathedral with
a great parcel of books beneath my arm, when I heard a shouting and
a jingling, and a troop of horse came down the street. I stood back
into the shelter of a doorway, for soldiers were wont to bear little love
to scholars, and I did not care to risk their rough jests. From this
place I watched their progress, and a gallant sight it was. Some
twenty men in buff jerkins and steel headpieces rode with a fine
clatter of bridles and clank of swords. I marked their fierce sun-brown
faces and their daredevil eyes as they looked haughtily down on the
crowd as on lower beings. And especially I marked their leader. He sat
a fine bay horse with ease and grace; his plumed hat set off his high-
coloured face and long brown curls worn in the fashion of the day;
and as he rode he bowed to the people with large condescension. He
was past in a second, but not before I had recognized the face and
figure of my cousin Gilbert.
I stood for some minutes staring before me, while the echoes of
the horses' hooves died away down the street. This, I thought, is the
destiny of my cousin, only two years my elder, a soldier, a gentleman,
a great man in his place; while I am but a nameless scholar, dreaming
away my manhood in the pursuits of a dotard. I was so overwhelmed
with confusion that I stood gaping with a legion of thoughts and
opposing feelings running through my brain. Then all the old fighting
spirit of my house rose within me. By Heaven, I would make an end
of this; I would get me home without delay; I would fling my books
into the Clyde; I would go to the wars; I would be a great cavalier,
and, by the Lord, I would keep up the name of the house! I was
astonished myself at the sudden change in my feelings, for in the
space of some ten minutes a whole age had passed for me, and I had
grown from a boy to some measure of manhood. I came out from the
close-mouth with my head in the air and defiance against all the
world in my eye.
Before I had gone five spaces I met the lad who had jostled me
aforetime, a big fellow of a raw-boned Ayrshire house, and before he
could speak I had him by the arm and had pulled him across the way
into the college gardens. There I found a quiet green place, and
plucking off my coat I said, "Now, Master Dalrymple, you and I have a
small account to settle." With that we fell to with our fists, and in the
space of a quarter of an hour I had beaten him so grievously that he
was fain to cry for mercy. I let him go, and with much whimpering he
slunk away in disgust.
Then I went into the town and bought myself a new blade and a
fine suit of clothes—all with the greatest gusto and lightness of heart.
I went to the inn where Maisie was stabled and bade them have her
ready for me at the college gate in an hour. Then I bade good-bye to
all my friends, but especially to Master Sandeman, from whom I was
loth to part. I did not fling my books into the Clyde as at first I
proposed, but left injunctions that they were to be sent by the carrier.
So, having paid all my debts, for my father had kept me well
appointed with money, I waved a long farewell and set out for my
own country.
CHAPTER V
COUSINLY AFFECTION
CHAPTER VI
HOW MASTER GILBERT BURNET PLAYED A GAME
AND WAS CHECKMATED
That night I was too wearied and sore in body to sleep. My mind also
was troubled, for I had made an enemy of my cousin, who, as I knew,
was not of a nature to forgive readily. His words about Marjory had
put me into a ferment of anxiety. Here was my love, bound to me by
no promise, at the mercy of all the gallants of the countryside. Who
was I, to call myself her lover, when, as yet, no word of love had
passed between us? Yet, in my inmost heart, I knew that I might get
the promise any day I chose. Then thoughts of my cousin came to
trouble me. I feared him no more than a fly in matters betwixt man
and man; but might he not take it into his head to make love to the
mistress of Dawyck? and all maids dearly love a dashing cavalier. At
length, after much stormy indecision, I made up my mind. I would
ride to Dawyck next morn and get my lady's word, and so forestall
Gilbert, or any other.
I woke about six o'clock; and, looking out from the narrow
window, for Barns had been built three hundred years before, I saw
that the sky was cloudless and blue, and the morning as clear as
could be seen in spring. I hastily dressed, and, getting some slight
breakfast from Jean Morran, saddled Maisie, who was now as active
as ever, and rode out among the trees. I feared to come to Dawyck
too early, so I forded Tweed below the island, and took the road up
the further bank by Lyne and Stobo. All the world was bright; an early
lark sang high in the heaven; merles and thrushes were making fine
music among the low trees by the river. The haze was lifting off the
great Manor Water hills; the Red Syke, the scene of the last night's
escapade, looked very distant in the morning light; and far beyond all
Dollar Law and the high hills about Manorhead were flushed with
sunlight on their broad foreheads. A great gladness rose in me when I
looked at the hills, for they were the hills of my own country; I knew
every glen and corrie, every water and little burn. Before me the Lyne
Water hills were green as grass with no patch of heather, and to the
left, the mighty form of Scrape, half-clothed in forest, lay quiet and
sunlit. I know of no fairer sight on earth; and this I say, after having
travelled in other countries, and seen something of their wonders; for,
to my mind, there is a grace, a wild loveliness in Tweedside, like a
flower-garden on the edge of a moorland, which is wholly its own.
I crossed Lyne Water by the new bridge, just finished in the year
before, and entered the wood of Dawyck. For this great forest
stretches on both sides of Tweed, though it is greater on the side on
which stands the house. In the place where I rode it was thinner, and
the trees smaller, and, indeed, around the little village of Stobo, there
lies an open part of some fields' width. At the little inn there, I had a
morning's draught of ale, for I was somewhat cold with riding in the
spring air. Then I forded Tweed at a place called the Cow Ford, and,
riding through a wide avenue of lime-trees, came in sight of the grey
towers of Dawyck.
I kept well round to the back, for I did not care that the serving-
folk should see me and spread tales over all the countryside. I knew
that Marjory's window looked sharp down on a patch of green lawn,
bordered by lime-trees, so I rode into the shadow and dismounted. I
whistled thrice in a way which I had, and which Marjory had learned
to know long before, when we were children, and I used to come and
beguile her out for long trampings among the hills. To-day it had no
effect, for the singing of birds drowned my notes, so I had nothing
left but to throw bits of bark against her window. This rude expedient
met with more success than it deserved, for in a minute I saw her
face behind the glass. She smiled gladly when she saw me, and
disappeared, only to appear again in the little door beside the lilacs.
She had no hat, so her bright hair hung loose over her neck and was
blown about by the morning winds. Her cheeks were pink and white,
like apple-blossom, and her lithe form was clad in a dress of blue
velvet, plainly adorned as for a country maiden. A spray of lilac was in
her breast, and she carried a bunch of sweet-smelling stuff in her
hands.
She came gladly towards me, her eyes dancing with pleasure.
"How soon you have returned! And how brave you look," said she,
with many more pretty and undeserved compliments.
"Ay, Marjory," I answered, "I have come back to Tweeddale, for I
have had enough of Glasgow College and books, and I was wearying
for the hills and Tweed and a sight of your face. There are no maidens
who come near to you with all their finery. You are as fair as the
spring lilies in the garden at Barns."
"Oh, John," she laughed, "where did you learn to pay fine
compliments? You will soon be as expert at the trade as any of them.
I met a man yesterday in the woods who spoke like you, though with
a more practised air; but I bade him keep his fine words for his fine
ladies, for they suited ill with the hills and a plain country maid."
At this, I must suppose that my brows grew dark, for she went
on laughingly.
"Nay, you are not jealous? It ill becomes a scholar and a
philosopher as you are, Master John, to think so much of an idle
word. Confess, sir, that you are jealous. Why, you are as bad as a lady
in a play."
I could not make out her mood, which was a new one to me—a
mocking pleasant raillery, which I took for the rightful punishment of
my past follies.
"I am not jealous," I said, "for jealousy is a feeling which needs
an object ere it can exist. No man may be jealous, unless he has
something to be jealous about."
"John, John," she cried, and shook her head prettily, "you are
incorrigible. I had thought you had learned manners in the town, and
behold, you are worse than when you went away. You come here, and
your first word to me is that I am nothing."
"God knows," I said, "I would fain be jealous, and yet—" I
became awkward and nervous, for I felt that my mission was not
prospering, and that I was becoming entangled in a maze of
meaningless speech. The shortest and plainest way is still the best in
love as in all things.
But I was not to be let off, and she finished my sentence for me.
"If only you could find a worthy object for your feeling, you mean,"
she said. "Very well, sir, since I am so little valued in your eyes, we
will speak no more on the matter."
"Marjory," I said, coming to the matter at once, "you and I have
been old comrades. We have fished and walked together, we have
climbed the hills and ridden in the meadows. I have done your
bidding for many years."
"True, John," she said with an accent of grudging reminiscence,
"you have dragged me into many a pretty pickle. I have torn my dress
on rough rocks and soaked my shoes in bogs, all in your company.
Surely we have had a brave time together."
"You met a man in the wood yesterday who would fain have
made love to you. That man was my cousin Gilbert."
"Oh," she replied in a tone of mock solemnity and amused
wonder, for I had blurted out my last words like the last dying
confession of some prisoner. "Verily you are honoured in your
cousinship, John."
"It is against him and such as him that I would protect you," I
said.
"Nay," she cried, with an affected remonstrance. "I will have no
fighting between cousins on my account. I will even defend myself, as
Alison did when the miller made love to her."
"O Marjory," I burst out, "will you not give me this right to defend
you? We have been old companions, but it was only yesterday that I
knew how dearly I loved you. I have had more cares since yester-
night than ever in my life. We have been comrades in childhood; let
us be comrades on the rough paths of the world."
I spoke earnestly, and her face, which had been filled with
mockery, changed gently to something akin to tenderness.
"How little you know of women!" she cried. "I have loved you for
years, thinking of you at all times, and now you come to-day,
speaking as if you had scarce seen me before. Surely I will bear you
company in life, as I have been your comrade at its beginning."
What followed I need scarce tell, since it is but part of the old
comedy of life, which our grandfathers and grandmothers played
before us, and mayhap our grand-children will be playing even now
when our back is turned. Under the spring sky among the lilies we
plighted our troth for the years, and I entered from careless youth
into the dim and resolute region of manhood.
With a great joy in my heart I rode home. I took the high way
over the shoulder of Scrape, for I knew that few folk ever went that
road, and I wished to be alone. The birds were singing, the fresh
clean air was blowing on my face, and the primroses and wind-flowers
made a gay carpet under my horse's feet. All the earth seemed to
partake in my gladness. It was a good world, I thought, full of true
hearts, fair faces, and much good; and though I have seen much
wickedness and sorrow in my day, I am still of the same way of
thinking. It is a brave world; a royal world for brave-hearted men.
When I came to Barns I found that my cousin had gone out an
hour since and left my father greatly wondering at my absence. He
sat in the chair by the fireplace, looking more withered and old than I
had ever seen him. My heart smote me for not staying at his side, and
so I sat down by him and told him many things of my doings in
Glasgow, and how I desired above all things to see the world, having
had my fill of books and colleges. Then I told him what he had long
guessed, of my love for Marjory Veitch and the promise which she had
given me. He heard me in silence, but when he spoke, his words were
cheerful, for he had long liked the lass. He made no refusal, too, to
the rest of my plans. "You shall go and see the world, John," he said,
"and take my blessing with you. It ill becomes a young mettlesome
lad in these stirring times to lounge at home, when he might be
wearing a steel breastplate in the King's Guards, or trying the
manners of twenty nations. Though I could wish you to bide at home,
for I am an old broken man with few pleasures, and I love the sight of
your face."
"Nay, I will never leave you," I said, "an you wish it. I am young
yet and a boy's road is a long road. Time enough for all."
After this I went out to see if the Weasel had come to any mishap
in the last night's ride. I found him as stout as ever, so I saddled him
and rode away by the green haughlands up the valley of the Manor,
for I longed for motion and air to relieve my spirit: and coming home
in the afternoon, I found my cousin returned and sitting with my
father in the dining-hall.
He glanced sharply at me when I entered, and I saw by his looks
that he was in no good temper. His heavy face was flushed and his
shaggy eyebrows were lowered more than their wont.
"Where have you been, Gilbert?" I asked. "I found you gone
when I came back in the morning."
"I took my horse down to Peebles to the farrier. Its knees were
sorely hurt last night on your infernal hills."
Now I knew that this was a lie, for I had looked at his horse
before I went out in the morning, and its wounds were so slight that
it would have been mere folly to take him to a farrier; and Gilbert, I
well knew, was not the man to be in error where horses were
concerned. So I judged that he had ridden in the contrary direction,
and gone to Dawyck, and, as I inferred from his sour looks, met with
no good reception there. I could afford to be generous; I felt a sort of
half-pity for his discomfiture, and forbore to ask him any further
questions.
We sat down to supper, he and I and my father, in a sober frame
of mind. I was full of my own thoughts, which were of the
pleasantest; my cousin was plainly angry with something or other;
and my father, in his weakness dimly perceiving that all was not right,
set himself to mend matters by engaging him in talk.
"You're a good shot with the musket, they tell me, Gibbie," he
said, using the old name which he had called him by when he first
came to Barns as a boy, "and I was thinking that it would be a rare
ploy for you and John to go down the water to Traquair, where
Captain Keith's horse are lying. He is an old friend of mine, and would
be blithe to see any of my kin. They tell me he has great trials of skill
in all exercises, and that he has gathered half the gentry in the place
about him."
"John," said my cousin in a scornful voice, "John is too busily
employed at Dawyck to care much for anything else. A flighty maid is
a sore burden on any man."
"I would have you learn, Master Gilbert," I said angrily, "to speak
in a better way of myself and my friends. You may be a very great
gentleman elsewhere, but you seem to leave your gentility behind
when you come here."
Now my cousin and I were of such opposite natures that I took
most things seriously, while he found matter for a jest in all—yet not
in full good-nature, but with a touch of acrid satire.
"Even a barn-door cock will defend his own roost. How one sees
the truth of proverbs!"
And then he added that which I will not set down, but which
brought my father and myself to our feet with flashing eyes and
quivering lips. I would have spoken, but my father motioned me to be
silent.
"Gilbert," he said, his voice shaking with age and anger, "you will
leave this house the morn. I will have no scoundrelly fellow of your
kidney here. You are no true nephew of mine, and God pity the father
that begat you."
My cousin smiled disdainfully and rose from his chair. "Surely I
will go and at once when my hospitable uncle bids me. The
entertainment in this damned hole is not so good as to keep me long.
As for you, Cousin John," and he eyed me malignantly, "you and I will
meet some day, where there are no dotards and wenches to come
between us. Then I promise you some sport. Till then, farewell. I will
down to Peebles to-night and trouble you no more." With a wave of
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookname.com