gpt4 book ai didi

ios - Swift 中来自两个不同 JSON 的数据放入同一结构中

转载 作者:行者123 更新时间:2023-11-30 11:50:12 24 4
gpt4 key购买 nike

我有一个带有两个参数的结构:

class feedStruct: NSObject {
var type: String?
var date: String?

//Irrelevant
}

然后,我有两种不同类型的 JSON,一种从我的网页获取数据,另一种从 Twitter 获取数据。他们各自在我的 UITableViewController 中自己的自定义单元格中显示这些数据。 .

其含义是从两个JSON中都有上述参数,因此网页JSON是从网页中获取日期并放入 "web"作为 feedStruct 的类型.

Twitter JSON 执行相同的操作,但类型为 feedStruct 中的“twitter” .

来自 HomeModel.swift 的相关代码(从我的网页获取 JSON)

var jsonElement = NSDictionary()
let locations = NSMutableArray()
let feeds = NSMutableArray()

for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary

let location = LocationModel()
let feed = feedStruct()

//the following insures none of the JsonElement values are nil through optional binding
if let name = jsonElement["name"] as? String,
let title = jsonElement["title"] as? String,
let article = jsonElement["article"] as? String,
let id = jsonElement["id"] as? String,
let category = jsonElement["category"] as? String,
let unixstamp = jsonElement["unixstamp"] as? String
{
location.name = name
location.title = title
location.article = article
location.id = id
location.category = category
location.unixstamp = unixstamp

feed.date = unixstamp
feed.type = "web"
}

locations.add(location)
feeds.add(feed)
}

DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items: locations)
self.delegate.feedDatesDownloaded(items: feeds)
})

来自twitterModel.swift的相关代码(从twitter获取JSON)

var jsonElement = NSDictionary()
let feeds = NSMutableArray()

for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary

let feed = feedStruct()

//the following insures none of the JsonElement values are nil through optional binding
if let unixstamp = jsonElement["created_at"] as? String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE MMM d HH:mm:ss Z yyyy"
let date = dateFormatter.date(from: unixstamp)!
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .hour], from: date)
let finalDate = calendar.date(from:components)

let timestamp = finalDate?.timeIntervalSince1970
let finalTimestamp = String(format: "%.0f", timestamp!)

feed.date = finalTimestamp
feed.type = "twitter"
}

feeds.add(feed)
}

DispatchQueue.main.async(execute: { () -> Void in
self.delegate.twitterDownloaded(items: jsonResult)
self.delegate.feedDatesDownloaded(items: feeds)
})

feedTableViewController我有这个函数接收 feedStruct 的数据:

func feedDatesDownloaded(items: NSArray) {
feedDates = items
self.tableView.reloadData()
}

问题是,来自网络的数据首先被存储。当 Twitter 数据也被存储时,Web 数据将被删除。

有什么方法可以将它们存储在同一个 feedStruct 中,并获得 feedDates.count共 54 个(网络数据:34,推特数据:20)?

编辑将其更改为 Array 而不是 NSMutable,并将 .add 替换为 .append。现在它给了我两本词典,里面有一本词典,而且还很多!甚至无法将所有代码放在这里,所以这只是一个示例:

[[Type: Optional("twitter"), Date: Optional("1516590000")]]
[[Type: Optional("web"), Date: Optional("1515970800"), Type: Optional("web"), Date: Optional("1515193200"), Type: Optional("web"), Date: Optional("1514847600"), Type: Optional("web"), Date: Optional("1514674800"), Type: Optional("web"), Date: Optional("1514329200"), Type: Optional("web"), Date: Optional("1513983600"), Type: Optional("web"), Date: Optional("1513551600"), Type: Optional("web"), Date: Optional("1513206000"), Type: Optional("web"), Date: Optional("1512946800"), Type: Optional("web"), Date: Optional("1512601200"), Type: Optional("web"), Date: Optional("1512169200"), Type: Optional("web"), Date: Optional("1511910000"), Type: Optional("web"), Date: Optional("1511650800"), Type: Optional("web"), Date: Optional("1511305200"), Type: Optional("web"), Date: Optional("1510441200"), Type: Optional("web"), Date: Optional("1509836400"), Type: Optional("web"), Date: Optional("1509577200"), Type: Optional("web"), Date: Optional("1509228000"), Type: Optional("web"), Date: Optional("1508709600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508018400"), Type: Optional("web"), Date: Optional("1506895200"), Type: Optional("web"), Date: Optional("1506463200"), Type: Optional("web"), Date: Optional("1506204000"), Type: Optional("web"), Date: Optional("1505772000"), Type: Optional("web"), Date: Optional("1505599200"), Type: Optional("web"), Date: Optional("1505340000"), Type: Optional("web"), Date: Optional("1504994400"), Type: Optional("web"), Date: Optional("1503784800"), Type: Optional("web"), Date: Optional("1503525600"), Type: Optional("web"), Date: Optional("1503180000"), Type: Optional("web"), Date: Optional("1502834400"), Type: Optional("web"), Date: Optional("1502575200")], [Type: Optional("twitter"), Date: Optional("1516719600"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516590000"), Type: Optional("twitter"), Date: Optional("1516590000")]]
[[Type: Optional("web"), Date: Optional("1515970800"), Type: Optional("web"), Date: Optional("1515193200"), Type: Optional("web"), Date: Optional("1514847600"), Type: Optional("web"), Date: Optional("1514674800"), Type: Optional("web"), Date: Optional("1514329200"), Type: Optional("web"), Date: Optional("1513983600"), Type: Optional("web"), Date: Optional("1513551600"), Type: Optional("web"), Date: Optional("1513206000"), Type: Optional("web"), Date: Optional("1512946800"), Type: Optional("web"), Date: Optional("1512601200"), Type: Optional("web"), Date: Optional("1512169200"), Type: Optional("web"), Date: Optional("1511910000"), Type: Optional("web"), Date: Optional("1511650800"), Type: Optional("web"), Date: Optional("1511305200"), Type: Optional("web"), Date: Optional("1510441200"), Type: Optional("web"), Date: Optional("1509836400"), Type: Optional("web"), Date: Optional("1509577200"), Type: Optional("web"), Date: Optional("1509228000"), Type: Optional("web"), Date: Optional("1508709600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508018400"), Type: Optional("web"), Date: Optional("1506895200"), Type: Optional("web"), Date: Optional("1506463200"), Type: Optional("web"), Date: Optional("1506204000"), Type: Optional("web"), Date: Optional("1505772000"), Type: Optional("web"), Date: Optional("1505599200"), Type: Optional("web"), Date: Optional("1505340000"), Type: Optional("web"), Date: Optional("1504994400"), Type: Optional("web"), Date: Optional("1503784800"), Type: Optional("web"), Date: Optional("1503525600"), Type: Optional("web"), Date: Optional("1503180000"), Type: Optional("web"), Date: Optional("1502834400"), Type: Optional("web"), Date: Optional("1502575200")], [Type: Optional("twitter"), Date: Optional("1516719600"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516590000"), Type: Optional("twitter"), Date: Optional("1516590000")]]
[[Type: Optional("web"), Date: Optional("1515970800"), Type: Optional("web"), Date: Optional("1515193200"), Type: Optional("web"), Date: Optional("1514847600"), Type: Optional("web"), Date: Optional("1514674800"), Type: Optional("web"), Date: Optional("1514329200"), Type: Optional("web"), Date: Optional("1513983600"), Type: Optional("web"), Date: Optional("1513551600"), Type: Optional("web"), Date: Optional("1513206000"), Type: Optional("web"), Date: Optional("1512946800"), Type: Optional("web"), Date: Optional("1512601200"), Type: Optional("web"), Date: Optional("1512169200"), Type: Optional("web"), Date: Optional("1511910000"), Type: Optional("web"), Date: Optional("1511650800"), Type: Optional("web"), Date: Optional("1511305200"), Type: Optional("web"), Date: Optional("1510441200"), Type: Optional("web"), Date: Optional("1509836400"), Type: Optional("web"), Date: Optional("1509577200"), Type: Optional("web"), Date: Optional("1509228000"), Type: Optional("web"), Date: Optional("1508709600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508018400"), Type: Optional("web"), Date: Optional("1506895200"), Type: Optional("web"), Date: Optional("1506463200"), Type: Optional("web"), Date: Optional("1506204000"), Type: Optional("web"), Date: Optional("1505772000"), Type: Optional("web"), Date: Optional("1505599200"), Type: Optional("web"), Date: Optional("1505340000"), Type: Optional("web"), Date: Optional("1504994400"), Type: Optional("web"), Date: Optional("1503784800"), Type: Optional("web"), Date: Optional("1503525600"), Type: Optional("web"), Date: Optional("1503180000"), Type: Optional("web"), Date: Optional("1502834400"), Type: Optional("web"), Date: Optional("1502575200")], [Type: Optional("twitter"), Date: Optional("1516719600"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516590000"), Type: Optional("twitter"), Date: Optional("1516590000")]]
[[Type: Optional("web"), Date: Optional("1515970800"), Type: Optional("web"), Date: Optional("1515193200"), Type: Optional("web"), Date: Optional("1514847600"), Type: Optional("web"), Date: Optional("1514674800"), Type: Optional("web"), Date: Optional("1514329200"), Type: Optional("web"), Date: Optional("1513983600"), Type: Optional("web"), Date: Optional("1513551600"), Type: Optional("web"), Date: Optional("1513206000"), Type: Optional("web"), Date: Optional("1512946800"), Type: Optional("web"), Date: Optional("1512601200"), Type: Optional("web"), Date: Optional("1512169200"), Type: Optional("web"), Date: Optional("1511910000"), Type: Optional("web"), Date: Optional("1511650800"), Type: Optional("web"), Date: Optional("1511305200"), Type: Optional("web"), Date: Optional("1510441200"), Type: Optional("web"), Date: Optional("1509836400"), Type: Optional("web"), Date: Optional("1509577200"), Type: Optional("web"), Date: Optional("1509228000"), Type: Optional("web"), Date: Optional("1508709600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508277600"), Type: Optional("web"), Date: Optional("1508018400"), Type: Optional("web"), Date: Optional("1506895200"), Type: Optional("web"), Date: Optional("1506463200"), Type: Optional("web"), Date: Optional("1506204000"), Type: Optional("web"), Date: Optional("1505772000"), Type: Optional("web"), Date: Optional("1505599200"), Type: Optional("web"), Date: Optional("1505340000"), Type: Optional("web"), Date: Optional("1504994400"), Type: Optional("web"), Date: Optional("1503784800"), Type: Optional("web"), Date: Optional("1503525600"), Type: Optional("web"), Date: Optional("1503180000"), Type: Optional("web"), Date: Optional("1502834400"), Type: Optional("web"), Date: Optional("1502575200")], [Type: Optional("twitter"), Date: Optional("1516719600"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516716000"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516712400"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516708800"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516626000"), Type: Optional("twitter"), Date: Optional("1516590000"), Type: Optional("twitter"), Date: Optional("1516590000")]]

最佳答案

首先。不要使用 Objective-C 集合,因为您会丢弃类型信息(请参阅前面的评论),并且它还会导致一些性能问题。你的问题怎么办,我不明白。如果您想在更新 TableView 之前从 Web 服务器和 twitter 加载数据,可以使用 DispatchGroup。如果没有,请解释您到底想要修复什么

关于ios - Swift 中来自两个不同 JSON 的数据放入同一结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48405981/

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