By default, Xcode does not render Markdown files as formatted text. To enable Markdown rendering for your project:
-
In the project root, create a file named: .xcodesamplecode.plist
-
Add the following XML content:
| // Demonstrates how `map` can be used to unwrap an optional value and execute code if it exists. | |
| var string: String? = "maybe" | |
| // This... | |
| if let string { | |
| print(string) | |
| } | |
| // Becomes... |
| // Demonstrates that tuple type aliases with identical structures are considered equal by Swift. | |
| typealias Person = (name: String, age: Int) | |
| typealias Address = (street: String, houseNumber: Int) | |
| let abbey = Person(name: "Abbey Road", age: 29) | |
| let home = Address(street: "Abbey Road", houseNumber: 29) | |
| if abbey == home { | |
| print("They are structurally equal!") // Works! |
| // Demonstrates how to use the `#Playground` directive right inside your main project. | |
| // The code inside this block executes immediately when run in Playgrounds. | |
| import SwiftUI | |
| import Playgrounds | |
| #Playground { | |
| print("Hello") | |
| } |
| // Demonstrates how to use `#Playground` blocks to test async API calls directly in Playgrounds. | |
| import Foundation | |
| import Playgrounds | |
| #Playground("Categories") { | |
| let apiService = APIService(urlString: "https://api.escuelajs.co/api/v1/categories") | |
| Task { | |
| let categories: [Category] = try await apiService.getJSON(dateDecodingStrategy: .iso8601) |
| // Demonstrates how to use `.glassEffect()` in SwiftUI to create a translucent background effect. | |
| // Available in iOS 18+. | |
| import SwiftUI | |
| struct GlassEffectsView: View { | |
| var body: some View { | |
| NavigationStack { | |
| VStack { | |
| Text("Hello World") |
| // Creates a "punched-out" look by grouping layers with `compositingGroup()` | |
| // and cutting out content using `.blendMode(.destinationOut)`. The shadow is | |
| // applied to the merged result of the group. | |
| import SwiftUI | |
| struct CompositingGroupDemoView: View { | |
| var body: some View { | |
| ZStack { | |
| LinearGradient(colors: [.gray.opacity(0.2), .gray.opacity(0.5)], |
| // Demonstrates hierarchical color variants (primary → quinary). | |
| // Useful for subtle gradients of the same base color without defining new values. | |
| import SwiftUI | |
| struct HierarchicalStylesView: View { | |
| var body: some View { | |
| VStack(spacing: 16) { | |
| Circle().foregroundStyle(Color.red) | |
| Circle().foregroundStyle(Color.red.secondary) |
| // ✅ Tip: | |
| // You can right-click an old PreviewProvider block and choose “Convert to Preview Macro” in Xcode 16+. | |
| // This enables advanced preview configurations with less boilerplate and full live state support. | |
| // Demonstrates the modern replacement for PreviewProvider using #Preview macro. | |
| // You can configure traits, states, and multiple previews with concise syntax. | |
| import SwiftUI | |
| struct CounterView: View { |
| // ✅ Tip: | |
| // ViewThatFits automatically selects the first view that fits within the container, making it perfect for adaptive accessibility layouts. | |
| // Automatically adjusts text to fit the available horizontal space. | |
| // Ideal for dynamic content that varies in length (e.g., dates, titles). | |
| import SwiftUI | |
| struct AccessibilityThatFitsView: View { | |
| private var titles: [String] { |