推荐答案
// www.javascriptcn.com code example // Int: 用于表示整数 let intValue: Int = 42 // Float: 用于表示32位浮点数 let floatValue: Float = 3.14 // Double: 用于表示64位浮点数 let doubleValue: Double = 3.1415926535 // Bool: 用于表示布尔值,true 或 false let boolValue: Bool = true // String: 用于表示文本 let stringValue: String = "Hello, Swift!"
本题详细解读
Int
Int 是 Swift 中用于表示整数的类型。它可以是正数、负数或零。Swift 中的 Int 类型会根据平台的不同自动选择 32 位或 64 位的表示方式。
let age: Int = 25 let temperature: Int = -10
Float
Float 是 Swift 中用于表示 32 位浮点数的类型。它适用于需要较小精度的浮点数计算。
let pi: Float = 3.14 let gravity: Float = 9.81
Double
Double 是 Swift 中用于表示 64 位浮点数的类型。它比 Float 具有更高的精度,适用于需要更高精度的浮点数计算。
let precisePi: Double = 3.1415926535 let speedOfLight: Double = 299792458.0
Bool
Bool 是 Swift 中用于表示布尔值的类型。它只有两个可能的值:true 或 false。
let isSwiftAwesome: Bool = true let isRaining: Bool = false
String
String 是 Swift 中用于表示文本的类型。它可以包含任意数量的字符,并且支持 Unicode 字符。
let greeting: String = "Hello, Swift!" let name: String = "Alice"
这些基本类型在 Swift 中非常常用,几乎每个 Swift 程序都会用到它们。理解它们的用法和区别是掌握 Swift 编程的基础。