Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ Swift 101: Understanding Types, Variables, and Constants


๐Ÿ“š Swift 101: Understanding Types, Variables, and Constants


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

Hola Mundo!

This is the second article in a series of Swift 101 notes ๐Ÿ“ I did while learning the language and I decided to share them because why not?.

So if you don't know what Swift is or you would like to know more about this language, I invite you to follow my series!๐Ÿ™Š

In this chapter I'll be sharing a little bit about Types, Variables and Constants and how they work and behave in Swift.

Swift is a strongly typed language, but what does that mean? It means that at the moment that the code compiles all variables and constants types are known and they cannot be changed after declaring it.

To know so, Swift will first check the variable or constant and check if we have declared the type. If we havenโ€™t Swift will take the first value assigned and set it.

If you have worked with other heavily typed languages like for example Java, C#, or C++ then you must know that Swift works similarly.

However, if you have no experience in coding, or have more experience in languages such as Javascript, โœจโ€‹that is what happened to me when I learned Swiftโœจโ€‹, this might be new for you.

Variables and constants

Variables and constants are a sort of storage location that we create and associate with a certain name so we can use it along our code. Is a container for a value๐Ÿ“ฆโ€‹.

As their name state variables are flexible in the sense that after declaring it, their value can change. However, constants canโ€™t, their value is set and it cannot be changed.

To use these containers and their value we must first declare them. To do so we must use var or let followed by a keyword that will work as a name.

var variableName = <Initial value>
let constantName = <Initial value>

So for example, let's think of a variable. We could have the userPhone that they can change later on. So we store the userPhone in a variable, and later, in a method we change it for the new one.

In the case of constants is the contrary, we have information that the user can't change. For example, delicate data like an ID number might be not editable because it's inherent to that user. So that kind of data will be stored in a constant.

Variable and constant naming

Naming a variable or constant can be tricky sometimes. The names can be composed of letters, numbers, and underscore. However, depending on the team conventions sometimes the underscore is not used and we use camelCase, which means that each time a word starts we put it in capital letters, it would look like this: thisIsTheNewVariable

Variables and constants names should also be self-explanatory, which means that is not a good practice to use namings such as var password and var password2. Because what does password2 mean, what does it do๐Ÿค”โ€‹?

Is better to set names as var currentPassword and var newPassword. This way, as soon as you see the naming you already know what is stored.

If at a certain point, you find yourself not knowing how to name something just ask yourself, what does this storage? what does it do? And go in that direction.

Basic Swift Types

As stated before when we create either a variable or constant we should set its type or Swift will set it for us ๐ŸŽฏโ€‹.

For declaring a type we will create our variable or constant followed by : and the type name. If it already has a value we will follow the type with the = and the initial value but this last part is not necessary.

var variableName: <Variable Type> = <Initial value>
var anotherVariable: <Variable Type>

There are many types in Swift, however, the most basic ones may be separated into two categories: individual data types and collection types.

Individual data types

As the name states these types tell the system that the value that we are storing is a single value. There are six main individual data types, three of them for different kinds of numbers, two of them with words, and the last one for booleans.

Let's have a look at themโ€‹ ๐Ÿ‘€

Type name Description Example
Int It reads as integers. This type is used for whole numbers var integer: Int = 2
Double Is another number-related type that represents 64-bit floating numbers (with decimals). Double range is set to 15 digits โ‰ˆ let pi: Double = 3.14159265358979323
Float Is used to represent 32-bit floating numbers. Float range is set to 7 digits โ‰ˆ let shorterPi: Float = 3.1415926
String To represent text, it may be words or sentences. The name comes from a string of characters. var greetings: String = "Welcome"
Character This is for a single character, such as a letter, digit, and even punctuation mark or symbols. var initial: Character = "W"
Bool Represents one of two possible values: true or false var isThisABool: Bool = true

As I said before, Swift is a strong typed language, so, if we donโ€™t set the type, our variable or constant will be assigned by the system.

How does Swift automatically assign types? It checks the variable value and sets it.

// Will be typed as a "String"
var variableA = "It's me a Mario"
// Will be typed as a Bool
var isYoshiAvailable = false

Collection data types

On the other hand, we have collection data types. Collections are types that contain multiple values. This means that one variable or constant can store many values in it.
There are many collection types in Swift but we are going to talk about them in another chapter of Swift 101.

So keep an eye on the next posts of this series๐Ÿค“!

Resources

If you want a Cheat Sheet with the individual data types information, feel free to save or download this image. If you share it please do not delete or hide my name ๐Ÿซถ๐Ÿปโ€‹

Cheat sheet with all the information in the table

Also if you want to keep learning more about this I highly recommend checking the official documentation and courses here

Want to keep learning about Swift?

This a full series on Swift 101, next chapter will be about Types, variables, and constants in Swift, so I hope to see you there!

If you enjoyed this, please share, like, and comment. I hope this can be useful to someone and that it will inspire more people to learn and code with Swift

...



๐Ÿ“Œ Swift 101: Understanding Types, Variables, and Constants


๐Ÿ“ˆ 80.55 Punkte

๐Ÿ“Œ Variables and Constants: Declaration and Usage


๐Ÿ“ˆ 41.24 Punkte

๐Ÿ“Œ Rust Basics Series #2: Using Variables and Constants in Rust Programs


๐Ÿ“ˆ 39.71 Punkte

๐Ÿ“Œ Variables, Shadowing, and Constants in Rust


๐Ÿ“ˆ 39.71 Punkte

๐Ÿ“Œ Static Variables, Constants, and Methods


๐Ÿ“ˆ 39.71 Punkte

๐Ÿ“Œ Mastering Go: Guide to Type Declarations, Variables, and Constants


๐Ÿ“ˆ 39.71 Punkte

๐Ÿ“Œ Day 2: Understanding Variables and Data Types in JavaScript


๐Ÿ“ˆ 35.88 Punkte

๐Ÿ“Œ Understanding Value Types and Reference Types in C#


๐Ÿ“ˆ 32.48 Punkte

๐Ÿ“Œ Regression Modeling 101: Understanding Different Types of Models and How to Choose the Right One


๐Ÿ“ˆ 32.14 Punkte

๐Ÿ“Œ Swift 101: What's Swift?


๐Ÿ“ˆ 31.17 Punkte

๐Ÿ“Œ Refactoring instance variables to local variables in Rails controllers


๐Ÿ“ˆ 28.94 Punkte

๐Ÿ“Œ Python Type Hinting: From Type Aliases To Type Variables and New Types


๐Ÿ“ˆ 27.07 Punkte

๐Ÿ“Œ Day 3-4: Variables and Data types


๐Ÿ“ˆ 27.07 Punkte

๐Ÿ“Œ Variables and Primitive Data Types in Rust


๐Ÿ“ˆ 27.07 Punkte

๐Ÿ“Œ Week's Digest : Variables and Data Types in JavaScript


๐Ÿ“ˆ 27.07 Punkte

๐Ÿ“Œ Types, Variables, and Arithmetics in C++


๐Ÿ“ˆ 27.07 Punkte

๐Ÿ“Œ Python Data Types and Variables


๐Ÿ“ˆ 27.07 Punkte

๐Ÿ“Œ " hello what are we opening with this JavaScript Fundamentals ๐Ÿ˜‰ ? ๐ŸŒŸ **Variables and Data Types in JavaScript** ๐ŸŒŸ


๐Ÿ“ˆ 27.07 Punkte

๐Ÿ“Œ PCalc for iOS and Mac enhanced with new capabilities for functions, conversions, and constants


๐Ÿ“ˆ 26.76 Punkte

๐Ÿ“Œ Part 5 - Data Types, Variables, Variable files in Terraform


๐Ÿ“ˆ 25.55 Punkte

๐Ÿ“Œ JS Versialari haqida: JS Data types: Variables (O'zgaruvchilar):


๐Ÿ“ˆ 25.55 Punkte

๐Ÿ“Œ XM^online Common Utils and Endpoints 0.2.1 Constants.java sql injection


๐Ÿ“ˆ 25.24 Punkte

๐Ÿ“Œ Humans and identity are constants in the ever-changing world of cybersecurity


๐Ÿ“ˆ 25.24 Punkte

๐Ÿ“Œ Protected API Calls and String Constants: Looting Dridexโ€™s Candy Box


๐Ÿ“ˆ 25.24 Punkte

๐Ÿ“Œ Protected API Calls and String Constants: Looting Dridexโ€™s Candy Box


๐Ÿ“ˆ 25.24 Punkte

๐Ÿ“Œ Custom Properties vs. CSS Variables: Understanding the Differences and When to Use Each


๐Ÿ“ˆ 24.8 Punkte

๐Ÿ“Œ An IDA Python script to extract information from string constants


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ ActionApps constants.php3 GLOBALS[AA_INC_PATH] privilege escalation


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ Extracting ROM constants from the 8087 math coprocessor's die


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ CVE-2023-1712 | deepset-ai haystack prior 0.1.30 hard-coded, security-relevant constants


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ Binary ninja equivalent for symbolic constants?


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ Constants are no longer constant in PHP


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ Code Smell 249 - Constants as Numbers


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ Constants in Solidity


๐Ÿ“ˆ 23.72 Punkte

๐Ÿ“Œ Constants in C++


๐Ÿ“ˆ 23.72 Punkte











matomo