简体   繁体   English

Swift:将数据从 function 传递到变量时出现问题

[英]Swift: Issue with passing data from function to a variable

I am trying to pass data from a function to a declared variable,我正在尝试将数据从 function 传递给声明的变量,

   func request(platform:String, rankData:[String : [Model]]) {
 
        
        APIService.shared.get(api: "someURL", parameters:[platform: platform], completion: { (result: Result<ModelResult,AFError>) in

            switch result {
            case .success(let success):
                
                rankData = success.result
                self.collectionView.reloadData()
                
            case .failure(let failure):
                print(failure.errorDescription!)
            }
        })
        
  
    }

but I am getting this error但我收到了这个错误

Cannot assign to value: 'rankData' is a 'let' constant

I added inout but still not working, also tried to add我添加了inout但仍然无法正常工作,也尝试添加

 var aData = rankData 

but still, I cannot pass data to my variables and xcode give this warrning:但是,我仍然无法将数据传递给我的变量,并且 xcode 给出了这个警告:

Variable 'aData' was written to, but never read

How I want to use:我想如何使用:

var firstResult:[String : [Model]] = [:]
request(platform: "car", rankData: firstResult)

any help?有什么帮助吗?

You cannot return something asynchronous through an inout parameter.您不能通过 inout 参数返回异步内容。

But the solution is pretty easy.但解决方案非常简单。

Replace代替

func request(platform:String, rankData:[String : [Model]]) {

with

func request(platform:String) {

And the line和线

rankData = success.result

with

self.firstResult = success.result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM