在Github 的一个简单项目中,我尝试下载 JSON 对象列表:
struct TopResponse: Codable {
let data: [Top]
}
struct Top: Codable /*, Identifiable */ {
let uid: Int
let elo: Int
let given: String
let photo: String?
let motto: String?
let avg_score: Double?
let avg_time: String?
}
Run Code Online (Sandbox Code Playgroud)
这效果很好,下一步我想将它显示在 SwiftUI 列表中,从而添加Identifiable到结构中。
不幸的是,这会产生编译错误Type 'Top' does not conform to protocol 'Identifiable':
根据我的后端应用程序的设计,该字段uid是一个唯一的数字。
所以我试图通过将其类型从Intto更改来修复编译错误ObjectIdentifier,但 Swift 编译器仍然对新错误不满意Type 'Top' does not conform to protocol 'Decodable'
这里发生了什么,编译器现在可能缺少uid用于解码 JSON …
我想知道是否可以用自定义标识符覆盖标准标识符。
我有一个简单的结构:
struct MyUserData: Identifiable {
var userId: String
var userFirstName: String
var userLastName: String
}
Run Code Online (Sandbox Code Playgroud)
但是,如果var id: ObjectIdentifier结构中没有行,可识别协议将无法工作。同时,我不想使用“id”名称。userId 在我的模型中也是唯一的(它是一个 UUID)。有没有办法告诉可识别协议接受“userId”而不是“id”?
谢谢!
我需要ForEach一个结构数组,因此它们每个都必须符合Identifiable协议。但由于这些结构是从获取的 JSON 中解码的,因此它们已经有一个 id 属性——我的数据库中使用的 id。我应该给他们另一个 UUID 来满足Identifiable协议吗?如果没有,如何使用现有属性作为 id?
struct Event: Codable, Identifiable {
let eventID: String
let description: String
let date: String
let location: String
let hostID: String
let hostName: String
// need?
let id = UUID()
}
Run Code Online (Sandbox Code Playgroud) 在嵌套Codable结构中使用解码器时,有没有办法访问父结构的属性?
我能想到的唯一方法(尚未测试)是在父结构中也使用手动解码器,在userInfo字典中设置属性,然后userInfo在子结构中访问。但这会导致大量样板代码。我希望有一个更简单的解决方案。
struct Item: Decodable, Identifiable {
let id: String
let title: String
let images: Images
struct Images: Decodable {
struct Image: Decodable, Identifiable {
let id: String
let width: Int
let height: Int
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
width = try container.decode(Int.self, forKey: .width)
height = try container.decode(Int.self, forKey: .height)
// How do I get `parent.parent.id` (`Item#id`) here?
id = "\(parent.parent.id)\(width)\(height)"
}
}
let original: Image
let …Run Code Online (Sandbox Code Playgroud) 使用 Swift 5.3,如何Identifiable通过使其身份取决于两个枚举变量的组合来在结构上实现协议?
有问题的代码很简单,
struct Card: Identifiable {
let suit: Suit
let rank: Rank
enum Suit {
case spades, clubs, diamonds, hearts
}
enum Rank: Int {
case one = 1, two, three, four, five, six, seven, jack, queen, king
}
}
Run Code Online (Sandbox Code Playgroud)
上面的结构体还不符合Identifiable协议。我如何将其身份实现为它的suit和rank(仅创建一次)的唯一组合?本质上,它的身份可以是“黑桃-1”或“钻石杰克”。此外,如果可能的话,我希望将 保留rank为一种Int类型,以便稍后进行算术运算。先感谢您!
我正在使用 Swift 5.3
试图理解为什么当我声明这个结构时
final class MyActivity: Identifiable {
public let iHaveNoId: String = ""
}
Run Code Online (Sandbox Code Playgroud)
它编译时没有任何错误(即使我没有实现“id”字段),而对于 struct
struct MyActivity: Identifiable {
public let iHaveNoId: String = ""
}
Run Code Online (Sandbox Code Playgroud)
我收到错误(如预期) -类型“MyActivity”不符合协议“可识别”
此外,如果我复制可识别源代码并将其重命名为我自己的名字,例如
public protocol MyIdentifiable {
associatedtype ID : Hashable
var id: Self.ID { get }
}
Run Code Online (Sandbox Code Playgroud)
那么实现 MyIdentific 协议的结构和类都将失败,并出现正确的错误类型“MyActivity”不符合协议“MytIdentABLE”
我很困惑。
我有一个关于遵守的小Identifiable问题SwiftUI。
在某些情况下,我们需要有一个给定的类型 MyType 来符合Identifiable.
但我面临着一种情况,我需要 [MyType] (MyType 数组)才能符合Identifiable.
我的 MyType 已经符合Identifiable. 我应该怎么做才能使 [MyType] 符合Identifiable?