No of course it isn't the real code
I wanted to isolate the response of the compiler
The Code is quite complex
class ResponseListModel: Decodable {
var moderators: [T]
var totalEnreg: Int
var hasMore: Bool
var currentPage: Int
var indexPage: Int = 0
var limit: Int = 0
var nbPagesInMemory: Int!
var fetchingData: Bool = true
enum CodingKeys: String, CodingKey {
case moderators = "items"
case hasMore = "has_more"
case totalEnreg = "total"
case currentPage = "page"
}
func destroyPageData(_ index: Int) {
moderators.removeSubrange(index..<index+min(limit, moderators.count))<br=""> }
func rowLoaded (_ row: Int) -> Bool {
let firstIndex = (currentPage-1)*limit
let lastIndex = firstIndex + moderators.count
return row >= firstIndex && row < lastIndex
}
func rowIndex (_ row: Int) -> Int {
return row - (currentPage - 1)*limit
}
func row(_ row: Int) -> T {
let rowCalculee = row - (currentPage-1)*limit
return moderators[rowCalculee]
}
func clean() {
moderators.removeAll()
}
func fetch(_ tableview: NSTableView, _ row: Int, params: Parameters, response: ResponseListModel.Type, completion: @escaping (Bool) -> Void) where T: Decodable {
Swift.print("ca tourne pour row=\(row)")
if !fetchingData /*|| !rowLoaded(row)*/ {
fetchingData = true
// 1 calculate what part of the array has to be destroyed
if row < indexPage {
destroyPageData((nbPagesInMemory-1)*limit)
} else {
destroyPageData(0)
self.indexPage += limit
}
// 2 Fetch data if necessary
StackExchangeClient().fetch(request: BaseRequest(parameters: params), dataResponse: response, completion: {
result in
switch result{
case .failure(_):
completion(false)
case .success(let response):
let rep = response as! ResponseListModel
self.currentPage += rep.moderators.count / self.limit
if row < self.indexPage {
self.moderators.insert(rep.moderators, at: 0)
} else {
self.moderators.insert(contentsOf: rep.moderators, at: self.moderators.count)
}
self.moderators.append(rep.moderators)
let rows = NSIndexSet(indexesIn: NSMakeRange(row, self.indexPage+self.moderators.count-1)) as IndexSet
let cols = NSIndexSet( indexesIn: NSMakeRange(0, tableview.numberOfColumns)) as IndexSet
DispatchQueue.main.async {
self.fetchingData = false
tableview.reloadData(forRowIndexes: rows, columnIndexes: cols)
}
completion(true)
}
})
}
}
I can't correctly reproduce the header of the fetch function with the advance.button of this forum, it corrects the code
my code is
func fetch<T>(_ tableview: NSTableView, _ row: Int, params: Parameters, response: ResponseListModel<T>.Type, completion: @escaping (Bool) -> Void) where T: Decodable {
}
and my problem appears on lines 63 and 65