Reader small image

You're reading from  iOS 17 Programming for Beginners - Eighth Edition

Product typeBook
Published inOct 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781837630561
Edition8th Edition
Languages
Tools
Right arrow
Author (1)
Ahmad Sahar
Ahmad Sahar
author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar

Right arrow

Simple Values and Types

Now that you have had a short tour of Xcode in the previous chapter, let’s look at the Swift programming language, which you will use to write your app.

First, you’ll explore Swift playgrounds, an interactive environment where you can type in Swift code and have the results displayed immediately. Then, you’ll study how Swift represents and stores various types of data. After that, you’ll look at some cool Swift features such as type inference and type safety, which help you to write code more concisely and avoid common errors. Finally, you’ll learn how to perform common operations on data and how to print messages to the Debug area to help you troubleshoot issues.

By the end of this chapter, you should be able to write simple programs that can store and process letters and numbers.

The following topics will be covered:

  • Introducing Swift playgrounds
  • Exploring data types
  • Exploring constants...

Technical requirements

To do the exercises in this chapter, you will need the following:

  • An Apple Mac computer running macOS 13 Ventura or macOS 14 Sonoma
  • Xcode 15 installed (refer to Chapter 1, Exploring Xcode, for instructions on how to install Xcode)

The Xcode playground for this chapter is in the Chapter02 folder of the code bundle for this book, which can be downloaded here:

https://github.com/PacktPublishing/iOS-17-Programming-for-Beginners-Eighth-Edition

Check out the following video to see the code in action:

https://youtu.be/Lr1USo4YoeE

In the next section, you’ll create a new playground, where you can type in the code presented in this chapter.

Join our book community on Discord

https://packt.link/iosdevelopment

Qr code Description automatically generated

Now that you have had a short tour of Xcode in the previous chapter, let's look at the Swift programming language, which you will use to write your app.First, you'll explore Swift playgrounds, an interactive environment where you can type in Swift code and have the results displayed immediately. Next, you'll study how Swift represents and stores various types of data. After that, you'll look at some cool Swift features such as type inference and type safety, which help you to write code more concisely and avoid common errors. Finally, you'll learn how to perform common operations on data and how to print messages to the Debug area to help you troubleshoot issues.By the end of this chapter, you should be able to write simple programs that can store and process letters and numbers.The following topics will be covered:

  • Introducing Swift playgrounds
  • Exploring data types
  • Exploring constants and...

Technical requirements

To do the exercises in this chapter, you will need the following:

  • An Apple Mac computer running macOS 13 Ventura or macOS 14 Sonoma
  • Xcode 15 installed (refer to Chapter 1, Exploring Xcode, for instructions on how to install Xcode)

The Xcode playground for this chapter is in the Chapter02 folder of the code bundle for this book, which can be downloaded here:https://github.com/PacktPublishing/iOS-17-Programming-for-Beginners-Eighth-Edition

Check out the following video to see the code in action:(to be created later)

In the next section, you'll create a new playground, where you can type in the code presented in this chapter.

Introducing Swift playgrounds

Playgrounds are interactive coding environments. You type code in the left-hand pane, and the results are displayed immediately in the right-hand pane. It's a great way to experiment with code and explore the iOS SDK.

SDK is an acronym for Software Development Kit. To learn more about the iOS SDK, visit https://developer.apple.com/ios/.

Let’s start by creating a new playground and examining its user interface. Follow these steps:

  1. To create a playground, launch Xcode and choose File | New | Playground... from the Xcode menu bar:

    Figure 2.1: Xcode menu bar with File | New | Playground... selected
  2. The Choose a template for your new playground: screen appears. iOS should already be selected. Choose Blank and click on Next:

    Figure 2.2: Choose a template for your new playground: screen
  3. Name your playground SimpleValues and save it anywhere you like. Click on Create when done:

    Figure 2.3: Save dialog box
  4. You’ll see the playground on your...

Exploring data types

All programming languages can store numbers, words, and logic states, and Swift is no different. Even if you're an experienced programmer, you may find that Swift represents these values differently from other languages that you may be familiar with.

For more information on data types, visit https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html.

Let's walk through the Swift versions of integers, floating-point numbers, strings, and Booleans in the next sections.

Representing integers

Let's say you want to store the following:

  • The number of restaurants in a city
  • Passengers in an airplane
  • Rooms in a hotel

You would use integers, which are numbers without a fractional component (including negative numbers).Integers in Swift are represented by the Int type.

Representing floating-point numbers

Let's say you want to store the following:

  • Pi (3.14159...)
  • Absolute zero (-273.15 °C)

You would use floating-point numbers, which are numbers...

Exploring constants and variables

Now that you know about the simple data types that Swift supports, let's look at how to store them so that you can perform operations on them later.You can use constants or variables to store values. Both are containers that have a name, but a constant's value can only be set once and cannot be changed after they are set, whereas a variable's value can be changed at any time.You must declare constants and variables before you use them. Constants are declared with the let keyword while variables are declared with the var keyword.Let's explore how constants and variables work by implementing them in your playground. Follow these steps:

  1. Add the following code to your playground to declare three constants:
let theAnswerToTheUltimateQuestion = 42 
let pi = 3.14159
let myName = "Ahmad Sahar"
  1. Click the Run button on the last line to run it. In each case, a container is created and named, and the assigned value is stored in...

Understanding type inference and type safety

In the previous section, you declared constants and variables and assigned values to them. Swift automatically determines the constant or variable type based on the value provided. This is called type inference. You can see the type of a constant or variable by holding down the Option key and clicking its name. To see this in action, follow these steps:

  1. Add the following code to your playground to declare a string:
let cuisine = "American"
  1. Click the Run button to run it.
  2. Hold down the Option key and click cuisine to reveal the constant type. You should see the following:
Figure 2.11: Type declaration displayed

As you can see, cuisine’s type is String.What if you want to set a specific type for a variable or constant? You'll see how to do that in the next section.

Using type annotation to specify a type

You've seen that Xcode tries to automatically determine the data type of a variable or constant based on...

Exploring operators

You can perform arithmetic, comparison, and logical operations in Swift. Arithmetic operators are for common mathematical operations. Comparison and logical operators check an expression's value and return true or false.

For more information on operators, visit https://docs.swift.org/swift-book/LanguageGuide/BasicOperators.html.

Let's look at each operator type in more detail. You'll start with arithmetic operators (addition, subtraction, multiplication, and division) in the next section.

Using arithmetic operators

You can perform mathematical operations on integer and floating-point numbers by using the standard arithmetic operators shown here:

Figure 2.13: Arithmetic operators

Let's see how these operators are used. Follow these steps:

  1. Add the following code to add arithmetic operations to your playground:
let sum = 23 + 20
let result = 32 - sum
let total = result * 5
let divide = total / 10
  1. Click the Run button to run it.

The results...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
iOS 17 Programming for Beginners - Eighth Edition
Published in: Oct 2023Publisher: PacktISBN-13: 9781837630561
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar