4 Ekim 2023

What is Data Types in swift?

96 1 ay önce

In Swift, data types are used to specify the type of data that a variable or constant can hold. Swift is a statically-typed language, which means that you must declare the data type of a variable when you create it. Here are some of the commonly used data types in Swift:

Int: Integers, which can be either signed (positive, negative, or zero) or unsigned (positive or zero).

let age: Int = 25
let itemCount: UInt = 10

Float and Double: Floating-point numbers for representing decimal values. Float is a 32-bit floating-point number, and Double is a 64-bit floating-point number. Examples:

let pi: Float = 3.14159
let distance: Double = 10.5

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

let isRaining: Bool = true

String: Represents a sequence of characters.

let name: String = "John"

Character: Represents a single character.

let firstLetter: Character = "A"

Array: An ordered collection of elements of the same type.

let numbers: [Int] = [1, 2, 3, 4, 5]

Dictionary: A collection of key-value pairs.

let person: [String: Any] = ["name": "Alice", "age": 30]

Set: An unordered collection of unique elements.

let uniqueNumbers: Set = [1, 2, 3, 4, 5]

Tuple: A grouping of values of different types into a single compound value.

let coordinates: (Double, Double) = (latitude: 42.3601, longitude: -71.0589)

Optional: Represents a value that may or may not exist. Optionals are used to handle situations where a value might be missing.

var optionalValue: Int? = 10

Enum: A user-defined data type that represents a group of related values.

enum Day {
case sunday, monday, tuesday, wednesday, thursday, friday, saturday
let today: Day = .thursday