gpt4 book ai didi

ios - swift 中的依赖倒置

转载 作者:行者123 更新时间:2023-11-28 13:49:39 25 4
gpt4 key购买 nike

您好,我有一个问题想问这里的开发人员,我正在阅读 big nerd ranch 的 iOS 编程书籍。我对如何构建和技术来创建应用程序很感兴趣。我试图实现它,它是一个依赖倒置,代码是这样的

这是书中app delegate中的代码

let rootViewController = window!.rootViewController as! UINavigationController
let photosViewController = rootViewController.topViewController as! PhotoViewController
photosViewController.store = PhotoStore()

这是 photoViewController 类

class PhotoInfoViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!

var photo: Photo! {
didSet {
navigationItem.title = photo.title
}
}

var store: PhotoStore!

override func viewDidLoad() {
super.viewDidLoad()

store.fetchImage(for: photo) { (result) in
switch result {
case let .success(image):
self.imageView.image = image
case let .failure(error):
print("Error fetching image for photo: \(error)")
}
}
}
}

这是用于依赖倒置代码的照片存储类

enum ImageResult {
case success(UIImage)
case failure(Error)
}

enum PhotoError: Error {
case imageCreationError
}

enum PhotoResult {
case success([Photo])
case failure(Error)
}

class PhotoStore {
private let session: URLSession = {
return URLSession(configuration: .default)
}()

let imageStore = ImageStore()

func fetchInterestingPhoto(completion: @escaping (PhotoResult) -> Void) {
let url = FlickerAPI.interestingPhotoURL
let request = URLRequest(url: url)
let task = session.dataTask(with: request) { (data, response, error) in
let result = self.processPhotosRequest(data: data, error: error)

OperationQueue.main.addOperation {
completion(result)
}
}
task.resume()
}

private func processPhotosRequest(data: Data?, error: Error?) -> PhotoResult {
guard let jsonData = data else { return .failure(error!) }
return FlickerAPI.photos(fromJSON: jsonData)
}

func fetchImage(for photo: Photo, completion: @escaping (ImageResult) -> Void) {
let photoKey = photo.photoID
if let image = imageStore.image(forKey: photoKey) {
OperationQueue.main.addOperation {
completion(.success(image))
}
return
}

let photoURL = photo.remoteURL
let request = URLRequest(url: photoURL)

let task = session.dataTask(with: request) { (data, response, error) in
let result = self.processImageRequest(data: data, error: error)

if case let .success(image) = result {
self.imageStore.setImage(image, forKey: photoKey)
}

OperationQueue.main.addOperation {
completion(result)
}
}
task.resume()
}

private func processImageRequest(data: Data?, error: Error?) -> ImageResult {
guard
let imageData = data,
let image = UIImage(data: imageData)
else {
// Couldn't create an image
if data == nil {
return .failure(error!)
} else {
return .failure(PhotoError.imageCreationError)
}
}
return .success(image)
}
}

我要问的是,如果我有一个选项卡栏 Controller 并且假设有 2 个 View Controller ,其中一个正在获取新闻数据,而另一个 Controller 正在获取天气数据。我如何实现这种依赖倒置,因为应用程序委托(delegate)中的 Root View Controller 将是选项卡栏 Controller ,这就是我混淆实现它的原因?因为这本书只使用一个 View Controller 。

最佳答案

请记住这是一个 hack,在我看来,不应该这样做,因为依赖项应该在 init 中声明。但是因为您可能正在从 Storyboard 创建 TabBar。您只有这个选项可以从窗口获取 TabBar。 TabBarController 有一组构成 TabBar 的 View Controller 。您只需迭代并尝试将它们转换为 PhotoViewController。如果是,则设置商店:

let rootViewController = window!.rootViewController as! UITabBarController

for viewController in rootViewController.viewControllers {
switch viewController {
case let photoViewController as PhotoViewController:
photoViewController.store = PhotoStore()
default:
break
}
}

关于ios - swift 中的依赖倒置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54856504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com