Contents
Landmarks
Yanis Zafirpulos
Swift 4 Recipes Hundreds of Useful Hand-picked Code Snippets
Yanis Zafirpulos
Granada, Spain
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/978-1-4842-4181-3 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-4181-3 e-ISBN 978-1-4842-4182-0
https://doi.org/10.1007/978-1-4842-4182-0
Library of Congress Control Number: 2018963844
Yanis Zafirpulos 2019
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
To my father
For having introduced me to the world of programming, 25 years ago; and with the hope that he will finally get himself a Mac.
Acknowledgments
I would like to thank Aaron, Jessica, Jim, Felipe (thank you so much!) and the whole Apress team for helping me make this book a reality.
And of course, all the legends: Bill, Steve, Linus, Andrew, Edsger, Richard, Niklaus, Rasmus, Chris, Matz, Guido, Dennis, Bjarne without whose inspiration, the author of this book might never have become a programmer.
Table of Contents
Part I: Beginner
Part II: Intermediate
Part III: Advanced
About the Author and About the Technical Reviewer
About the Author
Yanis Zafirpulos
is a Spain-based programmer and Mac convert, who adores any coding challenge and has been in love with the Art of Coding since he was a little kid. In his former life, he pretended to become a doctor, but for everyones sake this never happened. In this one, he pretends he is still a Computer Science undergraduate student.
In his free time, he likes writing, reading, drawing, collecting, cycling, linguistics, playing chess, creating programming languages, and pretty much anything that catches his eye.
Hes also the main developer and editor of the iSwift website, featuring an Objective-C to Swift converter with the same name as well as a vast collection of Swift-related tools and resources.
So, if youre interested, feel free to check this out too: https://iswift.org
About the Technical Reviewer
Felipe Laso
is a Senior Systems Engineer working at Lextech Global Services . Hes also an aspiring game designer/programmer. You can follow him on Twitter as @iFeliLM or on his blog .
Part I Beginner
Yanis Zafirpulos 2019
Yanis Zafirpulos Swift 4 Recipes https://doi.org/10.1007/978-1-4842-4182-0_1
1. Introduction
Now that you have gotten your hands on this little book, lets get them a bit... dirty. And whats better than delving into our first program?
In this chapter, well see how we can write our first program in Swift, compile it, run it, and even see how Swift can be used as a scripting language .
1.1 Getting Familiar with Swift
Problem
Youd like to execute and compile a simple Swift program that you just created.
Solution
Fire up your favorite editor, and lets write this very basic one line of code.
print("Hello Swift!")
How It Works
Now that we already have our code file, lets save this as hello.swift . The next rather logical question is: How do we actually run this thing?
Executing It
Well, you might be expecting something along the lines of Open Xcode, etc, etc, but - since Im rather old-school and prefer having total control over what goes on, why/how it is executed, and so on - what could be better than doing it straight from the command line?
So, just open the Terminal app and lets go to the location where your hello.swift resides. Now, all thats left provided that you already have Xcode installed (and the accompanying developer tools) is running our script:
swift hello.swift
And here is the output:
Hello Swift!
That was easy, wasnt it? Pretty much like with any scripting language (see PHP, Python, Ruby, Perl, etc.). But wait: Is Swift a scripting language? Well, not quite
Compiling It
What if we actually want to compile this code of ours and have a binary, which we can then use, without having to recompile or keep the Swift compiler around?
Lets see...
swiftc hello.swift
Super easy again, nope? Just note the use of swiftc (the Swift compiler) instead of swift .
Now, if you look again into your folder contents, youll notice a single hello binary, which you can execute, like any normal binary:
./hello
And that was it.
1.2 Scripting in Swift
Problem
Youd like to use Swift for Scripting , pretty much like any regular interpreted language, such as PHP, Python, Ruby, Perl, or... even Bash.
Well, heres the catch: given its speed, it sure can: at least for smaller scripts. Plus, since Apple has already launched it as open source software (already having been ported to Linux), this makes it a not-so Mac-only thing.
So, the answer is: Why not?
Solution
Start with the same Hello World script we played with in the previous section.
But now, lets add the magic shebang line: #!/usr/bin/env swift