top of page

Mastering the Swift: Data Types

Writer's picture: The Coder BuddyThe Coder Buddy

Imagine you are a multilingual person, you communicate in English to the person who speaks English, but if a person speaks a different language, you have to switch the language in that case. This is exactly how the data types are. Since variables in Swift can hold various values, we just need to label the type of this variable depending on the kind of information they store:



Integers (int):  

Represents whole numbers (integers) but no decimal values. Integers are either positive or negative.

var age: Int = 25
let age: Int = 25 

Double (double):  

Double is like a super precise measuring tool for numbers. It allows you to store and work with numbers that have decimal places, unlike an int (integer) which can only hold whole numbers. 

var price: Double = 125.23

Float (float):  

Float is similar to the double. It is used to store the decimal values. But there is a slight difference between the two! Double is perfect for very precious measurements, whereas float is less precise, it's still good for decimals, but not quite as exact.

var longitude: Float = 19.0760
var latitude: Double = 72.8777

String:  

A string data type is like a container that holds any collection of text characters. This allows you to store and manipulate words, sentences, and any kind of textual information within your code.

var name: String = "The Coder Buddy"
let name: String = "The Coder Buddy"

Boolean (Bool):  

A boolean data type, often shortened to bool, it can only hold one of two values: true or false. This variable is mostly used while making decisions.

var isLoading: Bool = true
let isLoading: Bool = false

Happy coding!!!

5 views0 comments

Recent Posts

See All

Comments


me.jpg
  • GitHub
  • Facebook
  • Instagram

Hi, I'm Sharvari

Talk can be boring. Let's dive straight into the code.

Coder Girl. Productivity. Creativity

©2024 Sharvari. All rights reserved.

bottom of page