我有一个带有两个参数的结构:
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中,并获得54的feedDates.count
(网络数据:34,Twitter数据: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")]]
答案 0 :(得分:0)
首先。不要使用Objective-C集合,因为您丢弃了类型信息(请参阅上一条评论),它也可能导致一些性能问题。你的问题怎么样,我不明白。如果要在更新表视图之前从Web服务器和Twitter加载数据,可以使用DispatchGroup。如果没有,请解释您想要修复的内容