Ios 如何将数据从应用程序委托传输到另一个类

Ios 如何将数据从应用程序委托传输到另一个类,ios,swift,uikit,appdelegate,Ios,Swift,Uikit,Appdelegate,我试图将包含数组的变量从我的应用程序委托传输到我称之为DataSource的类。但是我在传输数据时遇到了问题。当我查看并尝试调试我的应用程序时,它显示我的类DataSource中的变量没有值,而我的应用程序委托中的变量有值。这是我的密码,有人能帮我吗?[此外,我在此应用程序中使用了swiftui] AppDelegate: import UIKit import CoreData import Moya import Alamofire import AlamofireImage @UIApp

我试图将包含数组的变量从我的应用程序委托传输到我称之为DataSource的类。但是我在传输数据时遇到了问题。当我查看并尝试调试我的应用程序时,它显示我的类DataSource中的变量没有值,而我的应用程序委托中的变量有值。这是我的密码,有人能帮我吗?[此外,我在此应用程序中使用了swiftui]

AppDelegate:

import UIKit
import CoreData
import Moya
import Alamofire
import AlamofireImage

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

let service = MoyaProvider<YelpService.BusinessProvider>()
   let jsonDecoder = JSONDecoder()
var theViewModels = [RestrauntListViewModel]()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.



    loadBusinesses()


    return true
}


private func loadBusinesses () {
                    service.request(.search(lat: 34.0016, long: -117.8176)) { (result) in
                        switch result{
                        case.success(let response):
                            print("yaya")
                            let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
                            let viewModels = root?.businesses.compactMap(RestrauntListViewModel.init)
                            let dataSource = DataSource()
                            dataSource.arrayOfImages.removeAll()
                            for image in viewModels! {

                                Alamofire.request(image.imageURL).responseImage { response in
                                    if let image = response.result.value {
                                        print("image downloaded appdelegate")
                                        dataSource.arrayOfImages.append(image)
                                        print(dataSource.arrayOfImages)
                                    } else {
                                        print("ERROR: image does not = response.result.value")
                                    }
                                }
                            }

                            self.theViewModels = (root?.businesses.compactMap(RestrauntListViewModel.init))!
                            print("the constant theViewModels in the appdelegate has \(self.theViewModels.count) values")

                        case .failure(let error):
                            print("Error: \(error)")
                        }
    }
}

下面是一个简单的例子来解释我的评论:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var arrayOfData = [Int]()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        arrayOfData.append(1)
        arrayOfData.append(2)
        arrayOfData.append(3)

        return true
    }
然后,在VC中,您可以通过获取对应用程序委托的引用来访问阵列:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Get the data from app delegate
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
        print(appDelegate?.arrayOfData)
    }

}
在特定代码中,可以将此行移动到创建数据源的位置:

let dataSource = DataSource()
就在您声明视图模型(ViewModels)的位置之上,以便您可以从其他地方引用它,就像我对arrayOfData所做的那样

您可能还希望将您的属性设置为私有,并使用getter访问它,以遵守传统的编程实践

这里可以找到更多的例子,尽管公认的答案是针对Objective-C:


向下滚动,您将看到执行所需操作的其他方法,甚至可以在App Delegate和其他VCs之间来回传递数据。

您可以在LoadBusinesss()中创建一个数据源实例,该实例仅由图像下载请求捕获。完成所有图像下载后,该实例将超出范围并被删除,因为您没有将该实例存储在任何位置。peter,我如何存储该实例?您可以将数据源作为app delegate的财产。这样,您就可以从应用程序的其他部分访问它。
let dataSource = DataSource()