gpt4 book ai didi

ios - 如何在 Swift3 iOS 中根据键值按升序获取数组

转载 作者:行者123 更新时间:2023-11-28 13:50:58 26 4
gpt4 key购买 nike

我正在 Swift3 中创建一个数据库应用程序,我必须在其中显示来自 JSONUITableView 中的数据。下面是我的 JSON :

{
"Success": 1,
"data": [{
"Session_Details": [{
"Start_Time": "08:00",
"End_Time": "10:00",
"Tag_Details": [{
"Tag_Id": 1,
"Tag_Name": "Test 1",
"Tag_Order": 4
}]
},
{
"Start_Time": "10:30",
"End_Time": "12:30",
"Tag_Details": [{
"Tag_Id": 2,
"Tag_Name": "Test 2",
"Tag_Order": 1
}]
},
{
"Start_Time": "10:30",
"End_Time": "12:30",
"Tag_Details": [{
"Tag_Id": 3,
"Tag_Name": "Test 3",
"Tag_Order": 3
}]
},
{
"Start_Time": "13:30",
"End_Time": "15:20",
"Tag_Details": [{
"Tag_Id": 1,
"Tag_Name": "Test 1",
"Tag_Order": 4
}]
}
]
}]
}

我已经解析了 JSON 并获取了所有 JSON 数据。

我的问题是我必须创建一个应该具有唯一值的“Tag_Details”数组,这意味着 Tag_Id 应该是唯一的。此外,我还必须根据 Tag_Order 键以 升序 顺序设置数组。

我正在尝试下面的代码但没有工作:

var sessions : [SessionData]! {
return AgendaDataManager.sharedInstance.sessionData
}

let sortedResults = session.tagDetails!.sortedArray(using: [NSSortDescriptor(key: "tagOrder", ascending: true)])

let sessionTag = ((session.tagDetails as AnyObject).allObjects as! [TagData])[0]

请给我建议。谢谢。

最佳答案

如果您使用 Swift 3 编写代码并且无法使用 Codable 协议(protocol)

首先,您应该构建您的 json 数据。您可以使用这个助手 quick type这将提供一个很好的起点:

struct Root {
let success: Bool
let data: [Datum]
}

struct Datum {
let sessionDetails: [SessionDetail]
}

struct SessionDetail {
let startTime: String
let endTime: String
let tagDetails: [TagDetail]
}

struct TagDetail {
let tagId: Int
let tagName: String
let tagOrder: Int
}

您需要为您的根结构创建一个自定义初始化程序,该初始化程序采用数据参数(JSON 数据):

typealias Dictionary = [String: Any]
typealias Dictionaries = [[String: Any]]

extension Root {
init?(_ data: Data) {
let dictionary = (try? JSONSerialization.jsonObject(with: data)) as? Dictionary ?? [:]
success = dictionary["Success"] as? Bool == true
guard success else {
return nil
}
self.data = (dictionary["data"] as! Dictionaries).map(Datum.init)
}
}

以及为所有结构采用字典的初始化器。

extension Datum {
init(dictionary: [String: Any]) {
sessionDetails = (dictionary["Session_Details"] as! Dictionaries)
.map(SessionDetail.init)
}
}

extension SessionDetail {
init(dictionary: [String: Any]) {
startTime = dictionary["Start_Time"] as! String
endTime = dictionary["End_Time"] as! String
tagDetails = (dictionary["Tag_Details"] as! Dictionaries).map(TagDetail.init)
}
}

extension TagDetail: CustomStringConvertible {
init(dictionary: [String: Any]) {
tagId = dictionary["Tag_Id"] as! Int
tagName = dictionary["Tag_Name"] as! String
tagOrder = dictionary["Tag_Order"] as! Int
}
var description: String {
return "TagDetail(Id: \(tagId) - Name: \(tagName) - Order: \(tagOrder))"
}
}

接下来您需要使 TagDetail 符合 Equatable 和 Comparable:

extension TagDetail: Equatable, Comparable {
static func == (lhs: TagDetail, rhs: TagDetail) -> Bool {
return lhs.tagId == rhs.tagId
}
static func < (lhs: TagDetail, rhs: TagDetail) -> Bool {
return lhs.tagOrder < rhs.tagOrder
}
}

完成所有这些步骤后,您可以轻松地过滤和排序对象:


let data = Data("""
{
"Success": 1,
"data": [{
"Session_Details": [{
"Start_Time": "08:00",
"End_Time": "10:00",
"Tag_Details": [{
"Tag_Id": 1,
"Tag_Name": "Test 1",
"Tag_Order": 4
}]
},
{
"Start_Time": "10:30",
"End_Time": "12:30",
"Tag_Details": [{
"Tag_Id": 2,
"Tag_Name": "Test 2",
"Tag_Order": 1
}]
},
{
"Start_Time": "10:30",
"End_Time": "12:30",
"Tag_Details": [{
"Tag_Id": 3,
"Tag_Name": "Test 3",
"Tag_Order": 3
}]
},
{
"Start_Time": "13:30",
"End_Time": "15:20",
"Tag_Details": [{
"Tag_Id": 1,
"Tag_Name": "Test 1",
"Tag_Order": 4
}]
}
]
}]
}
""".utf8)

if let root = Root(data), root.success,
let sessionDetails = root.data.first?.sessionDetails {
for detail in sessionDetails {
print(detail)
}
let allTagDetails = sessionDetails.flatMap{$0.tagDetails}
let tagDetailsSorted = allTagDetails.sorted()
print("\n\n\n")
var set = Set<Int>()
let tagDetailsSortedSet = tagDetailsSorted.filter({ set.insert($0.tagId).inserted })
tagDetailsSortedSet.map{print($0)}
}

这将打印

SessionDetail(startTime: "08:00", endTime: "10:00", tagDetails: [TagDetail(Id: 1 - Name: Test 1 - Order: 4)])

SessionDetail(startTime: "10:30", endTime: "12:30", tagDetails: [TagDetail(Id: 2 - Name: Test 2 - Order: 1)])

SessionDetail(startTime: "10:30", endTime: "12:30", tagDetails: [TagDetail(Id: 3 - Name: Test 3 - Order: 3)])

SessionDetail(startTime: "13:30", endTime: "15:20", tagDetails: [TagDetail(Id: 1 - Name: Test 1 - Order: 4)])

TagDetail(Id: 2 - Name: Test 2 - Order: 1)

TagDetail(Id: 3 - Name: Test 3 - Order: 3)

TagDetail(Id: 1 - Name: Test 1 - Order: 4)

关于ios - 如何在 Swift3 iOS 中根据键值按升序获取数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54610068/

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