Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
70: DateFormatter and ISO8601DateFormatter
I've seen this exact bug happen in more production apps than I care to admit. You're building a feature that fetches data from a JSON API, the dates look perfect in the API documentation, and the code works flawlessly on your simulator. Then, a user in a different country downloads the app, and suddenly, every single date in your UI is blank or showing as "January 1, 1970."
// The "It worked on my machine" approach
let dateString = "2023-10-25T14:30:00Z"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
if let date = formatter.date(from: dateString) {
print("Success: \(date)")
} else {
print("Failed to parse date") // This happens for some users!
}
The Locale Trap
The problem here is that DateFormatter is designed to be user-facing. By default, it respects the user's current locale and calendar settings. If a user has their device set to a non-Gregorian calendar (like the Buddhist or Japanese calendar), your hardcoded dateFormat string will fail because the year "2023" doesn't exist in the same way in those systems.
When you're dealing with data coming from a server, you aren't talking to a user; you're talking to a machine. Machines should use a standard that doesn't change based on where the device is located. This is where ISO8601DateFormatter comes in.
Standardizing with ISO8601DateFormatter
If your API provides dates in the ISO 8601 format (the one with the 'T' and the 'Z'), stop using DateFormatter entirely for the parsing step. ISO8601DateFormatter is specifically built for this. It ignores the user's local settings and follows the international standard, ensuring your app parses the date the same way in New York as it does in Tokyo.
let dateString = "2023-10-25T14:30:00Z"
let isoFormatter = ISO8601DateFormatter()
if let date = isoFormatter.date(from: dateString) {
print("Parsed reliably: \(date)")
}
It's cleaner, it's faster to write, and most importantly, it's predictable. I always recommend using this for any Date object you're creating from a network response.
Converting Dates Back for the User
Now, you obviously shouldn't show "2023-10-25T14:30:00Z" to your users—that's ugly. To turn that Date object into something readable, you go back to DateFormatter. But here is the professional tip: avoid hardcoding strings like "MM/dd/yyyy" because people in Europe and the US read dates differently.
Instead, use setLocalizedDateFormatFromTemplate. This tells iOS: "I want the month, the day, and the year, but you decide the order based on the user's region."
let date = Date() // Assume this came from our ISO formatter
let displayFormatter = DateFormatter()
displayFormatter.setLocalizedDateFormatFromTemplate("yMMMd")
// On a US device: "Oct 25, 2023"
// On a UK device: "25 Oct 2023"
let dateString = displayFormatter.string(from: date)
By splitting your logic—using ISO8601DateFormatter for the API and a templated DateFormatter for the UI—you eliminate a massive category of regional bugs. I've spent way too many hours debugging "nil dates" just to realize the user was in Thailand; save yourself that headache.
📋 Practical Task
Build a Flight Arrival Timestamp Converter
You are building a travel app. The API provides flight arrival times in ISO 8601 format, but you need to display them to the user in a friendly, localized way.
Your Task:
- Create a constant string called
apiTimestampwith the value"2024-12-12T18:45:00Z". - Use
ISO8601DateFormatterto convert this string into a SwiftDateobject. - Use a
DateFormatterwithsetLocalizedDateFormatFromTemplateto convert thatDateobject into a user-facing string that includes the full month, day, and year. - Print the final localized string to the console.
There are no comments for now.