Chapter 3: Swift Syntax

Swift Syntax — written as if I’m sitting next to you, explaining slowly, with many small examples, comparisons to other languages you might know (C/C++/Python), common traps, and why Swift does things a certain way.

We will cover the most important syntax patterns you will see in almost every Swift file.

1. Variables & Constants – The foundation

Swift

Important style rule in modern Swift (2024–2026):

  • Default = let
  • Use varonly when you are sure the value will actually change

Very common beginner mistake:

Swift

Correct:

Swift

2. Type annotation vs type inference

Swift

When to write types explicitly:

  • When the inferred type is surprising
  • Working with very large / small numbers (Int64, UInt32, etc.)
  • Interacting with C / Objective-C APIs
  • Making function signatures clearer

3. String syntax – very flexible

Swift

Common operations:

Swift

4. Numbers – literals & separators

Swift

5. Tuples – lightweight mini-structures

Swift

6. Optionals – the syntax that defines Swift

Swift

Never do this (very dangerous):

Swift

7. Arrays – syntax variations

Swift

8. Dictionaries – key-value syntax

Swift

9. Control flow – if, guard, switch

Swift

10. Functions – basic syntax patterns

Swift

Quick Summary Table – Most Important Syntax Patterns

What Syntax Example Most common style in 2025–2026
Constant let count = 10 Always prefer let
Optional var email: String? if let, guard let, ??
String interpolation “Hello \(name)” Almost always used
Array var items: [String] = [] append, for-in
Dictionary [String: Int] subscript + ??
Switch with ranges case 200..<300: Very common for status codes
Guard guard let x else { return } Preferred for early exits
Function labels greet(person name: String) Clear external names

Would you like to go deeper into any of these areas next?

  • Enums (very powerful in Swift)
  • Struct vs Class (when to use which)
  • Closures (very important syntax)
  • Async/await syntax patterns
  • Property wrappers (@State, @Published, @ObservedObject…)
  • Or start building a small complete example program

Tell me what feels most useful or interesting to you right now 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *