Код: Выделить всё
protocol SearchApiResource {
associatedtype ModelType: Decodable
var methodPath: String { get }
var searchTerm: String { get set }
var pageNumber: Int { get set }
var parameters: [String: String] { get }
var url: URL { get }
}
Код: Выделить всё
struct SearchPhotoResource: SearchApiResource {
typealias ModelType = Photo
var methodPath = "/search/photos"
var searchTerm = String()
var pageNumber = Int()
let itemsPerPage = 30
let accessKey = "93e0a185df414cc1d0351dc2238627b7e5af3a64bb228244bc925346485f1f44"
var parameters: [String: String] {
var params = [String: String]()
params["query"] = searchTerm
params["page"] = String(pageNumber)
params["per_page"] = String(itemsPerPage)
params["client_id"] = accessKey
return params
}
var url: URL {
var components = URLComponents()
components.scheme = "https"
components.host = "api.unsplash.com"
components.path = methodPath
components.queryItems = parameters.map {URLQueryItem(name: $0, value: $1)}
return components.url!
}
}
Код: Выделить всё
func searchForItem(resource: SearchApiResource, searchTerm: String, pageNumber: Int, completion: @escaping (SearchApiResource.ModelType) -> Void ) {
}
"Связанный тип ModelType можно использовать только с конкретным типом или база общих параметров"
Как исправить ошибку и что я делаю не так?
Подробнее здесь: https://stackoverflow.com/questions/595 ... rom-method
Мобильная версия