I'm facing a strange issue i'm not able to track down.
I have a DataManager that is responsable to return data to my TableViewController.
This is the code
typealias NewsListCompletionBlock = (Result<NewsList>) -> Void
class DataManager {
var subManager: DataManagerProtocol
init() {
if (Config.isUITests()) {
subManager = LocalDataManager()
} else {
subManager = NetworkDataManager()
}
}
}
// MARK: News data manager
extension DataManager {
func getNewsList(completion:@escaping NewsListCompletionBlock) {
subManager.performRequest(route: nil, decoder: JSONDecoder(),
completion: completion)
}
}
This initializes a subManager based on if we are running UITests or we are running the real application.
Real application works as expected, but the LocalDataManager returns an error i can't track down.
class LocalDataManager: DataManagerProtocol {
@discardableResult
func performRequest<T>(route: APIRouter?, decoder: JSONDecoder = JSONDecoder(), completion: @escaping (Result<T>) -> Void) -> DataRequest where T : Decodable {
let newsListString = """
{
"news" : [
{
"id": 262,
"category": "e",
"title": "Scott Johnson & Paul Fowler Workshop",
"subtitle": "Wedding Photography Workshop | 20th-24th May 2019, Italy",
"link": "",
"date": "2018-08-31"
}
]
}
"""
if let jsonData = newsListString.data(using: .utf8)
{
let result :Result<NewsList>
do {
let newsListObject = try JSONDecoder().decode(NewsList.self, from: jsonData)
result = Result.success(newsListObject)
} catch {
result = Result.failure(NSError(domain: "", code: 123, userInfo: nil))
}
completion(result)
}
}
}
This is the code of the LocalDataManager. As you can see i simulate the call of the completion block providing dummy data, but when i compile i get
DataManager/LocalDataManager.swift:38:24: Cannot convert value of type 'Result<NewsList>' to expected argument type 'Result<_>'
This is because you are using the NewsList
explicitly in the place of generic parameter.
Changing the code to follows will work.
if let jsonData = newsListString.data(using: .utf8)
{
let result :Result<T>
do {
let newsListObject = try JSONDecoder().decode(T.self, from: jsonData)
result = Result.success(newsListObject)
} catch {
result = Result.failure(NSError(domain: "", code: 123, userInfo: nil))
}
completion(result)
}