Swift Int Double 进阶用法

1,869 阅读2分钟

Int动态宽度

Int 有符号整数类型, 在32位机器上和 Int32 等位宽, 在64位机器上和 Int64 等位宽

反射

// Int 实现了 CustomReflectable 协议, 支持通过反射获取类型的信息
struct MirrorObject {
   let intValue = 256
   let uint8Value: UInt8 = 15
   let boolValue = false
   let stringValue = "test"
}
Mirror(reflecting: MirrorObject()).children.forEach { (child) in
   print(child.label!, child.value)
}

Codeable

// Decodable & Encodable: 通过 Encoder 和 Decoder 进行 json 或者 xml 编码和解码
struct Person: Decodable, Encodable {
       let age: Int
       let sex: Int
       let name: String
}




let person = Person(age: 18, sex: 1, name: "小明")

let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted

let data = try encoder.encode(person)
String(data: data, encoding: .utf8)!
try! JSONDecoder().decode(Person.self, from: data)

FixedWidthInteger

/**
 FixedWidthInteger
    Int Int16 Int32 Int64 Int8 UInt UInt16 UInt32 UInt64 UInt8
    max min bigEndian littleEndian bitWidth
 */
print("最大最小值", Int.max, Int.min, Int64.max, Int64.min, Int8.max, Int8.min)
print("大小端", 5.bigEndian, 5.littleEndian)
print("位宽", 5.bitWidth, Int64.bitWidth, Int8.bitWidth)

Hashable

// Hashable: 生成 hash 值来来唯一表示对象

struct Point: Hashable { // Swift 的结构体枚举,只要它的所有属性都是 hashable, 编译器会自动实现 Hashable
    var x: Int
    var y: Int
//    func hash(into hasher: inout Hasher) {
//        hasher.combine(x) // 只要是相同的二进制流, hash 的结果就是相同的
//    }
}



var dic = [Point(x: 5, y: 5): "test"]
dic[Point(x: 5, y: 5)]!
Point(x: 5, y: 5).hashValue
Point(x: 5, y: 5).hashValue
Point(x: 5, y: 3).hashValue // 每次运行都会加不同的盐, 结构都不同, 所以不能持久化, 只能在内存中使用

初始化

let intValue = Int(Int8.max) + 1
//Int8(intValue) // 超出范围会崩溃

Int8(exactly: intValue) // 超过范围会返回 nil
Int8(exactly: 21.5) // 浮点型没法精确转换成整形返回 Nil

let p: Int16 = -500 // 11111110_00001100
let q = Int8(truncatingIfNeeded: p) // 00001100
let u: Int8 = 21 // 00010101
let v = Int16(truncatingIfNeeded: u) // 00000000_00010101
let w: Int8 = -21 // 11101011
let x = Int16(truncatingIfNeeded: w) // 11111111_11101011
let y = UInt16(truncatingIfNeeded: w) // 11111111_11101011

Int("10110110", radix: 2)
Int("54321", radix: 10)
Int("fa23b", radix: 16)

Strideable

var sum = 0
for x in 1...100 { // 整数步长的可以使用 for each 去遍历
    sum += x
}
for radians in stride(from: 0.0, to: .pi * 2, by: .pi / 2) { // 浮点型, 必须明确步长才能遍历 through
    let degrees = Int(radians * 180 / .pi)
    print("Degrees: \(degrees), radians: \(radians)")
}

Double

 ExpressibleByIntegerLiteral ExpressibleByFloatLiteral

let valueByInteger: Double = 5
let valueByDouble: Double = 5.0

四舍五入 rounded round

// Equal to the C 'round' function: 四舍五入
var round1 = 6.5
round1.round(.toNearestOrAwayFromZero)

// Equal to the C 'trunc' function:
var round2 = 6.5
round2.round(.towardZero)
// x == 6.0

// Equal to the C 'ceil' function:
var round3 = 6.5
round3.round(.up)
// y == 7.0

// Equal to the C 'floor' function:
var round4 = 6.5
round4.round(.down)

round4.round() // 相当于 toNearestOrAwayFromZero 四舍五入

进制

Double.radix // 进制
let maxValue = Double.greatestFiniteMagnitude
maxValue.exponent // 指数
maxValue.significand // 有效数
// significand * (radix ** exponent)
Double.infinity // 无穷大
Double.nan // not a number
Double.pi 
maxValue.isNormal // 非 infinity 和 nan 的数字