1

我正在努力学习 VIPER。我跟着这个教程。我有这些InteractorPresenter

class PPresenter: ViewToPresenterProtocol {
    
    var view: PresenterToViewProtocol?  
    
    var router: PresenterToRouterProtocol? = PRouter()
    
    var interactor: PresenterToInteractorProtocol? = PInteractor()

    
    func initiateFetch() {
        interactor?.fetchMatches()
    }
    
    func showMatchScreen(navigationC: UIViewController) {
        router?.pushToMatchDetailScreen(navigationC: navigationC)
    }
}

extension PPresenter: InteractorToPresenterProtocol {
    func matchFetched(match: MatchDetails?, banner: Banner?) {
        print(match!)
        
        print(banner!)
    }
    
    func matchFetchError() {
        //TODO
    }
}


class PInteractor: PresenterToInteractorProtocol {
    
    var presenter: InteractorToPresenterProtocol? = PPresenter()
    
    var live: Live?
    
    var upcoming: Upcoming?
    
    var banners: Banner?
    
    func fetchMatches() {
        let parameters = ["api_token" : Constants.USER_INFO["api_token"].rawValue,"player_id" : Constants.USER_INFO["player_id"].rawValue]
        
        ServiceHelper.sharedInstance.sendRequest(path: "get-predictor", params: parameters, showSpinner: true) { (response, error) in
            
            if let error = error {
                print("Unable to fetch match listing",error.localizedDescription)
                
                return
            } else {
                guard let obj = try? JSONDecoder().decode(MatchDetails.self, from: response.rawData()) else { self.presenter?.matchFetchError(); return }
                
                guard let bannerObj = try? JSONDecoder().decode(Banner.self,from: response.rawData()) else {self.presenter?.matchFetchError(); return }
                
                self.presenter?.matchFetched(match: obj, banner: bannerObj)
            }
        }
    }
}

现在,这里发生了什么,我让路由器工作,视图来了,它正在调用演示者,演示者正在调用交互器,交互器正在成功调用 API 并获取数据,现在是时候返回数据了从InteractortoPresenter和 here 收到它不断抛出以下错误:

线程 1 EXC_BAD_ACCESS(代码=2,地址=0x7ffeeb1aeff8)

4

1 回答 1

1

我认为你有一个循环调用,也许你的交互器没有完全初始化,然后你想要从中获取数据,然后你得到“错误访问错误”。

于 2018-11-27T11:35:53.247 回答