Simulations in Swift 5: Design and Implement with Swift Playgrounds
()
About this ebook
Design and implement simulations using Apples Swift Playgrounds. Computer simulations are used to replicate the physical systems we see all around us. They help us solve problems by performing experiments that may otherwise be impossible in the real world. This book uses simulations to solve counter-intuitive logic puzzles, create procedural artworks, and build habitats for digital lifeforms.
To help you create your own simulations using the Swift programming language, this book is filled with projects that you can easily follow along with. All you need is a Mac with the latest version of Xcode installed. All projects are written in Xcode’s Playgrounds. This keeps the process simple. No need to setup an Apple account, or learn anything further about building apps.
Simulations are used in many areas within computer science and yet, barely anyone is teaching them. Sure, the fundamentals are in algorithm design, but simulations are a step above that. Usually when writing an algorithm you have a specific outcome in mind. However, when you create a simulation, the system you are modelling can be far more unpredictable than sorting a list, or navigating a maze. Simulations in Swift 5 will guide you through all of this.
What You'll Learn
- Simulate situations with advanced algorithms to solve advanced problems
- Incorporate real-world physics into simulated problems
- Predict biological and other systems behavior and the outcome of events based on presented factors
Who This Book Is For
Swift programmers interested in tackling fun projects in Swift involving advanced algorithms and factors
Related to Simulations in Swift 5
Related ebooks
Swift 5 for Absolute Beginners: Learn to Develop Apps for iOS Rating: 0 out of 5 stars0 ratingsCocos2D Game Development Essentials Rating: 0 out of 5 stars0 ratingsObjective-C for Absolute Beginners: iPhone, iPad and Mac Programming Made Easy Rating: 0 out of 5 stars0 ratingsInstant PhoneGap Social App Development Rating: 0 out of 5 stars0 ratingsmacOS App Development: The SwiftUI Way Rating: 0 out of 5 stars0 ratingsGetting Started with Visual Studio 2019: Learning and Implementing New Features Rating: 0 out of 5 stars0 ratingsGetting Started with Coding: Get Creative with Code! Rating: 0 out of 5 stars0 ratingsIOS Programming For Beginners: The Simple Guide to Learning IOS Programming Fast! Rating: 0 out of 5 stars0 ratingsSoftware Engineering from Scratch: A Comprehensive Introduction Using Scala Rating: 0 out of 5 stars0 ratingsBeginning Xamarin Development for the Mac: Create iOS, watchOS, and Apple tvOS apps with Xamarin.iOS and Visual Studio for Mac Rating: 0 out of 5 stars0 ratingsSwift 2 By Example Rating: 5 out of 5 stars5/5Artificial Neural Networks with Java: Tools for Building Neural Network Applications Rating: 0 out of 5 stars0 ratingsInstant Play Framework Starter Rating: 0 out of 5 stars0 ratingsLet’s Build a Multiplayer Phaser Game: With TypeScript, Socket.IO, and Phaser Rating: 0 out of 5 stars0 ratingsBeginning Android Games Development: From Beginner to Pro Rating: 0 out of 5 stars0 ratingsBeginning iOS AR Game Development: Developing Augmented Reality Apps with Unity and C# Rating: 0 out of 5 stars0 ratingsSproutCore Web Application Development Rating: 0 out of 5 stars0 ratingsTest-Driven iOS Development with Swift Rating: 5 out of 5 stars5/5Beginning Bazel: Building and Testing for Java, Go, and More Rating: 0 out of 5 stars0 ratingsSwift 2 Blueprints Rating: 0 out of 5 stars0 ratingsVisual Studio Code Distilled: Evolved Code Editing for Windows, macOS, and Linux Rating: 3 out of 5 stars3/5Developing Web Components with TypeScript: Native Web Development Using Thin Libraries Rating: 0 out of 5 stars0 ratingsVisual Studio Condensed: For Visual Studio 2013 Express, Professional, Premium and Ultimate Editions Rating: 0 out of 5 stars0 ratingsProgramming the BeagleBone Black: Getting Started with JavaScript and BoneScript Rating: 0 out of 5 stars0 ratingsPractical Rust Projects: Building Game, Physical Computing, and Machine Learning Applications Rating: 3 out of 5 stars3/5Mastering The Spritekit Framework: Develop Professional Games With This New Ios 7 Framework Rating: 0 out of 5 stars0 ratingsGetting to Know Vue.js: Learn to Build Single Page Applications in Vue from Scratch Rating: 0 out of 5 stars0 ratingsBeginning Game Development with Amazon Lumberyard: Create 3D Games Using Amazon Lumberyard and Lua Rating: 0 out of 5 stars0 ratingsiOS 9 Game Development Essentials Rating: 0 out of 5 stars0 ratingsPractical React Native: Build Two Full Projects and One Full Game using React Native Rating: 0 out of 5 stars0 ratings
Programming For You
Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsWeb Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratings
Reviews for Simulations in Swift 5
0 ratings0 reviews
Book preview
Simulations in Swift 5 - Beau Nouvelle
© Beau Nouvelle 2019
B. NouvelleSimulations in Swift 5https://doi.org/10.1007/978-1-4842-5337-3_1
1. Swift Playgrounds
Beau Nouvelle¹
(1)
Victoria, Australia
We’re going to be creating most of our projects with Xcode’s Swift Playgrounds. They’re the easiest and fastest way to start writing Swift. I use them all the time for prototyping, or working through math problems and refining algorithms. Once I’m happy with the code, I will usually move it into a more significant project.
Playgrounds will run automatically. You can type a line of code, and the results will appear immediately. It makes for a rapid feedback loop, where you can iterate quickly on your project, find mistakes sooner, and stay productive. Larger Xcode projects, like the ones used for full applications, can take minutes to compile and launch in a simulator. Playgrounds help to reduce the wait times between writing code and testing.
If you’re already familiar with playgrounds and already have Xcode installed, you can skip this chapter.
System Requirements
At the time of writing, the latest release version of Xcode is 11 and Swift 5.1. There has been some major progress with Swift since version 4.0, which is what the first edition of this book was based on. For one thing, we now have something called ABI (Application Binary Interface) stability. This feature provides source compatibility between different versions of Swift moving forward. So if you bought this book and the latest version of Swift is 6.0, don’t worry as all exercises should still work.
Up until ABI stability, there could be significant breaking changes between different versions of Swift. This would require developers to rebuild their dependencies and refactor any code that would no longer compile.
Another added benefit is that developers will no longer need to embed a version of the Swift standard library within their apps. This means that shipped Swift apps will take up less space on a user’s device. However, since this book’s exercises are all within Xcode’s Swift Playgrounds, shipping code isn’t something we’ll need to worry about.
You’ll need
1.
macOS 10.15 or later
2.
At least 15GB of free space
Playgrounds run well on most hardware; however, if you plan on developing larger projects, I recommend that you have a Mac built within the last 4 years with a built-in SSD. Xcode’s Interface Builder tends to get overwhelmed with just a few view controllers on the best of hardware, and compile times on 5200rpm drives can be frustratingly slow.
Installation
The installation will take some time on slower connections. The download size is about 6GB.
You can also install Swift on Linux machines, but that process is out of scope for this book.
1.
Open the App Store on your Mac.
2.
Search for Xcode
.
3.
Click Xcode within the search results to navigate to its store page (see Figure 1-1).
4.
Install. It’s free!
../images/485779_1_En_1_Chapter/485779_1_En_1_Fig1_HTML.jpgFigure 1-1
Xcode’s store page in the App Store app
If you don’t own a Mac or have an Internet connection that allows you to download 6GB of data, there are some web sites out there that allow you to write and compile Swift directly in the browser. The IBM Swift Sandbox is one such web site. This will do fine for a few early projects in the book, but later on we’ll be using the live views feature of Xcode’s playgrounds to render some artwork and watch our simulations come to life.
Your First Project
After installation, you can find Xcode in the applications folder on your Mac. When first opening Xcode, it will ask you for permission to install some additional components. Click Install, and you may also need to enter your Mac’s login password to continue. This will take a few minutes to complete. On the start-up screen (Figure 1-2), there are three options on the left.
../images/485779_1_En_1_Chapter/485779_1_En_1_Fig2_HTML.jpgFigure 1-2
Xcode’s start-up screen
Get started with a playground, Create a new Xcode project, and Clone an existing project. We’re going to click the top one; Get started with a playground.
On the following screen (Figure 1-3), ensure that iOS is highlighted in the tab bar at the top, select the blank project template, and click Next.
../images/485779_1_En_1_Chapter/485779_1_En_1_Fig3_HTML.jpgFigure 1-3
Template selection screen
Now you’ll be asked to make a decision on where you would like to save this playground. I have a dedicated development folder within my documents for projects like these. This is a great place to put your projects if you want to ensure that you have a backup and access to them on all of your Apple devices. This is all handled for you; anything you put into the documents folder will be sent to iCloud storage.
Once you’ve done that, click Create!
The Code Editor
That large area with the text Hello, playground
is where you’ll write your code, but before we do that, let’s have a little look around first.
The bar at the top of the window displays status information for your project (Figure 1-4). This is usually for things like the current build status, or if Xcode has detected any errors.
../images/485779_1_En_1_Chapter/485779_1_En_1_Fig4_HTML.jpgFigure 1-4
New playground project window
../images/485779_1_En_1_Chapter/485779_1_En_1_Figa_HTML.jpgTo the right of the status bar is a button with a plus symbol; clicking this will open up a snippets window. Snippets are usually small blocks of reusable code, but they can be any size really. This is a great time–saving feature, especially if you find yourself writing the same code often. Clicking and holding on this button will open up a mini menu which allows you to also choose to open the media library. The media library will be empty. We won’t be using either of these features in this book.
../images/485779_1_En_1_Chapter/485779_1_En_1_Figb_HTML.jpgThe last three tab buttons relate to some extra panels you can show and hide on the left, bottom, and right side of the edit window. The first button opens up the navigator, and in the navigator you can switch between the files that make up your project. The middle button shows the debug area—which we’ll look at in depth in a moment—and the right one will toggle the inspector panel. We won’t be using the inspector panel, but we will be making use of the other two.
At the bottom left of the playground window, there should be a play button. Click and hold until a little dropdown window appears and ensure that Automatically Run is selected.
Go ahead and delete Hello, playground
. The playground should auto-run, and after a second or two you’ll get an error, the debug area will open, and the line of code in the editor causing the issue will be highlighted in red (Figure 1-5).
Figure 1-5
Playground window showing debug area with an error
If the debug area does not open automatically for you, double-check that you have auto-run turned on.
The Debug Area
Since the error is indicating that our variable str is missing an initial value, let’s write some code to fix this by typing Hello,
followed by your name.
var str = Hello, Name
After you’re done, the playground will auto-run again, and the error printout should disappear from the debug area, and you will see a result in the side bar.
Below this line add a print function:
print(str)
The print() function will print out anything you pass in as a parameter, to the debug area. After the playground runs again, you will now see the text Hello,
printed out where the error was previously.
We’ll be using print() all throughout this book to give us feedback on the status of our running simulations.
The Side Bar
The side bar is the shaded/gray section to the right of the code editor. It displays contextual results from the code written on the same line on the left. This content can be a number of things. Strings, print statements, results of math equations, or even a loop count.
Clicking on the little square to the right of the text will show the result directly inside the code editor. This feature isn’t all that useful if your result is only plain text; however,