IDKit

简单轻量级的库,提供了一组类和函数来处理标识符。

AnyID

不再需要使用 StringInt 作为你的标识符。只需使用 AnyID,忘记类型转换。

struct User: Codable {
    let id: AnyID
    let name: String
    let age: Int
}

TypedID

TypedID<...> 将标识符表示为强类型标识符。TID<...> - 为了方便使用而缩写。

IDRepresentable

可以使用该协议来定义你的自定义标识符类型。

struct User: IDRepresentable {
    // let id: AnyID  <-- property is not required
    let name: String
    let age: Int
}

// but in any place you can identify your strong type id
let identifier: TID<User> 

IDContract

它就像 TypedID/TID 和 IDRepresentable 协议的结合。它需要有一个名为 id 的属性,类型为 TypedID<Self>。该库会自动确认 Swift.Identifiable 协议,并将 TypedID<Self> 类型的 id 属性作为其标识符。

struct User: IDContract {
    let id: TypedID<Self> <-- property is required
}

已知问题:SwiftUI+Preview 崩溃

由于 SwiftUI+Preview 中的一个错误,当使用 ForEach 配合默认符合 Identifiable 协议的 IDContract 模型时,会导致崩溃。这是 SwiftUI+Preview 中的一个错误,而不是此软件包中的错误。有时它可以工作,但有时会崩溃...

用法

ForEach(.fixID($items)) { item in
    // take the properties from the item directly
    let some = item.some
    // or
    // take the original item
    let item = item.original
}

崩溃日志

== PREVIEW UPDATE ERROR:

LinkDylibError: Failed to build RoutesView.swift

Linking failed: linker command failed with exit code 1 (use -v to see invocation)

ld: warning: search path '/Applications/Xcode.app/Contents/SharedFrameworks-iphonesimulator' not found
ld: warning: Could not find or use auto-linked framework 'CoreAudioTypes': framework 'CoreAudioTypes' not found
Undefined symbols for architecture arm64:
"nominal type descriptor for IDKit.TypedID", referenced from: