从 Yelp Api 获取信息
Get info from Yelp Api
我连接到 Yelp Api 以获取数据,然后以卡片格式显示这些数据。但是,我的问题是我似乎无法将 yelp 数据传递到此数组中以提取 yelp 数据。我该怎么做呢?我怎样才能在 20 家餐厅的规模上做到这一点?
这是我获取信息的方式:
//
// Get Restaurants.swift
// Pickt
//
// Created by Morris Richman on 8/29/21.
//
import Foundation
import Moya
private let apiKey = "<api key>"
enum YelpService {
enum BuisnessesProvider: TargetType {
var baseURL: URL {
return URL(string: "https://api.yelp.com/v3/businesses")!
}
var path: String {
switch self {
case .search:
return "/search"
}
}
var method: Moya.Method {
return .get
}
var sampleData: Data {
return Data()
}
var task: Task {
switch self {
case let .search(loc):
return .requestParameters(parameters: ["location": loc, "term": "restaurants", "limit": 1], encoding: URLEncoding.queryString)
}
}
var headers: [String : String]? {
return ["Authorization": "Bearer \(apiKey)"]
}
case search(loc: String)
}
}
struct Root: Codable {
let businesses: [Business]
}
struct Business: Codable {
let id: String
let name: String
let imageUrl: URL
let rating: Double
let url: String
}
struct RestaurantListViewModel {
let name: String
let imageUrl: URL
let id: String
let rating: Double
let url: String
}
extension RestaurantListViewModel {
init(business: Business) {
self.name = business.name
self.id = business.id
self.imageUrl = business.imageUrl
self.rating = business.rating
self.url = business.url
}
}
正在调用获取函数:
let service = MoyaProvider<YelpService.BuisnessesProvider>()
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
service.request(.search(loc: "98117")) { result in
switch result {
case .success(let response):
let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
let viewModels = root?.businesses.compactMap(RestaurantListViewModel.init)
print(viewModels!)
print(Restaurants.mock)
Restaurants.viewModels = viewModels!
// print(try? JSONSerialization.jsonObject(with: response.data, options: []))
case .failure(let error):
print("Error = \(error)")
}
}
终于取回了它:
let name: String
let imageUrl: URL
let id: String
let rating: Double
let url: String
static var viewModels: [RestaurantListViewModel] = []
static var mock: [Restaurants] = [
Restaurants(name: "Silverio's Mexican Kitchen", imageUrl: URL(string: "https://mainsite-prod-cdn.azureedge.net/partner-images/432257/micrositeimage_p1.jpg")!, id: "hjkhjhjh", rating: 2, url: "https://google.com"),
Restaurants(name: "Taqueria La Esquinita", imageUrl: URL(string: "https://s3-media0.fl.yelpcdn.com/bphoto/x-KCQ7osmvBWLA9WpPdO_Q/o.jpg")!, id: "hjdha", rating: 3, url: "https://google.com")
]
我想我使用 CDYelpFusionKit 找到了答案。我唯一的问题是当我通过 Cocoapods 安装时,我得到这个错误:
Multiple commands produce '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/Pickt.app/Frameworks/CDYelpFusionKit.framework': 1) Target 'Pickt' has copy command from '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/CDYelpFusionKit.framework' to '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/Pickt.app/Frameworks/CDYelpFusionKit.framework' 2) That command depends on command in Target 'Pickt': script phase “[CP] Embed Pods Frameworks”
为什么不使用“业务”模型保存在数组中。
已经有你想在“RestaurantListViewModel”
中复制的值
> let array = [Business]()
> let service = MoyaProvider<YelpService.BuisnessesProvider>()
> let jsonDecoder = JSONDecoder()
> jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
> service.request(.search(loc: "98117")) { result in
> switch result {
> case .success(let response):
> let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
> array.append(contentsOf:root.businesses)
> // print(try? JSONSerialization.jsonObject(with: response.data, options: []))
> case .failure(let error):
> print("Error = \(error)")
> }
> }
此外,使用 limit 和 offset 查询参数获取页面信息,并使用 array.count() 作为偏移量,在 yelp API[=12 中默认限制为 20 =]
修复它,它正在获取数据并附加它,只是内容没有显示,因为它会在完成之前加载。
我连接到 Yelp Api 以获取数据,然后以卡片格式显示这些数据。但是,我的问题是我似乎无法将 yelp 数据传递到此数组中以提取 yelp 数据。我该怎么做呢?我怎样才能在 20 家餐厅的规模上做到这一点?
这是我获取信息的方式:
//
// Get Restaurants.swift
// Pickt
//
// Created by Morris Richman on 8/29/21.
//
import Foundation
import Moya
private let apiKey = "<api key>"
enum YelpService {
enum BuisnessesProvider: TargetType {
var baseURL: URL {
return URL(string: "https://api.yelp.com/v3/businesses")!
}
var path: String {
switch self {
case .search:
return "/search"
}
}
var method: Moya.Method {
return .get
}
var sampleData: Data {
return Data()
}
var task: Task {
switch self {
case let .search(loc):
return .requestParameters(parameters: ["location": loc, "term": "restaurants", "limit": 1], encoding: URLEncoding.queryString)
}
}
var headers: [String : String]? {
return ["Authorization": "Bearer \(apiKey)"]
}
case search(loc: String)
}
}
struct Root: Codable {
let businesses: [Business]
}
struct Business: Codable {
let id: String
let name: String
let imageUrl: URL
let rating: Double
let url: String
}
struct RestaurantListViewModel {
let name: String
let imageUrl: URL
let id: String
let rating: Double
let url: String
}
extension RestaurantListViewModel {
init(business: Business) {
self.name = business.name
self.id = business.id
self.imageUrl = business.imageUrl
self.rating = business.rating
self.url = business.url
}
}
正在调用获取函数:
let service = MoyaProvider<YelpService.BuisnessesProvider>()
let jsonDecoder = JSONDecoder()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
service.request(.search(loc: "98117")) { result in
switch result {
case .success(let response):
let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
let viewModels = root?.businesses.compactMap(RestaurantListViewModel.init)
print(viewModels!)
print(Restaurants.mock)
Restaurants.viewModels = viewModels!
// print(try? JSONSerialization.jsonObject(with: response.data, options: []))
case .failure(let error):
print("Error = \(error)")
}
}
终于取回了它:
let name: String
let imageUrl: URL
let id: String
let rating: Double
let url: String
static var viewModels: [RestaurantListViewModel] = []
static var mock: [Restaurants] = [
Restaurants(name: "Silverio's Mexican Kitchen", imageUrl: URL(string: "https://mainsite-prod-cdn.azureedge.net/partner-images/432257/micrositeimage_p1.jpg")!, id: "hjkhjhjh", rating: 2, url: "https://google.com"),
Restaurants(name: "Taqueria La Esquinita", imageUrl: URL(string: "https://s3-media0.fl.yelpcdn.com/bphoto/x-KCQ7osmvBWLA9WpPdO_Q/o.jpg")!, id: "hjdha", rating: 3, url: "https://google.com")
]
我想我使用 CDYelpFusionKit 找到了答案。我唯一的问题是当我通过 Cocoapods 安装时,我得到这个错误:
Multiple commands produce '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/Pickt.app/Frameworks/CDYelpFusionKit.framework': 1) Target 'Pickt' has copy command from '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/CDYelpFusionKit.framework' to '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/Pickt.app/Frameworks/CDYelpFusionKit.framework' 2) That command depends on command in Target 'Pickt': script phase “[CP] Embed Pods Frameworks”
为什么不使用“业务”模型保存在数组中。 已经有你想在“RestaurantListViewModel”
中复制的值> let array = [Business]()
> let service = MoyaProvider<YelpService.BuisnessesProvider>()
> let jsonDecoder = JSONDecoder()
> jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
> service.request(.search(loc: "98117")) { result in
> switch result {
> case .success(let response):
> let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
> array.append(contentsOf:root.businesses)
> // print(try? JSONSerialization.jsonObject(with: response.data, options: []))
> case .failure(let error):
> print("Error = \(error)")
> }
> }
此外,使用 limit 和 offset 查询参数获取页面信息,并使用 array.count() 作为偏移量,在 yelp API[=12 中默认限制为 20 =]
修复它,它正在获取数据并附加它,只是内容没有显示,因为它会在完成之前加载。