Variables, Constants, & Data Types
Master Swift Variables Constants and Data Types
Core Programming Concepts Covered
Variables & Constants
Learn to declare and manage mutable variables and immutable constants in Swift. Master the difference between var and let keywords.
Data Types
Understand Int, String, Float, Double, and Boolean data types. Learn when to use each type for optimal memory management.
String Operations
Master string interpolation and concatenation techniques. Learn to combine variables with text for dynamic output.
Line numbers help you stay organized and reference code easily. Access them via Xcode > Preferences > Text Editing > Show > Line numbers.
Let's clean up the output by removing the newline character. Add the bold code:
print("This song has \(numberOfVerses) verses", terminator: "")Xcode Playground automatically adds \n at the end of printed lines. Use terminator: "" parameter to suppress this behavior when needed.
Variable Mutability
Variables vs Constants
| Feature | Variables | Constants |
|---|---|---|
| Keyword | var | let |
| Mutability | Mutable | Immutable |
| Memory Usage | Higher | Lower |
| Performance | Standard | Optimized |
| Use Case | Changing data | Fixed values |
Swift Data Types Overview
String
Set of text characters used for names, messages, and text data. Enclosed in double quotes.
Int
Whole numbers without decimal points. Perfect for counters, ages, and counting operations.
Float
Decimal numbers with 6 decimal places precision. Suitable for basic currency calculations.
Double
High-precision decimal numbers with 15 decimal places. Recommended by Apple for most decimal operations.
Boolean
True or false values for yes/no questions and conditional logic in your applications.
Swift is a type-safe language that prevents mixing incompatible data types, reducing common causes of app crashes. Type inference allows Swift to automatically determine data types from initial values.
Type Annotation vs Type Inference
| Feature | Type Annotation | Type Inference |
|---|---|---|
| Syntax | var name: String = "John" | var name = "John" |
| When Required | Always explicit | Only when no initial value |
| Code Length | Longer | Shorter |
| Clarity | Very clear | Context dependent |
String Concatenation Methods
Basic Concatenation
Use the + operator to join strings: firstName + lastName creates a combined string
Adding Spaces
Insert spaces with + " " between variables to prevent words from running together
String Interpolation
Use \(variableName) syntax within strings for cleaner, more readable code integration
Compound Assignment Operators
Addition Assignment (+=)
Adds a value to the variable's current value. Commonly used for string concatenation and numeric increments.
Subtraction Assignment (-=)
Subtracts a number from the variable's current value. Useful for counters and inventory management.
Division Assignment (/=)
Divides the variable's current value by a given number. Applied in mathematical calculations and scaling operations.
Multiplication Assignment (*=)
Multiplies the variable's current value by a given number. Helpful for scaling values and compound calculations.
Variable Declaration Methods
| Feature | Traditional | Inline Multiple |
|---|---|---|
| Lines of Code | 3 separate lines | 1 single line |
| Syntax | var price = 20.00 | var price = 20.00, inventory: Int |
| Same Type Declaration | Repeat type each time | Declare type once at end |
| Readability | Very clear | Compact but dense |
Key Takeaways