Answer the question
In order to leave comments, you need to log in
How to correctly put parameters in completion escaping without destroying the filter?
i have structure:
struct APIResponce: Codable {
let result: IsResult
}
struct IsResult: Codable {
let items: [ResultItem]
let lastID: Int
let lastSortingValue: Double
enum CodingKeys: String, CodingKey {
case items
case lastID = "lastId"
case lastSortingValue
}
}
public func getTopStories(lastID: Int = 0, lastSortingValue: Double = 0, completion: @escaping (Result<[ResultItem], Error>)
do {
let json = try JSONDecoder().decode(APIResponce.self, from: data)
let obj = json.result.items.filter{$0.data != nil}
completion(.success(obj))
}
(Result<[ResultItem], Error>)
with (Result<[IsResult], Error>)
do {
let json = try JSONDecoder().decode(APIResponce.self, from: data)
// filter не работает т.к obj становится [ResulItem] , а completion [IsResult]
// let obj = json.result.items.filter{$0.data != nil}
completion(.success([json.filter]))
}
private var result = [ResultItem]()
// private var pag = [IsResult]()
private var viewModels = [NewsTableViewCellViewModel]()
private var id = 0
private var sortVal = 0.0
}
}
}
private func fetchTopStories() {
APICaller.shared.getTopStories(lastID: id, lastSortingValue: sortVal) { [weak self] json in
switch json {
case .success(let result):
// case .success(let pag):
// self?.pag = result.items // компилятор ругается
// self?.viewModels = result.items.compactMap({ viewModels in // компилятор ругается
self?.result = result
self?.viewModels = result.compactMap({ viewModels in
// guard let uuid = viewModels.data?.blocks?.first(where: {$0.data.text == nil && $0.data.items != nil})?.data.items?.first?.image?.data?.uuid else { return nil }
// return NewsTableViewCellViewModel(
// name: viewModels.data?.subsite?.name ?? "unknown",
// author: viewModels.data?.author?.name ?? "No name",
// title: viewModels.data?.title ?? "NO TITLE",
// subtitle: viewModels.data?.blocks?.first?.data.text ?? "",
// imageURL: URL(string: "https://leonardo.osnova.io/\(uuid.uuidString.lowercased())")
// )
})
DispatchQueue.main.async {
self?.tableView.reloadData()
}
// self?.id = pag.first?.lastID ?? 0
// self?.sortVal = pag.first?.lastSortingValue ?? 0
case .failure(let error):
print(error)
Answer the question
In order to leave comments, you need to log in
Sorry, but in my opinion you are already confused in your algorithms. You understand that you cannot assign a value of type A to a variable of type B, right? However, you try to do this and are surprised when the compiler swears.
If you need to change the type of the output parameter of a function, it is logical that you also need to change the functions that accept it.
No one knows better than you what you need. If you need to return IsResult - return it, if necessary [IsResult] - return it, if necessary [ResultItem] - then return it.
It seems to me that you are already confused in logic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question