采用委托来自YouTube的数据未通过

问题描述 投票:1回答:1

我想要从家里控制器来自YouTube的数据和我的数据传递给我的视频控制器,但数据它没有被转移到我的视频控制器。但数据它的存在在家里控制器,当我尝试打印。这是我的代码

这是我的方案,我把上面的家居控制器类

protocol VideoListDelegate: class {
    func didAddVideoItem(results: [SearchResult])
}

这是我的实例代表在我家的控制器类

weak var delegate: VideoListDelegate?

这是我的API调用家居控制器

private func youtubeAPI(with query: Method) {
    let request = SearchListRequest(part: [.id, .snippet], maxResults: 50, searchQuery: query.rawValue)

    ApiSession.shared.send(request) { (results) in
        if let err = results.error {
            print("💥 -- Failed to get data -- 💥")
            print("💥 -- \(err) -- 💥")
            return
        }

        if let result = results.value?.items {
            self.delegate?.didAddVideoItem(results: result)
        }

        let layout = UICollectionViewFlowLayout()
        let searchVideoController = VideoController(collectionViewLayout: layout)

        self.navigationController?.pushViewController(searchVideoController, animated: true)
    }
}

然后我遵守我的视频控制器和我的协议

class VideoController: UICollectionViewController, UICollectionViewDelegateFlowLayout, VideoListDelegate {

// Add video into video array
func didAddVideoItem(results: [SearchResult]) {
    for item in results {
        print(item.snippet.title)

        let video = Video(title: item.snippet.title, videoId: item.id.videoID ?? "")
        self.videos.append(video)
    }

    DispatchQueue.main.async {
        self.collectionView.reloadData()
    }
  }
}
swift delegates
1个回答
0
投票

你的API方法应该是这样的,那么你就不需要代表团来传递数据:

private func youtubeAPI(with query: Method) {
    let request = SearchListRequest(part: [.id, .snippet], maxResults: 50, searchQuery: query.rawValue)

    ApiSession.shared.send(request) { (results) in
        if let err = results.error {
            print("💥 -- Failed to get data -- 💥")
            print("💥 -- \(err) -- 💥")
            return
        }

        let layout = UICollectionViewFlowLayout()
        let searchVideoController = VideoController(collectionViewLayout: layout)
        searchVideoController.results = results.value?.items ?? []
        self.navigationController?.pushViewController(searchVideoController, animated: true)
    }
}

而在你VideoViewController添加型results的一个变量[SearchResult]

热门问题
推荐问题
最新问题