Basic data types - Swift Tutorial

Ozan Emre Duran
AppleCode Chronicles
2 min readJun 3, 2023

Variables and Constants

In Swift, you can declare variables and constants to store and manipulate data. Variables are mutable, meaning their values can be changed, while constants are immutable and their values cannot be modified once assigned.

var name = "John Doe" // Variable
let age = 25 // Constant

Data Types

Swift has various built-in data types to represent different kinds of values. Here are some commonly used types:

String: Used for representing textual data. Strings are sequences of characters enclosed in double quotes (“ “).

let name: String = "John Doe"

Int: Used for storing whole numbers without a fractional component. Integers can be either positive or negative.

let age: Int = 25

Double: Used for storing floating-point numbers, which include numbers with a fractional component. Doubles can represent both whole and decimal numbers.

let pi: Double = 3.14159

Bool: Used for representing Boolean values, which can be either true or false.

let isActive: Bool = true

You can declare variables or constants of these types by specifying the data type explicitly, as shown in the examples above. However, Swift also supports type inference, so you can often omit the type annotations and let the compiler automatically infer the appropriate data type based on the assigned value. For example:

let name = "John Doe" // Inferred as String 
let age = 25 // Inferred as Int
let pi =3.14159 // Inferred as Double
let isActive = true // Inferred as Bool

Type Safety

Swift is a type-safe language, which means that once a variable or constant is assigned a particular type, you cannot change its type later. This helps catch errors at compile-time and enhances the reliability of your code.

Conclusion

Swift is a powerful and intuitive programming language that has gained popularity for iOS, macOS, watchOS, and tvOS app development. It offers a clean syntax, strong type safety, and a wide range of features to simplify and enhance the development process.

If you’re interested in learning more about Swift, I recommend checking out my articles on Medium. You can access them through the template I created on Notion, which provides a structured learning process and easy navigation to different topics. Happy learning, and best of luck on your Swift programming journey!

Click here for Notion Swift Tutorial Template :)

--

--

Ozan Emre Duran
AppleCode Chronicles

I'm a passionate programmer who loves exploring and using Apple products. Swift is my language of choice.