This lesson is meant for someone who has some (even if minimal) knowledge of computer science. It only introduces Swift’s unique syntax by presenting different coding exercises, but it does not explain the respective coding constructs by discussing how or why they work. For a more comprehensive introduction to coding, contact firstname.lastname@example.org.
First, be sure to download the latest Xcode onto your Mac. Next, create an Xcode folder where you can save all your Xcode files to.
Click Get started with a playground.
(An Xcode playground is where you can test out the Swift language without having to go through the hassle of actually making a whole app.)
Test the Syntax
Title your playground Swift Testing. Make sure the platform is set to iOS, and click Next. Save your Playground to your Xcode folder. The quick topics we’ll go over are Variables, Control Flow, Classes, Functions, and Comments + Errors.
Variables: Type the following into your Swift playground.
** Note the difference between var and let! Swift has type inference, meaning you don’t have to declare different variables using int or string – Swift will detect what type your variable is without them. Notice how to print to the console… it’s just print(“what you want to print”).
Control Flow (also notice how to print variables: print(“text \(variable) text”):
Classes: Type the following into your Swift playground.
Comments + Errors:
When you see this type of error, Xcode already knows how to fix it! You can click on the red circle, and then click on Xcode’s suggestion, and your code will be fixed.
When you see this type of error, Xcode has no idea how to fix it. It will, however, tell you what the error is if you click on the red circle.
...And that’s it! Now that you’ve got a handle on the syntax of Swift, you’re ready to start on your very first Xcode app, which I’ll be discussing in my next blog post. See you then!