我有一个我已下载的JSON文件,我试图将.flatMap编译成我创建的结构数组,但.flatMap没有将数据初始化为结构。
下面是JSON文件的解析版本,它代表数组中的一个结构:
[["eventUri": <null>,
"wgt": 266482320,
"source": {
dataType = news;
title = WAFB;
uri = "wafb.com";
},
"uri": 885629090,
"body": A bunch of text,
"lang": eng,
"dateTime": 2018-06-12T06:52:00Z,
"date": 2018-06-12,
"sim": 0,
"isDuplicate": 1,
"title": Now a candidate, Romney appears to embrace Trump presidency,
"time": 06:52:00,
"url": http://www.wafb.com/story/38391905/now-a-candidate-romney-appears-to-embrace-trump-presidency,
"dataType": news]]
这是struct,包含结构的共享实例,以及flatMap方法。所有初始化程序下标都是根据JSON的键命名的:
struct Article {
static var sharedInstance: [Article] = [Article]()
// MARK: Properties
var dataType = ""
var date = ""
var eventUri = ""
var isDuplicate = false
var sourceName = ""
var lang = ""
var sim = 0.0
var time = ""
var articleTitle = ""
var image: UIImage? = nil
// MARK: Initializer
init?(dictionary: [String:AnyObject]) {
// GUARD: Do all dictionaries have values?
guard
let dataType = dictionary[EventRegistry.ParameterValues.dataType] as? String,
let date = dictionary[EventRegistry.ParameterValues.date] as? String,
let eventUri = dictionary[EventRegistry.ParameterValues.eventUri] as? String,
let isDuplicate = dictionary[EventRegistry.ParameterValues.isDuplicate] as? Bool,
let source = dictionary[EventRegistry.ParameterKeys.source] as? [String:Any],
let sourceName = source[EventRegistry.ParameterValues.sourceName] as? String,
let lang = dictionary[EventRegistry.ParameterValues.lang] as? String,
let sim = dictionary[EventRegistry.ParameterValues.sim] as? Double,
let time = dictionary[EventRegistry.ParameterValues.time] as? String,
let articleTitle = dictionary[EventRegistry.ParameterValues.articleTitle] as? String
// If not, return nil
else { return nil }
// Otherwise, initialize values
self.dataType = dataType
self.date = date
self.eventUri = eventUri
self.isDuplicate = isDuplicate
self.sourceName = sourceName
self.lang = lang
self.sim = sim
self.time = time
self.articleTitle = articleTitle
}
static func articlesFromResults(_ results: [[String:AnyObject]]) -> [Article] {
return results.flatMap(Article.init)
}
}
这是我调用启动flatMap方法的调用,该方法不返回任何结果:
func getEventRegistrySearchResults(completionHandlerForSearchResults: @escaping (_ articles: [Article]?, _ errorString: String?) -> Void) {
taskForGetEventRegistrySearchResults(method: "", parameters: ["" : (Any).self]) { (result, error) in
if error != nil {
print("There was an error getting articles")
}
if let result = result {
if let articles = result["articles"] as? [String:AnyObject] {
if let articleResults = articles["results"] as? [[String:AnyObject]] {
Article.sharedInstance = Article.articlesFromResults(articleResults)
print(Article.sharedInstance.count) // Returns 0
}
} else {
completionHandlerForSearchResults(nil, "There was a problem")
}
}
}
}
打印“articleResults”这里给我上面的JSON文件,但是当我尝试将.flatMap映射到我的结构数组时,它不会初始化它们中的任何一个。我错过了什么?
答案 0 :(得分:0)
如果我是你,我会在我的文章结构中实现Swift 4 Codable协议,然后我使用JSONDecoder解码数组,就像这样
let decoder = JSONDecoder()
let articles = try! decoder.decode(Array<Article.self>, for: results)
所以你的文章结构看起来像这样:
struct Article: Codable {
var dataType: String
var date: Date
var eventUri: URL
var isDuplicate: Bool
var sourceName: String
var lang: String
var sim: Double
var time: String
var articleTitle: String
var image: UIImage? = nil
static func articlesFromResults(_ results: [[String : Any]]) -> [Article] {
let decoder = JSONDecoder()
let articles = try! decoder.decode(Array<Article.self>, for: results)
}
}
现在,这会留下图像属性的问题,因为UIImage不符合Codable,但你可以在这里寻找解决方案How to conform UIImage to Codable?