27,59 €
This book starts at the beginning by introducing programming through easy to use examples with the Swift Playgrounds app. Kids are regularly encouraged to explore and play with new concepts to support knowledge acquisition and retention – these newly learned skills can then be used to express their own unique ideas. Children will be shown how to create their first iOS application and build their very own movie night application.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 249
Veröffentlichungsjahr: 2017
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: March 2017
Production reference: 1170317
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B32PB, UK.
ISBN 978-1-78712-074-7
www.packtpub.com
Authors
Steffen D. Sommer
Jim Campagno
Copy Editor
Shaila Kusanale
Reviewer
Doug Sparling
Project Coordinator
Ritika Manoj
Commissioning Editor
Ashwin Nair
Proofreader
Safis Editing
Acquisition Editor
Shweta Pant
Indexer
Tejal Daruwale Soni
Content Development Editor
Aditi Gour
Graphics
Jason Monteiro
Technical Editor
Sushant S Nadkar
Production Coordinator
Melwyn Dsa
Since Swift was announced at WWDC, Steffen D. Sommer has had a passionate interest in the programming language. He's currently working as a lead Vapor developer at a company called Nodes in Copenhagen, where he focuses on developing backend systems using Swift.
In his spare time, he helps organize the local iOS meet up, visits iOS conferences around the world, and explores the different aspects of and use cases for Swift, such as putting Swift on the server and doing functional programming in Swift. You can also find him contributing to open source projects on GitHub or blogging on his personal website.
First, I would like to thank my girlfriend, Mia, for her never-ending feedback and support while I wrote this book. I would also like to thank my friends at IAIF for their input and support throughout the process.
Jim Campagno is an iOS developer and teacher living in New York City. He's currently working as an iOS instructor at the Flatiron School, helping beginners of Swift and iOS become iOS developers.
Jim has a deep desire and high level of creativity that he brings to teaching. He created the Swift online course offered at Flatiron School, which includes in-depth readings along with test-driven labs, challenging the student to write code in Swift. Jim also runs an active YouTube channel, putting out in-depth content and helping students understand everything in iOS and Swift—from the basics to complex topics.
Most importantly, Jim ensures that the content he creates is accessible, fun, and interactive. He enjoys putting together a story behind every topic to make it more enjoyable for the reader.
I would like to thank my roommates (Tom and Matt) for putting up with me while I wrote this book. I would also like to thank Tim Clem, who has inspired me to keep learning. Lastly, I would like to thank Joe Burgess for teaching me how to code.
Doug Sparling works as a technical architect and software developer for Andrews McMeel Universal, a publishing and syndication company in Kansas City, MO. At AMU, he uses Go for web services, Python for backend services, Ruby on Rails and WordPress for website development, and Objective-C, Swift, and Java for native iOS and Android development. AMU's sites include www.gocomics.com, www.uexpress.com, www.puzzlesociety.com, and dilbert.com.
He also was the co-author of a Perl book, Instant Perl Modules for McGraw-Hill, and a reviewer for other Packt Publishing books, including jQuery 2.0 Animation Techniques: Beginner's Guide and WordPress Web Application Development. Doug has also played various roles for Manning Publications as reviewer, technical development editor, and proofer, working on books such as Go in Action, The Well-Grounded Rubyist 2nd Edition, iOS Development with Swift, and Programming for Musicians and Digital Artists.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/Swift-Programming-Kids-Steffen-Sommer/dp/1787120740/.
If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Swift has risen quickly to be one of the preferred languages among developers and will have a long and fruitful future under the guidance of Apple. Since making the language open source over a year ago, it's become clear that Swift will be a versatile cross-platform language with a wealth of opportunities beyond Apple's ecosystem.
This book will introduce you to programming in a fun and approachable way. We will be building and interacting with fun examples to help you grasp multiple concepts. We will also be building fun iOS applications to help solidify your knowledge of Swift.
Chapter 1, What is Programming?, introduces the Swift programming language.
Chapter 2, Getting Set up, shows you how to install Xcode and introduces you to writing code in a Playground file.
Chapter 3, Say Hello, helps you create your own Playground file and write your first line of code.
Chapter 4, Favorite Things, discusses how to store values in variables and constants.
Chapter 5, Factories, explains types and introduces the String and int types.
Chapter 6, Making Pizza, outlines the problem that functions solve along with creating your own functions.
Chapter 7, Toy Bin, covers the array and dictionary types.
Chapter 8, Smarter Toy Bin, focuses on the use of loops and if-else statements.
Chapter 9, Make Some Friends, takes you through object-oriented programming and classes. You will create your first instance of a class.
Chapter 10, Pokémon Battle, helps you create your own Pokémon class along with having instances of this class interact with each other.
Chapter 11, Simon Says, introduces Interface Builder and Storyboards, and also helps you create your first application with a user interface.
Chapter 12, Starry Night, showcases the view hierarchy and auto layout. You will be creating an application that has a user interface that scales across multiple screen sizes and that will be able to change its background color at the press of a button.
Chapter 13, Space Pizza Delivery, shows you how to create an iOS application that incorporates everything we’ve learned so far. It also introduces enums, private variables, protocols, delegates, and property observers.
Chapter 14, Movie Night - iOS App, takes you through creating an iOS application that introduces UITableViews and persisting data between the launches of the application.
You will need the following things for the book:
Children who are curious about the technology we use in our daily lives and who want to know how it works can use this book to learn about programming and build their first iOS app. No prior programming experience is necessary.
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Swift-iOS-Programming-for-Kids. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/SwiftiOSProgrammingforKids_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/supportand enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at [email protected] with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.
We use programming to instruct a computer. Computers include smartphones (such as the iPhone), tablets, game consoles (such as the PlayStation), and a lot more. We say that a computer runs a program, which means that when you open your favorite game or application (also referred to as an app) on your PlayStation or on your iPhone, the device will know how to interpret and execute the program, which in this case is your game or application. A program is simply a set of instructions that your device is able to understand. We can also consider these instructions as a recipe for the device. The recipe will tell your iPhone what your favorite game is all about-everything from the colors of your avatar, to the text in the menus, to what happens when you tap on an enemy or on an animal in a game. This recipe can be written in many ways depending on your personal preference and the device that is supposed to run it. The programming language refers to the language you choose to write your program in.
This chapter will go through the following topics:
Swift is a programming language introduced by Apple, which you can use to create apps for the most common Apple devices, including the iPhone. The language and device you choose defines the possibilities for your program, for example, using Swift and targeting an iPhone as your device, you will be able to access the camera of the iPhone to take photos in your application, or the speaker of the iPhone in order to play sounds in your application. If you choose JavaScript as your programming language, HTML as your markup language and target the browser on your computer as your device, then you will be able to create interactive web pages. A programming language can differ much from language to language, and although the languages can share common concepts, it does not mean that one will master every programming language just by learning one. This leads to what programming is: it is the act of creating a program or recipe for your device to run. What is the result? The result is your program, your application, or your game.
Let's imagine this simple application for an iPhone-Movie Night, which has a list of your favorite movies. If the list is longer than the height of your device, you are able to scroll through the list by swiping up and down. You can add movies to your list by tapping a button and entering a name for the movie and you can delete them again by swiping left on the movie in the list. The idea of the app is to keep a list of your favorite movies so that you can remember them when having a movie night. In this case, the program is the application (called Movie Night), the device is the iPhone, and the instructions of the application could be the colors of the background and texts, the list in which you can scroll, and the add and delete functionalities. Programming is about creating these set of instructions so that we are able run our application on our iPhone. If you're excited about creating your own Movie Night app that you can use with your friends, then read along as this is an application we will build together in Chapter 14, Movie Night - iOS App.
There are many reasons for why programming is becoming increasingly popular. The result of doing programming allows people to connect through social media, such as Facebook and Instagram; it allows people to be entertained through immersive and rich games; and it allows people to learn and become educated in a fun and engaging manner using interactive applications. Programming has enabled us to share knowledge across geographical boundaries in a way that seemed impossible before programming existed. Programming has helped us automate tasks such as selling tickets at the local train station, paying in a faster and secure way using our smartphone, and programming has helped us measure the well-being of humans in order to give the right treatment at the right moment.
Programming has made it possible for us to instruct a computer on how to perform certain tasks in a more efficient and reliable manner than would ever be possible for a human being. There are natural limits to how much a person can do within the 24 hours of a day or within the lifetime of a person's life. Also, it is costly to have a person perform tedious tasks that seem repetitive, time-consuming, or based on deterministic behavior, such as calculations. As it is costly and it can be hard to find the right or, simply, enough people, it also means that it does not scale very well. Computers are relatively cheap and can be considered as fairly stable. If computers break, they are easy to replace, which means that performing tasks using computers scales really well. Just imagine a bank system without computers; imagine people sitting and keeping track of each person's balance and trying to keep it synchronized across large geographical distances.
Imagine the recent Pokémon smartphone game being done without computers. With no computers, there would need to be maps distributed to each player that show exactly where a Pokémon is located and people (from the game) would need to be at the actual geographical location in order to tell or validate that the Pokémon trainer had just found a Pokémon. Also, in order to keep track of Pokémon caught, and not to mention the outcome of a battle with another trainer, a lot needs to be handled on paper by the player. Without computers, one can easily think of problems with consistent play experiences, such as verifying that a player does not cheat and modifying the game rules as the game evolves, just to name a few. In general, computers have the ability to enhance our daily life, and we quickly forget what the world would look like without computers. Traditionally, programming has had a steep learning curve and, in general, been inaccessible by younger people. Today, programming is widely used and we have many different languages and tools that solve a lot of different types of problems. This means that it has been possible to lower the learning curve and involve people of different ages and with different capabilities.
This book gives you an introduction to the programming language, Swift. Swift is a fairly new programming language (version 1 came out in September 2014) and the language can be used to write programs for most of the Apple devices, including the iPhone, iPad, Apple Watch, MacBook, and Apple TV. Throughout the book, we will gradually introduce you to more and more programming concepts until we have enough knowledge to start creating our own programs. The book will let you create programs such as a small Pokémon game, where Pokémon will be able to battle each other.
We will also be creating an app to deliver pizzas in outer space and the aforementioned Movie Night app to keep a list of your favorite movies. After creating our programs, you will be able to install the applications on an iPhone and show it to your friends. After being introduced to Swift and the knowledge and tools required to create an application for the iPhone, you should be able to continue to explore this area of programming. You should not only be able to continue working on the applications we create throughout this book, but also be capable of continuing the learning journey using other books or Internet resources. By using the skills provided in this book along with some imagination, a lot of fun and useful applications can be created for iOS devices such as the iPhone.
In this chapter, we looked at what programming actually is, why it is useful, and examples of what we use programming for. We discussed what a world without programming would look like and we looked at a short introduction to the Swift programming language. Lastly, we introduced what the reader can expect to learn when reading this book and how this can be used moving further into the world of programming.
In the next chapter, we will go over the practical part of getting the user set up with the right tools in order to begin programming.
The first step in creating an application is getting yourself set up. This chapter helps you get set up by taking you through the necessary steps to install Xcode. We will also explain what Xcode and playgrounds are. These are essential tools in helping you learn how to code and create iOS applications. Think of it as the paint and the canvas an artist needs in order to paint a beautiful picture.
By the end of this chapter, you will have all the necessary tools that will allow you to begin your journey in creating an iOS application. In this chapter, we will cover:
If you were to send a text to a friend from your mobile phone, what steps would you take? What would you say? You would open up the messages application and write out a message to your friend telling them that you're learning how to code. After typing out your message on the screen, you think twice about adding a few pizza emojis to it. After sending the message, your mobile phone is able to interpret that information and send it over to your friend, all within a few seconds-how amazing!
All related source code for this chapter can be found here: https://github.com/swift-book-projects/swift-3-programming-for-kids/tree/master/Chapter-2
Powering the messages application that you're using to text your friend is code. This code is a list of instructions that is processed by a computer, written by developers like yourself. But where does a developer write code? It is written in an application that is known as an Integrated Development Environment (IDE). An IDE is a piece of software that combines the basic tools developers need to write, test, and run their applications. Xcode is a specific IDE that includes everything you need to create amazing apps for iPhone, iPad, Mac, Apple Watch, and Apple TV. It is shown in the following screenshot:
This is a screenshot of Xcode. We've begun working on an iOS app that displays a red heart on a blue background. After showing our friends how gorgeous our iOS app looks, one told us how we're going about it all wrong and that we should have a blue heart on a red background. Xcode allows us to easily make these changes:
In order for you to be able to install the latest version of Xcode (at the time of writing, it is version 8.2.1), you need to be on a Mac. You need to be running OS X 10.11.5 or later to install Xcode (at the time of writing).
The following are the instructions to download Xcode:
When in the App Store application, in the upper right-hand corner, search for Xcode, as illustrated:Download Xcode; as of version 8.1, it's 4.47 GB:
After Xcode has finished installing, open the Finder application, which you should find on your toolbar (it looks like a smiling face). Within Finder, you should see that you have access to various directories. Select the applications directory, which should then list all of the available applications on your Mac. You should find Xcode in that list of applications. Considering, you might be working with Xcode a lot, you should drag this application down to your Dock for easy access. After doing so, launch Xcode.
After launching Xcode, you will be met with the following launch screen:
We are given three options. For now, we are most interested in Get started with a playground. Its description states, Explore new ideas quickly and easily. What exactly is a playground?
A playground is a place where we can write Swift code and see the results immediately. You won't be building apps in a playground, but they are a great place to learn:
Writing Swift code in a playground is very simple. As you're writing code, you will see the results in the right-hand pane, updated in real time, as illustrated:
In subsequent chapters, you will be tasked with writing code in a playground file. This will make your learning experience more interactive. You will be able to see, in real time, whether or not your code (list of instructions) is doing what you expect.
You are now equipped with the necessary tools to begin your journey; We wish you luck!
In this chapter, we've set you up with the necessary tools to create iOS applications. In the next chapter, we will dive right in and write some Swift code, beginning with the most famous line of code ever.
print("Hello World")Also, we will help you understand the various parts that make up Xcode and how you will interact with them when creating iOS applications.
For historic reasons, the first program we usually make when learning a new programming language is a program that simply outputs the text Hello World to the console. Printing to the console is reasonably simple and is, therefore, often a good starting point to ensure that everything is set up correctly. To honor traditions, we will do the same with our very first program written in the Swift programming language:
print("Hello World")Exciting! To achieve this, the chapter will cover the following topics:
