G
G
Gleb2021-08-04 11:39:52
Swift
Gleb, 2021-08-04 11:39:52

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
    }
}


Parsing function:
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))
                }


Well, to get the lastID values, I replaced (Result<[ResultItem], Error>)with (Result<[IsResult], Error>)
and in the do block, respectively, we need to put
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]))
                }


There is another idea to process the result in the ViewController, but there I caught a lot of errors like:
cannot asign [ResultItem] expect [IsResult]
I commented out the idea:
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)


The question itself: is it possible to "painlessly" add to the [ResultItem] which I put in the complition the lastID and lastSortingValue parameters?
Or will I have to change the whole escaping to put in complition [IsResult]? If so, then I don’t know if it will be possible to filter the incoming data from nil and put it in complition.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
briahas, 2021-08-04
@shevzoom

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 question

Ask a Question

731 491 924 answers to any question