Skip to Content
Swift📘 Ngôn ngữ SwiftString Interpolation

String Interpolation trong Swift

1. Basic Interpolation

let name = "Alice" let age = 25 print("Hello, \(name)!") print("I am \(age) years old")

2. Expressions

let a = 10 let b = 20 print("Sum: \(a + b)") print("Max: \(max(a, b))")

3. Properties và Methods

let numbers = [1, 2, 3, 4, 5] print("Count: \(numbers.count)") print("Sum: \(numbers.reduce(0, +))")

4. Multi-line với Interpolation

let name = "Bob" let items = ["Apple", "Banana"] let message = """ Hello, \(name)! You have \(items.count) items: \(items.joined(separator: ", ")) """

📝 Tóm tắt

  • \(variable) - Variable interpolation
  • \(expression) - Expression interpolation
  • Hoạt động trong regular và multi-line strings
Last updated on