Bỏ qua để đến nội dung

Gia Tri Va Kieu Du Lieu

let age: Int = 25
let byte: Int8 = 127 // -128 to 127
let short: Int16 = 32767
let int: Int32 = 2147483647
let long: Int64 = 9223372036854775807
let ubyte: UInt8 = 255 // 0 to 255
let uint: UInt = 100 // Unsigned
let pi: Double = 3.14159 // 64-bit (mặc định)
let float: Float = 3.14 // 32-bit
let isSwiftFun: Bool = true
let isPythonBetter: Bool = false
let name: String = "Swift"
let letter: Character = "A"
// Multi-line string
let multiLine = """
Line 1
Line 2
"""
let age = 25 // Int
let height = 1.75 // Double
let name = "Alice" // String
let isValid = true // Bool

Swift là strongly typed:

let age: Int = 25
// let name: String = age // Error!
let intValue = 10
let doubleValue = Double(intValue)
let stringValue = String(intValue)
// Parse string
let str = "42"
if let number = Int(str) {
print(number) // 42
}
typealias Age = Int
typealias Name = String
let age: Age = 25
let name: Name = "Alice"
let person = ("Alice", 25)
print(person.0) // Alice
print(person.1) // 25
// Named tuples
let namedPerson = (name: "Bob", age: 30)
print(namedPerson.name) // Bob
print(namedPerson.age) // 30

  • Int, Double, Bool, String, Character
  • Type inference tự động
  • Type safety - kiểm tra chặt chẽ
  • Phải chuyển đổi kiểu tường minh
  • Tuples cho multiple values
  • Type aliases cho đặt tên tùy chỉnh