iOS 12 Programming Fundamentals With Swift Swift Xcode and Cocoa Basics 5th Edition Matt Neuburg Download PDF
iOS 12 Programming Fundamentals With Swift Swift Xcode and Cocoa Basics 5th Edition Matt Neuburg Download PDF
com
https://textbookfull.com/product/ios-12-
programming-fundamentals-with-swift-swift-xcode-
and-cocoa-basics-5th-edition-matt-neuburg/
https://textbookfull.com/product/ios-12-programming-fundamentals-with-
swift-swift-xcode-and-cocoa-basics-5th-edition-matt-neuburg-2/
textbookfull.com
https://textbookfull.com/product/ios-12-programming-fundamentals-with-
swift-swift-xcode-and-cocoa-basics-5th-edition-matt-neuburg-3/
textbookfull.com
https://textbookfull.com/product/ios-10-programming-fundamentals-with-
swift-swift-xcode-and-cocoa-basics-3rd-edition-matt-neuburg/
textbookfull.com
https://textbookfull.com/product/the-rough-guide-to-poland-8th-
edition-rough-guides/
textbookfull.com
https://textbookfull.com/product/civil-society-3rd-edition-michael-
edwards/
textbookfull.com
https://textbookfull.com/product/norms-in-the-wild-how-to-diagnose-
measure-and-change-social-norms-1st-edition-cristina-bicchieri/
textbookfull.com
https://textbookfull.com/product/coding-art-the-four-steps-to-
creative-programming-with-the-processing-language-yu-zhang/
textbookfull.com
https://textbookfull.com/product/advances-in-chromatography-
volume-57-1st-edition-nelu-grinberg-editor/
textbookfull.com
Xc
C o 0,
od
ve a n
e1
rs d
i O Sw
S 1 if t
2, 4
.2
iOS 12
Programming
Fundamentals
with Swift
SWIFT, XCODE, AND COCOA BASICS
Matt Neuburg
FIFTH EDITION
iOS 12 Programming
Fundamentals with Swift
Swift, Xcode, and Cocoa Basics
Matt Neuburg
Boston
iOS 12 Programming Fundamentals with Swift, Fifth Edition
by Matt Neuburg
Copyright © 2018 Matt Neuburg. All rights reserved.
Printed in the United States of America.
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://oreilly.com/safari). For more information, contact our corporate/insti‐
tutional sales department: 800-998-9938 or corporate@oreilly.com.
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. iOS 12 Programming Fundamentals
with Swift, the image of a harp seal, and related trade dress are trademarks of O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the information and
instructions contained in this work are accurate, the publisher and the author disclaim all responsibility
for errors or omissions, including without limitation responsibility for damages resulting from the use of
or reliance on this work. Use of the information and instructions contained in this work is at your own
risk. If any code samples or other technology this work contains or describes is subject to open source
licenses or the intellectual property rights of others, it is your responsibility to ensure that your use
thereof complies with such licenses and/or rights.
ISBN: 978-1-492-04455-0
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Part I. Language
1. The Architecture of Swift. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Ground of Being 3
Everything Is an Object? 5
Three Flavors of Object Type 6
Variables 6
Functions 8
The Structure of a Swift File 9
Scope and Lifetime 11
Object Members 13
Namespaces 13
Modules 14
Instances 15
Why Instances? 17
The Keyword self 19
Privacy 20
Design 22
Object Types and APIs 23
Instance Creation, Scope, and Lifetime 25
Summary and Conclusion 25
2. Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Function Parameters and Return Value 27
Void Return Type and Parameters 31
Function Signature 32
External Parameter Names 32
iii
Overloading 34
Default Parameter Values 35
Variadic Parameters 35
Ignored Parameters 36
Modifiable Parameters 37
Function in Function 40
Recursion 42
Function As Value 42
Anonymous Functions 45
Define-and-Call 51
Closures 52
How Closures Improve Code 54
Function Returning Function 55
Closure Setting a Captured Variable 58
Closure Preserving Its Captured Environment 59
Escaping Closures 60
Curried Functions 60
Function References and Selectors 62
Function Reference Scope 64
Selectors 65
iv | Table of Contents
Subscripts 132
Nested Object Types 135
Instance References 135
Enums 137
Raw Values 138
Associated Values 140
Enum Case Iteration 142
Enum Initializers 143
Enum Properties 144
Enum Methods 145
Why Enums? 146
Structs 147
Struct Initializers, Properties, and Methods 147
Struct As Namespace 149
Classes 149
Value Types and Reference Types 150
Subclass and Superclass 156
Class Initializers 161
Class Deinitializer 169
Class Properties and Methods 170
Polymorphism 172
Casting 175
Casting Down 176
Type Testing and Casting Down Safely 176
Type Testing and Casting Optionals 178
Bridging to Objective-C 178
Type References 180
From Instance to Type 180
Type as Value 181
The Keyword Self 183
Comparing Types 184
Summary of Type Terminology 185
Protocols 186
Why Protocols? 187
Protocol Type Testing and Casting 189
Declaring a Protocol 190
Protocol Composition 191
Optional Protocol Members 192
Class Protocol 194
Implicitly Required Initializers 195
Literal Convertibles 196
Generics 197
Table of Contents | v
Generic Declarations 200
Contradictory Resolution 202
Type Constraints 203
Explicit Specialization 205
Generic Invariance 207
Associated Type Chains 208
Where Clauses 210
Extensions 213
Extending Object Types 213
Extending Protocols 215
Extending Generics 217
Umbrella Types 219
Any 219
AnyObject 221
AnyClass 223
Collection Types 224
Array 224
Dictionary 240
Set 247
vi | Table of Contents
6. Anatomy of an Xcode Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
New Project 313
The Project Window 315
The Navigator Pane 317
The Utilities Pane 322
The Editor 323
The Project File and Its Dependents 325
What’s In the Project Folder 326
Groups 327
The Target 328
Build Phases 328
Build Settings 330
Configurations 331
Schemes and Destinations 333
From Project to Built App 335
Build Settings 338
Property List Settings 338
Nib Files 339
Additional Resources 340
Code Files 342
Frameworks and SDKs 343
The App Launch Process 345
The Entry Point 345
UIApplicationMain 346
App Without a Storyboard 348
Renaming Parts of a Project 349
8. Documentation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
The Documentation Window 385
Class Documentation Pages 386
Quick Help 390
Symbol Declarations 392
Header Files 393
Sample Code 394
Internet Resources 394
Table of Contents | ix
NSSet and Friends 496
NSIndexSet 497
NSNull 498
Immutable and Mutable 498
Property Lists 500
Codable 501
Accessors, Properties, and Key–Value Coding 504
Swift Accessors 505
Key–Value Coding 507
Uses of Key–Value Coding 508
KVC and Outlets 510
Cocoa Key Paths 510
The Secret Life of NSObject 511
x | Table of Contents
Autorelease Pool 547
Memory Management of Instance Properties 549
Retain Cycles and Weak References 550
Unusual Memory Management Situations 552
Notification Observers 552
KVO Observers 554
Timers 554
Other Unusual Situations 556
Nib Loading and Memory Management 556
Memory Management of CFTypeRefs 557
Property Memory Management Policies 559
Debugging Memory Management Mistakes 561
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
Table of Contents | xi
Preface
On June 2, 2014, Apple’s WWDC keynote address ended with a shocking announce‐
ment: “We have a new programming language.” This came as a huge surprise to the
developer community, which was accustomed to Objective-C, warts and all, and
doubted that Apple could ever possibly relieve them from the weight of its venerable
legacy. The developer community, it appeared, had been wrong.
Having picked themselves up off the floor, developers immediately began to consider
this new language — Swift — studying it, critiquing it, and deciding whether to use it.
My own first move was to translate all my existing iOS apps into Swift; this was
enough to convince me that Swift deserved to be, and probably would be, adopted by
new students of iOS programming, and that my books, therefore, should henceforth
assume that readers are using Swift.
That decision has proven prophetic. Programmers of iOS have flocked to Swift in
increasing numbers, and Swift itself has only improved. My iOS apps (such as Diabel‐
li’s Theme, LinkSame, Zotz!, TidBITS News, and my Latin and Greek flashcard apps)
have all been rewritten in Swift, and are far easier for me to understand and maintain
than their Objective-C originals.
Xcode 10 comes with Swift 4.2. The language has evolved greatly in its details and in
the nature of its integration with the Cocoa libraries that underlie iOS programming,
but its spirit has remained constant. The Swift language is designed from the ground
up with these salient features:
Object-orientation
Swift is a modern, object-oriented language. It is purely object-oriented: “Every‐
thing is an object.”
Clarity
Swift is easy to read and easy to write. Its syntax is clear, consistent, and explicit,
with few hidden shortcuts and minimal syntactic trickery.
xiii
Safety
Swift enforces strong typing to ensure that it knows, and that you know, what the
type of every object reference is at every moment.
Economy
Swift is a fairly small language, providing some basic types and functionalities
and no more. The rest must be provided by your code, or by libraries of code that
you use — such as Cocoa.
Memory management
Swift manages memory automatically. You will rarely have to concern yourself
with memory management.
Cocoa compatibility
The Cocoa APIs are written primarily in C and Objective-C. Swift is explicitly
designed to interface with most of the Cocoa APIs.
These features make Swift an excellent language for learning to program iOS.
The alternative, Objective-C, still exists, and you can use it if you like. Indeed, it is
easy to write an app that includes both Swift code and Objective-C code; and you may
have reason to do so. Objective-C, however, lacks the very advantages that Swift
offers. Objective-C agglomerates object-oriented features onto C. It is therefore only
partially object-oriented; it has both objects and scalar data types, and its objects have
to be slotted into one particular C data type (pointers). Its syntax can be difficult and
tricky; reading and writing nested method calls can make one’s eyes glaze over, and it
invites hacky habits such as implicit nil-testing. Its type checking can be and fre‐
quently is turned off, resulting in programmer errors where a message is sent to the
wrong type of object and the program crashes.
Recent revisions and additions to Objective-C — ARC, synthesis and autosynthesis,
improved literal array and dictionary syntax, blocks — have made it easier and more
convenient, but such patches have also made the language even larger and possibly
even more confusing. Because Objective-C must encompass C, there are limits to
how far it can be extended and revised. Swift, on the other hand, is a clean start. If
you were to dream of completely revising Objective-C to create a better Objective-C,
Swift might be what you would dream of. It puts a modern, rational front end
between you and the Cocoa Objective-C APIs.
Still, the reader may also need some awareness of Objective-C (including C). The
Foundation and Cocoa APIs, the built-in commands with which your code must
interact in order to make anything happen on an iOS device, are still written in C and
Objective-C. In order to interact with them, you might have to know what those lan‐
guages would expect.
xiv | Preface
Therefore, although I do not attempt to teach Objective-C in this book, I do describe
it in enough detail to allow you to read it when you encounter it in the documenta‐
tion and on the Internet, and I occasionally show some Objective-C code. Part III, on
Cocoa, is really all about learning to think the way Objective-C thinks — because the
structure and behavior of the Cocoa APIs are fundamentally based on Objective-C.
And the book ends with an appendix that details how Swift and Objective-C commu‐
nicate with one another, as well as explaining how your app can be written partly in
Swift and partly in Objective-C.
Preface | xv
Swift playgrounds or the REPL. My focus here is real-life iOS programming, and
my explanation of Swift therefore concentrates on those common, practical
aspects of the language that, in my experience, actually come into play in the
course of programming iOS.
• Part II turns to Xcode, the world in which all iOS programming ultimately takes
place. It explains what an Xcode project is and how it is transformed into an app,
and how to work comfortably and nimbly with Xcode to consult the documenta‐
tion and to write, navigate, and debug code, as well as how to bring your app
through the subsequent stages of running on a device and submission to the App
Store. There is also a very important chapter on nibs and the nib editor (Interface
Builder), including outlets and actions as well as the mechanics of nib loading;
however, such specialized topics as autolayout constraints in the nib are post‐
poned to the other book.
• Part III introduces the Cocoa Touch framework. When you program for iOS, you
take advantage of a suite of frameworks provided by Apple. These frameworks,
taken together, constitute Cocoa; the brand of Cocoa that provides the API for
programming iOS is Cocoa Touch. Your code will ultimately be almost entirely
about communicating with Cocoa. The Cocoa Touch frameworks provide the
underlying functionality that any iOS app needs to have. But to use a framework,
you have to think the way the framework thinks, put your code where the frame‐
work expects it, and fulfill many obligations imposed on you by the framework.
To make things even more interesting, Cocoa uses Objective-C, while you’ll be
using Swift: you need to know how your Swift code will interface with Cocoa’s
features and behaviors. Cocoa provides important foundational classes and adds
linguistic and architectural devices such as categories, protocols, delegation, and
notifications, as well as the pervasive responsibilities of memory management.
Key–value coding and key–value observing are also discussed here.
The reader of this book will thus get a thorough grounding in the fundamental
knowledge and techniques that any good iOS programmer needs. The book itself
doesn’t show how to write any particularly interesting iOS apps, but it does constantly
use my own real apps and real programming situations to illustrate and motivate its
explanations. And then you’ll be ready for Programming iOS 12, of course!
Versions
This book is geared to Swift 4.2, iOS 12, and Xcode 10.
In general, only very minimal attention is given to earlier versions of iOS and Xcode.
It is not my intention to embrace in this book any detailed knowledge about earlier
versions of the software, which is, after all, readily and compendiously available in my
earlier books. The book does contain, nevertheless, a few words of advice about back‐
ward compatibility (especially in Chapter 9).
xvi | Preface
A word about method names. I generally give method names in Swift, in the style of a
function reference (as described in Chapter 2) — that is, the name plus parentheses
containing the parameter labels followed by colon. Now and then, if a method is
already under discussion and there is no ambiguity, I’ll use the bare name. In a few
places, such as Appendix A, where the Objective-C language is explicitly under dis‐
cussion, I use Objective-C method names.
Please bear in mind that Apple continues to make adjustments to the Swift language.
I have tried to keep my code up-to-date right up to the moment when the manuscript
left my hands; but if, at some future time, a new version of Xcode is released along
with a new version of Swift, some of the code in this book, and even some informa‐
tion about Swift itself, might be slightly incorrect. Please make allowances, and be
prepared to compensate.
Screenshots of Xcode were taken using Xcode 10 under macOS 10.13 High Sierra. I
have not upgraded my machine to macOS 10.14 Mojave, because at the time of this
writing it was too new to be trusted with mission-critical work. If you are braver than
I am and running Mojave, your interface may naturally look slightly different from
the screenshots (especially if you’re using “dark mode”), but this difference will be
minimal and shouldn’t cause any confusion.
Acknowledgments
My thanks go first and foremost to the people at O’Reilly Media who have made writ‐
ing a book so delightfully easy: Rachel Roumeliotis, Sarah Schneider, Kristen Brown,
Dan Fauxsmith, Adam Witwer, and Sanders Kleinfeld come particularly to mind.
And let’s not forget my first and long-standing editor, Brian Jepson, whose influence
is present throughout.
As in the past, I have been greatly aided by some fantastic software, whose excellences
I have appreciated at every moment of the process of writing this book. I should like
to mention, in particular:
• git (http://git-scm.com)
• Sourcetree (http://www.sourcetreeapp.com)
• TextMate (http://macromates.com)
• AsciiDoc (http://www.methods.co.nz/asciidoc)
• Asciidoctor (http://asciidoctor.org)
• BBEdit (http://barebones.com/products/bbedit/)
• EasyFind (http://www.devontechnologies.com/products/freeware.html)
• Snapz Pro X (http://www.ambrosiasw.com)
• GraphicConverter (http://www.lemkesoft.com)
Preface | xvii
• OmniGraffle (http://www.omnigroup.com)
The book was typed and edited entirely on my faithful Unicomp Model M keyboard
(http://pckeyboard.com), without which I could never have done so much writing over
so long a period so painlessly. For more about my physical work environment, see
http://matt.neuburg.usesthis.com.
xviii | Preface
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
This book is not intended to disparage Apple’s own documentation and example
projects. They are wonderful resources and have become more wonderful as time
goes on. I have depended heavily on them in the preparation of this book. But I also
find that they don’t fulfill the same function as a reasoned, ordered presentation of
the facts. The online documentation must make assumptions as to how much you
already know; it can’t guarantee that you’ll approach it in a given order. And online
documentation is more suitable to reference than to instruction. A fully written
example, no matter how well commented, is difficult to follow; it demonstrates, but it
does not teach.
A book, on the other hand, has numbered chapters and sequential pages; I can
assume you know views before you know view controllers for the simple reason that
Part I precedes Part II. And along with facts, I also bring to the table a degree of expe‐
rience, which I try to communicate to you. Throughout this book you’ll find me
referring to “common beginner mistakes”; in most cases, these are mistakes that I
have made myself, in addition to seeing others make them. I try to tell you what the
pitfalls are because I assume that, in the course of things, you will otherwise fall into
them just as naturally as I did as I was learning. You’ll also see me construct many
examples piece by piece or extract and explain just one tiny portion of a larger app. It
is not a massive finished program that teaches programming, but an exposition of the
thought process that developed that program. It is this thought process, more than
anything else, that I hope you will gain from reading this book.
Preface | xix
This element signifies a general note.
xx | Preface
For more information, please visit http://oreilly.com/safari.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at http://bit.ly/ios12-prog-fundamentals.
To comment or ask technical questions about this book, send email to bookques‐
tions@oreilly.com.
For more information about our books, courses, conferences, and news, see our web‐
site at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Preface | xxi
PART I
Language
This part of the book teaches the Swift language, from the ground up. The descrip‐
tion is rigorous and orderly. Here you’ll become sufficiently conversant with Swift to
be comfortable with it, so that you can proceed to the practical business of actual
programming.
• Chapter 1 surveys the structure of a Swift program, both physically and concep‐
tually. You’ll learn how Swift code files are organized, and you’ll be introduced to
the most important underlying concepts of the object-oriented Swift language:
variables and functions, scopes and namespaces, object types and their instances.
• Chapter 2 explores Swift functions. We start with the basics of how functions are
declared and called; then we discuss parameters — external parameter names,
default parameters, and variadic parameters. Then we dive deep into the power
of Swift functions, with an explanation of functions inside functions, functions as
first-class values, anonymous functions, functions as closures, curried functions,
and function references and selectors.
• Chapter 3 starts with Swift variables — their scope and lifetime, and how they are
declared and initialized, along with features such as computed variables and set‐
ter observers. Then some important built-in Swift types are introduced, includ‐
ing Booleans, numbers, strings, ranges, tuples, and Optionals.
• Chapter 4 is all about Swift object types — classes, structs, and enums. It explains
how these three object types work, and how you declare, instantiate, and use
them. Then it proceeds to polymorphism and casting, protocols, generics, and
extensions. The chapter concludes with a discussion of Swift’s umbrella types,
Random documents with unrelated
content Scribd suggests to you:
came unto the sepulchre, bringing the spices which they had
prepared, and certain others with them. And they found the stone
rolled away from the sepulchre. And they entered in, and found not
the body of the Lord Jesus. And it came to pass, as they were
much perplexed thereabout, behold, two men stood by them in
shining garments. And as they were afraid, and bowed down their
faces to the earth, they said unto them, Why seek ye the living
among the dead? He is not here, but is risen: remember how he
spake unto you when he was yet in Galilee, Saying, The Son of
man must be delivered into the hands of sinful men, and be
crucified, and the third day rise again. And they remembered his
words. And returned from the sepulchre, and told all these things
unto the eleven, and to all the rest.—St. Luke xxiii: 56; xxiv: 1-9.
72
73
SOMEBODY’S MOTHER.
This lesson was taught on the shore of the Sea of Galilee. Some of
the disciples of Jesus had gone back to their old trade of fishing. On
one occasion they had been out all night, but had caught nothing.
The next morning, Jesus stood on the shore of the lake, but they did
not know him. He asked them if they had anything to eat. They said
no. He told them to cast the net on the right side of the ship, and
they would find plenty of fish. They did so, and their nets were filled
at once. Then John, the loving disciple, was the first to find out who
it was. He said to Peter, “It is the Lord.” As soon as Peter heard this,
he fastened his fishing coat about him, plunged into the sea, and
swam to the shore. The other disciples rowed to land in their boat.
As soon as they landed, they found a fire of coals, with fish and
bread all ready for eating. Jesus invited them to come and dine with
him. They did so, and when the dinner was over, he had a long
conversation with them. As they were talking together, he said to
Peter, “Simon, son of Jonas, lovest thou me?” He saith unto him,
“Yea, Lord; Thou knowest that I love thee.” He saith unto him, “Feed
my lambs.” This question was repeated by Jesus the second and the
third time. Peter gave the same answer each time. The reply of
Jesus was—“Feed my sheep.” The lambs of Christ’s flock mean the
children, or young members of his church. The sheep of Christ mean
the older members of his church. By feeding his lambs and his
sheep, Jesus meant teaching his people, both young an old, about
himself, and what he has done for their salvation. And by what Jesus
said on this occasion, he meant to teach Peter, and you and me, and
all his people, that if we really love him, the way in which he wishes
us to show that love, is by being kind to others—by teaching them
about him, and by trying to get them to love and serve him, too.
This is a good lesson for ministers to learn; for parents, for
teachers, and for all who love Jesus. When we have found what a
faithful friend, what a loving Saviour, what a kind and tender
comforter we have in him, then he wishes us to do all we can to
help others to know him, and love him, and serve him. This is what
we should all be trying to do.
Let us look at some of the ways in which we may do this.
“The Unexpected Friend.” The Rev. Mr. Moffatt,—the missionary to
Southern Africa, tells an interesting story which illustrates very
strikingly this part of our subject. “In one of my early journeys in this
land,” he says, “I came, with my companions, to a heathen village
on the banks of a river. We had travelled far, and were hungry and
thirsty and very weary. The people of the village would not let us
come near them. We asked for water and they would not give us
any. We offered to buy milk, but they refused to sell us any. We had
no prospect but that of spending the night without anything to eat,
or to drink. But at the close of the day a woman came to us from
the village. She bore on her head a bundle of wood, and had a
vessel of milk in her hand. Without saying a word, she handed us
the milk. She laid down the wood and returned to the village.
Presently she came again with a cooking vessel on her head, with a
leg of mutton in one hand and a vessel of water in the other. Silently
she kindled the fire and put on the meat. Again and again we asked
her who she was and why she was doing all this for us strangers. At
last she said that years before there had been a missionary in her
neighborhood. He had gone away a long while ago; but from him
she had learned to know the Saviour. ‘I love him,’ she said, ‘whose
servants you are, and I wish to show my love to him by doing what I
can to help you.’ ‘I asked her,’ said Mr. Moffatt, ‘how she, alone in
that dark land, without a minister, without a church, and without any
Christian friends, had kept up the light of God in her heart?’ She
drew from her bosom a soiled and worn copy of the New Testament,
which the missionary had given her. ‘This,’ she said, ‘is the fountain
from which I drink; this is the oil that makes my lamp burn.’”
“How a Boy Showed his Love for Christ.” Some time ago a dreadful
accident took place on the river Thames, in England. A steamer,
called the Princess Alice, when crowded with passengers, on an
excursion, was run into by another vessel and sunk. Fearful screams
filled the air as the great crowd of people were plunged into the
water. Among those who were drawn to the spot was a good
Christian boy, about sixteen years of age, who worked in the
neighborhood. Being a good swimmer, he at once plunged into the
water, took hold of the first struggler he met with, and bore him
away in safety to the shore. He did the same the second time, and
then the third. As he was nearing the shore the third time he saw a
small bundle floating on the water, which he thought must be a
baby. He caught it with his teeth, and thus was the means of saving
four lives on that terrible occasion. By the time he had done this, his
strength was exhausted, and he was unable to venture again among
the drowning ones. But he carried the baby home to his mother’s
humble dwelling, and placing the little orphan in her arms, he said,
“Here, mother, suppose you nurse this baby for our blessed Saviour;
and I will work for its support as long as I live.”
A noble boy that was! and a beautiful illustration he gave of the
way in which we should show our love to Jesus, by feeding and
taking care of his lambs, and doing good to his people.
I never saw the lesson now before us better expressed than in the
following simple lines:
“Are you shining for Jesus children? You have given your
hearts to Him;
But is the light strong within them, or is it but pale and dim?
Can everybody see it—that Jesus is all for you?
That your love to Him is burning with radiance warm and
true?
Is the seal upon your forehead, so that it must be known
That you are ‘all for Jesus,’ that your hearts are all His own?
“Are you shining for Jesus, children, so that the holy light
May enter the hearts of others, and make them glad and
bright?
Have you spoken a word for Jesus, and told to some around
—
Who do not care about Him,—what a Saviour you have
found?
Have you lifted the lamp for others, that has guided your
own glad feet?
Have you echoed the loving message, that seemed to you so
sweet?
“Are you shining for Jesus, children, shining just
everywhere?
Not only in easy places, not only just here or there?
Shining in happy gatherings, where all are loved and known?
Shining where all are strangers,—shining when quite alone?
Shining at home, and making true sunshine all around?
Shining at school, and faithful—perhaps among faithless—
found?
“Oh! rise, and ‘watching daily,’ ask Him your lamps to trim,
With the fresh oil which He giveth, that they may not burn
dim.
Yes, rise and shine for Jesus! Be brave, and bright and true,
To the true and loving Saviour, who gave Himself for you.
Oh! shine for Jesus, children! and henceforth be your way
Bright with the light that shineth unto the perfect day!”
THE ASCENSION
We come now to the last event in the history of our Saviour’s life
on earth. His work is done. His teachings are finished. His sufferings
are ended. Nothing remains for him to do but to return to heaven,
from whence he came, and take his seat at the right hand of God.
And this is the subject we are now to consider—The Ascension of
Christ.
And in considering it, the first thing for us to notice is—the time of
the Ascension.
And in all the New Testament there is only one place in which
anything is said about the time of the ascension. Indeed it is
surprising that so little is said about it altogether. Two of the gospels,
that of St. Matthew and St. John, have not a word to say on the
subject. And the other two do not say much. All that St. Mark says
about it is in a single verse. We read thus in Chap. xvi: 19:—“So,
then, after that the Lord had spoken unto them, he was received up
into heaven, and sat on the right hand of God.” There are two verses
in St. Luke in which the ascension is spoken of. In Ch. xxiv: 50, 51,
we find the ascension thus described: “And he led them out as far as
to Bethany; and he lifted up his hands and blessed them. And it
came to pass, while he blessed them, he was parted from them, and
carried up into heaven.”
The most particular account of the ascension that we have in the
New Testament is given in the “Acts of the Apostles.” In the first
chapter of this book we are told that the ascension took place “forty
days after the resurrection.” We are told of the “many infallible
proofs” that he had risen, and how he spoke to them “of the things
pertaining to the kingdom of heaven.” And he “commanded them
not to depart from Jerusalem,” until they should “be baptized with
the Holy Ghost,” which was to take place “not many days” after his
ascension. “Ye shall receive power,” said he, “after that the Holy
Ghost is come upon you.” Then he told them how “they should be
witnesses for him, both in Jerusalem and in all Judea, and unto the
uttermost parts of the earth.” “And when he had spoken these
things, while they beheld, he was taken up; and a cloud received
him out of their sight.”
And then we read about the two angels who appeared to the
disciples and told them that “this same Jesus, which was taken up
from you into heaven, shall so come, in like manner, as ye have seen
him go into heaven.”
In the third verse of this chapter we have the only information
which the whole Bible gives us about the time of the ascension. Here
we learn that this great event took place on the fortieth day after his
resurrection. We are not told why the ascension was delayed so long
after Jesus had risen from the dead. But, no doubt, there were good
reasons for it. And it may be that we shall know all about these
reasons hereafter, though we do not know them now.
If we begin and count the forty days from Easter Sunday, the
fortieth day will always come on the Thursday in the fifth week after
Easter. And this day is always kept in the Church of England, in the
Protestant Episcopal Church in this country, and in some other
churches in memory of the important event we are now considering.
It is called Ascension Day, or Holy Thursday; and the portions of
Scripture read on that day all have reference to the ascension of our
blessed Lord. And this is all that need be said about the time of the
ascension.
The place where the ascension occurred is the next thing to
notice.
From what we read in the gospel of St. Luke, we might suppose it
was from Bethany that Jesus made his ascension. Here it says, “He
led them out as far as to Bethany, and he lifted up his hands and
blessed them. And it came to pass, while he blessed them, he was
parted from them, and carried up into heaven.” St. Luke xxiv: 50,
51. But in the first chapter of the Acts of the Apostles, after
describing his ascension, we read that the disciples “returned unto
Jerusalem from the Mount called Olivet.” Acts i: 12. But there is no
contradiction here; for Bethany, the home of Lazarus and his sisters,
was on the Mount of Olives. It was situated just below the top of the
Mount, on the other side from Jerusalem. And so we know that it
was either from the village of Bethany, or from some spot between
that and the summit of the Mount, that Jesus made his ascension.
When he was here on earth he often went to the Mount of Olives. It
was from this mountain that “Jesus beheld the city and wept over it,”
when he used that beautiful illustration, “how often would I have
gathered thy children together, even as a hen gathereth her chickens
under her wings, and ye would not!” It was on this mountain that he
sat with his disciples when he gave that wonderful prophecy about
the destruction of Jerusalem, and his coming again into our world, of
which we read in the twenty-fourth chapter of St. Matthew.
The Mount of Olives was the last spot of this earth on which the
feet of the blessed Saviour stood before he went up to heaven. And
when he comes again into our world he will return to the place from
which he ascended. This we are told by the prophet Zechariah. For it
is when he is speaking of the return of Jesus from heaven that he
says—“And his feet shall stand at that day on the Mount of Olives.”
Ch. xiv: 4. This thought very naturally makes Olivet an interesting
place to visit.
Jesus Revealeth Himself to Mary
Magdalene
Mary stood without at the sepulchre weeping: and as she wept,
she stooped down, and looked into the sepulchre. And seeth two
angels in white sitting, ... where the body of Jesus had lain. And
they say unto her, Woman, why weepest thou? She saith unto
them, Because they have taken away my Lord, and I know not
where they have laid him. And when she had thus said she turned
herself back, and saw Jesus standing, and knew not that it was
Jesus. Jesus saith unto her, Woman, why weepest thou? whom
seekest thou? She, supposing him to be the gardener, saith unto
him, Sir, if thou have borne him hence, tell me where thou hast laid
him, and I will take him away. Jesus saith unto her, Mary. She
turned herself, and saith unto him, Rabboni; which is to say, Master.
Jesus saith unto her, Touch me not; for I am not yet ascended to
my Father: but go to my brethren, and say unto them, I ascend
unto my Father, and your Father; and to my God, and your God.
Mary Magdalene came and told the disciples that she had seen the
Lord, and that he had spoken these things unto her.—St. John xx:
11-18.
74
I remember, when at Jerusalem, a very pleasant visit we made to
this sacred Mount. It was at the close of a Sunday afternoon. The
sun went down as we stood there. And there was something very
sweet and solemn in the thought that Jesus, our glorious Lord, had
once stood on that Mount, near where we then were. It was from
there that his disciples saw him go into heaven. And when he comes
back from heaven, his feet will stand again upon the Mount of
Olives. And so, when we think of our Saviour going back to heaven,
we may always remember that some spot on or near the top of the
Mount of Olives was the place of the ascension.
The Manner of the Ascension—is the third thing of which to speak.
It was a visible ascension. There are only two other persons
spoken of in the Bible as having gone up from earth to heaven in a
bodily form. One of them was “Enoch, the seventh from Adam.” His
ascension was not visible. No one saw him go. It took place in
secret. We are told in one place that “he was not, for God took him.”
In another place it is said, “he was translated.” This is all we know
about the translation of Enoch.
The other case mentioned in the Bible is that of the prophet Elijah.
His ascension was visible, indeed, but it was only seen by one
person, and that was the prophet Elisha. But it was different with
the ascension of Christ. This did not take place in secret, but in
public. It was not only visible, but was witnessed by a crowd of
people. All the eleven disciples were there to see it. And there can
be no doubt that a great many others, besides the apostles, were
there too. And they all saw him, as he rose from the midst of them,
and went up to heaven. It was a visible ascension.
It was a calm and tranquil ascension. It was not done in a hurry.
Solomon tells us, when speaking of God’s doings, that his
“judgments are not executed speedily.” This means they are not
done in haste. God never works in a hurry. After he gave to Adam
the first promise, of a future deliverer from the effects of sin, he
waited more than four thousand years before he sent him into the
world. And, after Christ had risen from the dead, he was not in haste
to leave the world, and go back to heaven. We might have expected
that he would just have shown himself once or twice to his disciples,
so as to make them sure of his resurrection, and would have left
immediately for heaven. But it was not so. Instead of this he
remained here for forty days. He did not spend all this time in the
company of his disciples. He only showed himself to them from time
to time, and talked with them “of things pertaining to the kingdom
of God.” And when at last these days were over, and the time came
for him to go, still there was no haste about it. He did not go up with
a rush, as a rocket goes up. That would not have been like him. But,
as he stood on the Mount of Olives talking to his disciples, who were
standing round him, he began to rise slowly and silently towards
heaven. And as he began, so he went on. Slowly and silently he
continued to rise. Upwards he went, higher—and higher,—till at last
a cloud received him out of their sight. That cloud became, as it
were, the chariot in which he was carried up in triumph to heaven. It
was a calm and tranquil ascension.
It was a blessed ascension. You know how it is at the close of a
service in church. The minister lifts up his hands to bless the people,
or as we say, to pronounce the benediction. And this was what Jesus
was doing at the very moment of his ascension. He knew that the
time had come for him to go. He knew that he was about to be
separated from his disciples, and that they would see him no more
in this world. So he lifted up his hands to bless them; and, while he
was in the act of speaking those words of blessing, the ascension
took place. As his hands were stretched out to bless his disciples, he
rose calmly in the midst of them and went back to heaven, from
whence he came.
It was said of him before he came into our world, that—“men
should be blessed in him:” Ps. lxxii: 17. He was the promised seed of
Abraham, in whom it was declared that “all the nations of the earth
should be blessed.” Gen. xxii: 18. The blessing of the world was
wrapped up in Jesus. When he came into the world, he came to
bless it. And when he began his public ministry in the Sermon on the
Mount, the first thing of which he had to speak was the blessings he
came to bring. We read, “And when he was set, his disciples came to
him, and he opened his mouth and taught them, saying, Blessed are
the poor in spirit, &c. Blessed are they that mourn, &c. Blessed are
the meek, &c. Blessed are the merciful, &c. Blessed are the pure in
heart, &c.” He began his work in blessing; he continued it in
blessing; and he ended it in blessing. But the work of blessing in
which he had been engaged here did not cease when he ascended
into heaven. He has been carrying on the work of blessing men ever
since he ascended. He went to heaven to procure for his people the
best of all possible blessings. He told his disciples that it was
necessary for him to leave them and go to heaven; because if he did
not go the Holy Ghost would not come to them; but that when he
went to heaven he would send the Spirit to be their helper and
comforter. It is the Holy Spirit who helps us to understand the
Scriptures and who teaches us how to love and serve God. And
whatever helps us to do this is the best thing—the greatest blessing
for us. And when we know that Jesus went to heaven to obtain for
us the help of God’s grace and Spirit, we may well say that the
ascension of Christ was a blessed ascension.
The only other thing we have to say about the ascension of Christ
is that it was a wonderful ascension. There were two wonderful
things connected with it. It was wonderful to think where he went.
He did not go simply to join the company of the angels, who have
always lived in heaven, and of the good people who went there
when they died. No; but he went where no one else had ever gone
before, and where no one else could go. When he arrived in heaven,
he rose above all the company that was there, and took his seat at
the right hand of the throne of God. This was wonderful, indeed.
When Jesus was on earth, he was so poor that he “had not where to
lay his head.” He was despised and persecuted; “a man of sorrows
and acquainted with grief.” He was put to death as a criminal, by
being crucified between two thieves. But when he ascended into
heaven it was to take his place “far above all principalities and
powers.” And so his ascension was wonderful when we think—where
he went.
And it was wonderful, too, when we think how he went there. If
he had left his human body behind him, and had ascended to
heaven simply in his divine nature, as God, it would not have been
so wonderful. But he did not do this. He took his human body with
him. The body that was nailed to the cross and laid in the grave, he
took with him to heaven. He ascended, indeed, as the Son of God.
But that was not all. No, for he ascended as the Son of man, too. It
was Jesus Christ who ascended into heaven. But it was our human
nature, as well as the divine nature which helped to make up the
person of Jesus Christ. And so when he ascended into heaven, and
sat down at the right hand of the throne of God, he took our human
nature with him. He took a body like yours and mine, up to that high
and glorious place. And he is sitting there now, on the throne of
God, as our brother. This is the most surprising thing connected with
the great event we are now considering. This shows us how
wonderful the ascension of Christ was.
And so, in studying this subject, these are the things about it that
we have noticed, viz.: the time of the ascension—forty days after the
resurrection; the place of the ascension—the Mount of Olives; the
manner of the ascension—visible—calm and tranquil—blessed—and
wonderful.
There are several lessons that may be learned from the ascension
of our Saviour, but we can only speak of one. This is the lesson of
obedience to the command of Christ.
Just before he went up to heaven, Jesus said to his disciples—“Go
ye into all the world, and preach the gospel to every creature.” St.
Mark xvi: 15. This is the last command he gave to his people. He
kept it till now because he wished it to be especially remembered.
When we lose a father, or mother, or a friend whom we love very
much, we listen attentively to the words spoken by that friend as we
gather round the dying bed. We may forget some things spoken by
him in the days of his health and strength, but those last words that
were spoken just before he died, we always remember. They seem
very sacred to us, and we pay particular attention to them. And no
doubt this was the reason why Jesus kept this command about the
missionary work he wishes his people to engage in, till now. He had
finished his work for them. He had died for them. He had done all
that was necessary for them to be saved, and to be happy with him
forever in heaven. He knew how much they ought to love him for all
this. And he knew that those who really did love him would wish to
show their love by doing what he had told them to do. And so he
kept this—the most important of all his commands—to the last. He
wished it to be connected with the thought of his leaving the world
and going back to heaven. And as they gathered round him, to see
him and hear him for the last time on earth, he spoke these words:
—“Go, ye, into all the world, and preach the gospel to every
creature.” He did not speak them for the apostles or the early
Christians only. No; but he spoke them for you, and for me, and for
all his people, till the end of time. He intended his church to be a
missionary church. He intended that all the members of that church
should feel an interest in the missionary work, and that they should
do all they can to help it on. This command is very broad. It takes in
“all the world,” and “every creature.” We have no right to make the
field for this work narrower than Jesus made it. And, until the gospel
has been preached in “all the world,” and until “every creature” has
heard it, this command of Christ is binding upon all his loving
people. If we do not have the missionary spirit which these words
require us to have, that is, if we are not trying all we can to bring
“every creature,” in “all the world,” to be the friend and servant of
Jesus and to be saved by him, then, it is clear, either that we are not
loving Jesus as we ought to love him; or else, we are not showing
our love for him in the way in which he wishes us to show it. He
says, “If ye love me, keep my commandments.” And this is his last
and most solemn command for us to remember, and keep.
When he says—“Go ye into all the world and preach,” he does not
mean that all his people must leave their homes and go out as
ordained ministers. What he means is that whether we go out as
missionaries, or not, we must have the missionary spirit, and must
do all we can to help the missionaries in their work. We must try to
get those around us to know, and love, and serve Jesus. And the
important question for us all to consider here is—how can we do
this? In answering this question, let me point out four ways in which
this may be done.
We can be missionaries, and help to preach the gospel, by our
example.
A little boy named Ernest had begun to love the Lord Jesus Christ
and to be a Christian. One day he said to his aunt, “Now I want to
grow up a big man, and then I will be a minister and preach to lots
of people about Jesus.”
His aunt told him that he need not wait till he was a man before
he began to preach. “Try now, every day, to learn your lessons well,”
said his aunt; “to be kind, and gentle to all, and try by God’s help to
overcome your bad temper, and so, by your example you may be
preaching Jesus every day.”
“The Children’s Service.” A little girl went to a Children’s service
one Sunday afternoon. On going home she told her mother about it.
“What led you to go, Mary?” asked the mother.
“I went, mamma,” she said, “because I was invited to come by a
kind boy that I met in the street yesterday. I saw him stop and
stroke a donkey that was frightened by a cruel boy. Then he kindly
picked up my ball that was rolling away and returned it to me. As he
did this he handed me a paper about the children’s service, and
invited me to come. I knew he was a good boy because he was so
kind and had such a happy face. He said he was sure I would like
the services; and I did like them. And I mean to go every time.”
“A Young Hero.” Thirty years ago a boy had given his heart to the
Saviour. He had been confirmed and joined the church. The next day
he went to school. Some of the wild boys of the school heard that he
had joined the church, and they made up their minds to have some
fun with him about it. At recess time they formed a ring about him,
and cried out:
“Oh! here is the good boy, Charley! He is going to be a Christian!”
And what did Charley do? Did he feel ashamed and try to steal
away? Did he get angry, and hit, or kick, or speak cross to them?
No. But he calmly looked the rude boys in the face, and said, in a
manly way: “Yes, boys, I am trying to be a Christian. And isn’t that
right?”
The boys’ consciences told them he was right. They felt ashamed.
The ring was broken up at once, and Charley had no more trouble.
He was preaching by his example.
“A Beautiful Illustration.” Mary Duncan was a little girl, only four
years old, who was trying to be a Christian. This incident shows us
that even at that early age she was beginning to preach, and do
good by her example.
One day she was playing with her little brother. In a fit of anger he
struck her in the face. But instead of screaming out, or striking him
back, she quietly turned to him the other cheek; and said, “There
Corie, now strike that!” The uplifted hand was dropped. The tears
came into her brother’s eyes. He kissed her, and said—“Forgive me,
sister, and I’ll never strike you again.”
When Mary was asked what led her to do so, her reply was that
she had heard her papa read out of the Bible, at prayers that
morning, what Jesus had said about it.
And so if we try to be like Jesus, we may be helping to keep his
last command, and to preach the gospel—by our example.
Another way in which we may do this is—by our efforts. This
means by what we say and do to show that we love him, and to try
to bring others to do so too. We find many illustrations of the way in
which this may be done.
“Preaching in the Hayfield.” A good, earnest minister of the gospel
was riding one day past a hayfield. A sudden shower was coming up.
He saw a farmer, who never went to church, sending off in haste for
a horse to draw his hay in before the rain came. The minister
stopped and offered his horse to do the work. It was declined; but
the minister insisted, and pulling off his coat, unhitched his horse
and went to work helping the farmer to load the hay. They got in
several loads, and when the last load was in the barn, and the rain
came pouring down, the farmer drew out his pocket book, and said:
“How much do I owe you, sir, for your help?”
“Oh, nothing,” said he, “nothing at all.”
But the farmer insisted on paying him for his work.
“Well, my friend,” said the minister, “did you ever hear an Irishman
preach?”
“No,” said the farmer.
“Well, you come next Sunday, and hear me preach, and we’ll call it
even.”