【发布时间】:2020-06-28 23:17:29
【问题描述】:
我在 AppDelegate 中收到此错误,但我不确定问题可能是什么。任何帮助表示赞赏,谢谢!
(出现错误的确切行是:“private func loadDetails”部分中的“let detailsViewModel = DetailsJobView(details: details)”)
顺便说一句,错误在(详细信息:详细信息)中的第二个“详细信息”中强调了“d”
我已经在代码中注明了,但可能很难找到。
import UIKit
import Firebase
import CoreLocation
import Moya
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let window = UIWindow()
let locationService = LocationService()
let homeStoryboard = UIStoryboard(name: "Home", bundle: nil)
let service = MoyaProvider<YelpService.BusinessesProvider>()
let jsonDecoder = JSONDecoder()
var navigationController: UINavigationController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Change color of tab bar items
UITabBar.appearance().tintColor = .black
FirebaseApp.configure()
jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
locationService.didChangeStatus = { [weak self] success in
if success {
self?.locationService.getLocation()
}
}
locationService.newLocation = { [weak self] result in
switch result {
case .success(let location):
self?.loadJobs(with: location.coordinate)
case .failure(let error):
assertionFailure("Error getting the users location \(error)")
}
}
switch locationService.status {
case .notDetermined, .denied, .restricted:
let locationViewController = homeStoryboard.instantiateViewController(withIdentifier: "LocationViewController") as? LocationViewController
locationViewController?.delegate = self
window.rootViewController = locationViewController
default:
let nav = homeStoryboard
.instantiateViewController(withIdentifier: "JobNavigationController") as? UINavigationController
self.navigationController = nav
window.rootViewController = nav
locationService.getLocation()
(nav?.topViewController as? JobTableViewController)?.delegete = self
}
window.makeKeyAndVisible()
return true
}
private func loadDetails(for viewController: UIViewController, withId id: String) {
service.request(.details(id: id)) { [weak self] (result) in
switch result {
case .success(let response):
guard let strongSelf = self else { return }
if let details = try? strongSelf.jsonDecoder.decode(Details.self, from: response.data) {
let detailsViewModel = DetailsJobView(details: details) //ERROR IS HERE
(viewController as? DetailsJobViewController)?.viewModel = detailsViewModel
}
case .failure(let error):
print("Failed to get details \(error)")
}
}
}
private func loadJobs(with coordinate: CLLocationCoordinate2D) {
service.request(.search(lat: coordinate.latitude, long: coordinate.longitude)) { [weak self] (result) in
guard let strongSelf = self else { return }
switch result {
case .success(let response):
let root = try? strongSelf.jsonDecoder.decode(Root.self, from: response.data)
let viewModels = root?.jobs
.compactMap(JobListViewModel.init)
.sorted(by: { $0.distance < $1.distance })
if let nav = strongSelf.window.rootViewController as? UINavigationController,
let jobListViewController = nav.topViewController as? JobTableViewController {
jobListViewController.viewModels = viewModels ?? []
} else if let nav = strongSelf.homeStoryboard
.instantiateViewController(withIdentifier: "JobNavigationController") as? UINavigationController {
strongSelf.navigationController = nav
strongSelf.window.rootViewController?.present(nav, animated: true) {
(nav.topViewController as? JobTableViewController)?.delegete = self
(nav.topViewController as? JobTableViewController)?.viewModels = viewModels ?? []
}
}
case .failure(let error):
print("Error: \(error)")
}
}
}
}
extension AppDelegate: LocationActions, ListActions {
func didTapAllow() {
locationService.requestLocationAuthorization()
}
func didTapCell(_ viewController: UIViewController, viewModel: JobListViewModel) {
loadDetails(for: viewController, withId: viewModel.id)
}
}
这是 @heitormurara 要求的 DetailsJobView:
import UIKit
import MapKit
@IBDesignable class DetailsJobView: CoreView {
@IBOutlet weak var collectionView: UICollectionView?
@IBOutlet weak var pageControl: UIPageControl?
@IBOutlet weak var priceLabel: UILabel?
@IBOutlet weak var hoursLabel: UILabel?
@IBOutlet weak var locationLabel: UILabel?
@IBOutlet weak var ratingsLabel: UILabel?
@IBOutlet weak var mapView: MKMapView?
@IBAction func handleControl(_ sender: UIPageControl) {
}
}
【问题讨论】:
-
您能提供您的
DetailsJobView初始化程序吗?另外,能否调试一下错误行中的变量details? -
@heitormurara 当然,我会编辑问题。另外,如何调试变量详细信息?谢谢谢谢。
-
在您的 Xcode 中有行号指示器,单击所需的 (在这种情况下,您需要
let detailsViewModel = DetailsJobView(details: details)行),您将有一个断点,然后当执行通过该行时,您的项目将停止。如果您的控制台尚未打开,请单击⇧⌘C将其打开。然后,写po details,你就会得到信息。 -
@heitormurara 对不起,我以前从未这样做过。当您说执行时,您的意思是构建应用程序对吗?导致我由于错误而无法构建应用程序,即使有断点
-
是的,这就是我的意思,忘记了“无法构建问题”抱歉