gpt4 book ai didi

ios - Swift:将 [A, B, B, B, A, B, B, B] 数组转换为哈希数组 [ [A: [B, B, B], [A: [B, B, B] ] ]

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:59 28 4
gpt4 key购买 nike

我有两种结构,HeaderSession,它们都符合协议(protocol)TimelineItem
我有一个 ArrayTimelineItem 组成,如下所示:
[Header1, SessionA, SessionB, Header2, SessionC, SessionD]

我的需要是将 Session 分组到相关的 Header 下,如下所示:
[ [Header1: [SessionA, SessionB], [Header2: [SessionC, SessionD] ]

我尝试使用filter 方法只检索Header 结构,并使用split 方法检索Session 数组> 数组。这些工作正常,但我无法管理如何协调两者以构建我的最终 [[Header: [Session]]] 对象。

这是我的示例代码:

enum TimelineItemType: String {  
case Header = "header"
case Session = "session"
}

protocol TimelineItem {
var id: Int { get }
var type: TimelineItemType { get }
var startDate: NSDate { get }
}

Header 结构

struct Header: TimelineItem, Decodable, Hashable, Equatable {
let id: Int
let type: TimelineItemType = .Header
let startDate: NSDate
let text: String

init?(json: JSON) {
guard let id: Int = "id" <~~ json,
let type: TimelineItemType = "type" <~~ json,
let startDate: NSDate = "startDate" <~~ json,
let text: String = "text" <~~ json where type == .Header else {
return nil
}
self.id = id
self.startDate = startDate
self.text = text
}

var hashValue: Int {
// As id is unique, we can use it for hash purpose
return id
}
}

Session 结构

struct Session: TimelineItem, Decodable, Equatable {
let id: Int
let type: TimelineItemType = .Session
let startDate: NSDate
let name: String
let syllabus: String
let speaker: Speaker
let language: String
let room: String
let duration: Int

init?(json: JSON) {
guard let id: Int = "id" <~~ json,
let type: TimelineItemType = "type" <~~ json,
let startDate: NSDate = Decoder.decodeDateISO8601("startDate")(json),
let name: String = "name" <~~ json,
let speaker: Speaker = "speaker" <~~ json,
let syllabus: String = "syllabus" <~~ json,
let language: String = "language" <~~ json,
let room: String = "room" <~~ json,
let duration: Int = "duration" <~~ json where type == .Session else {
return nil
}
self.id = id
self.startDate = startDate
self.name = name
self.speaker = speaker
self.syllabus = syllabus
self.language = language
self.room = room
self.duration = duration
}
}

最后是我尝试拆分数组的代码:

func timelineFromItems(timelineItems: [TimelineItem]) -> [[Header: [Session]]]? {

let slicedSessions = timelineItems.split { $0 is Header }
let sessions = Array(slicedSessions)
let headers = timelineItems.filter { $0.type == .Header }
var timeline = [[Header: [Session]]]()
// HOW TO FILL THE TIMELINE ??
}

如何填写时间表?

最佳答案

我通过删除所有不必要的信息来减少这个例子

protocol P {}
struct A: P, Hashable {
var i:Int
var hashValue: Int { return i }
}
func ==(lhs: A, rhs: A)->Bool {
return lhs.i == rhs.i
}
struct B: P {
var i:Int
}

// your current data
let arr:[P] = [A(i: 1),B(i: 1),B(i: 2), A(i: 2), B(i: 3), B(i: 4), B(i: 5)]

将数据转换为所需格式的函数

func foo(arr: [P])->[[A:[B]]]? {

var dict:[A:[B]] = [:]
var arrb:[B] = []
let arrk:[A] = arr.filter { $0 is A }.map { $0 as! A }
guard var key = arr[0] as? A else { return nil }

arr.forEach { (p) in

if let a = p as? A {
dict[key] = arrb
arrb = []
key = a
}
if let b = p as? B {
arrb.append(b)
}
}
dict[key] = arrb
var arrr:[[A:[B]]] = []
arrk.forEach { (a) in
if let arrb = dict[a] {
arrr.append([a:arrb])
}
}
return arrr
}

现在生成的数组符合您的要求(我希望 :-))

if let result = foo(arr) {
print(result) // [[A(i: 1): [B(i: 1), B(i: 2)]], [A(i: 2): [B(i: 3), B(i: 4), B(i: 5)]]]
}

另一个测试数据

let arr:[P] = [A(i: 1),B(i: 1),B(i: 2), A(i: 2), A(i: 3), B(i: 3)]

给你

[[A(i: 1): [B(i: 1), B(i: 2)]], [A(i: 2): []], [A(i: 3): [B(i: 3)]]]

所以,即使 A 后面没有 B,它也能工作

关于ios - Swift:将 [A, B, B, B, A, B, B, B] 数组转换为哈希数组 [ [A: [B, B, B], [A: [B, B, B] ] ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474735/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com