【发布时间】:2020-08-31 01:42:11
【问题描述】:
所以我想知道如何在将返回数据发送到以下命令之前检查返回数据是否包含标题。
guard let urls = URL(string: "https://api.drn1.com.au:9000/nowplaying/\(stationurl)?uuid=\(MusicPlayer.uuid!)") else { return }
URLSession.shared.dataTask(with: urls) { ( data, _, _) in
// Based on the updated structs, you would no
// longer be decoding an array
if(data != nil){
let podcast = try! JSONDecoder().decode(Nowplayng.self, from: data!)
DispatchQueue.main.async{
// The array is stored under programs now
completion(podcast.data.first!.track)
}
}
}
.resume()
}
因为根据 Crashlytics 以下内容
let podcast = try! JSONDecoder().decode(Nowplayng.self, from: data!)
如果应用没有标题,则会导致应用崩溃
podcast.data.first!.track.title
我从 Crashlytics 得到的错误。
Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "track", intValue: nil), CodingKeys(stringValue: "title", intValue: nil)], debugDescription: "Expected String value but found null instead.", underlyingError: nil)): file /Users/russellharrower/Apps/vscroll/vscroll/Core/NowPlaying.swift, line 57
经过进一步检查,我发现上面写着stringValue: "data", intValue: nil
太好了,因为我需要做的就是在将它发送到 try 脚本之前检查一下?
【问题讨论】:
-
错误告诉你路径
data/array[0]/track/title为nil。如果您的操作可能失败,则不应使用try!。在这种特定情况下,您要么需要修复模型以接受选项,要么修复 Web 服务以确保它永远不会为该特定路径发送 nil 数据。
标签: ios swift codable decodable