首页 > 解决方案 > 无法从闭包中修改类的变量

问题描述

我想将 decodedResult 变量分配给 exchangeInformations 变量: self.exchangeInformations = decodedResult

我创建了一个 exchangeInfo() 类的实例。我尝试打印 exchangeInformations 变量的值,但结果为零。为什么?

import Cocoa

class exchangeInfo {

    var exchangeInformations: exchangeInformation? //Exchange information
    let exchangeInformationURL: String = "https://api.binance.com/api/v3/exchangeInfo"
    
    struct exchangeInformation: Codable {
        var timezone: String //UTC
        var serverTime: Int //1565246363776
        var rateLimits: [rateLimits] = []//Rate limits
        var exchangeFilters: [exchangeFilters] = [] //Exchange filters
        var symbols: [symbols] //Symbols
    }
    
    struct rateLimits: Codable { //Rate limits
        var rateLimitType: String //REQUEST_WEIGHT, ORDERS, RAW_REQUESTS
        var interval: String //interval
        var intervalNum: UInt16 //1
        var limit: UInt32 //1200
    }
    
    struct exchangeFilters: Codable {
        var filterType: String
        var minPrice, maxPrice, tickSize, multiplierUp, multiplierDown, minQty, maxQty, stepSize, minNotional, maxPosition: String?
        var avgPriceMins, limit, maxNumOrders, maxNumAlgoOrders, maxNumIcebergOrders: UInt16?
        var applyToMarket: Bool?
    }
    
    struct symbols: Codable { //Symbols
        var symbol: String //ETHBTC
        var status: String //TRADING
        var baseAsset: String //ETH
        var baseAssetPrecision: UInt16 //8
        var quoteAsset: String //BTC
        var quotePrecision: UInt16 //8
        var quoteAssetPrecision: UInt16 //8
        var baseCommissionPrecision: UInt16 //8
        var quoteCommissionPrecision: UInt16 //8
        var orderTypes: [String] //orderTypes
        var icebergAllowed: Bool //true
        var ocoAllowed: Bool //true
        var quoteOrderQtyMarketAllowed: Bool //true
        var isSpotTradingAllowed: Bool //true
        var isMarginTradingAllowed: Bool //true
        var filters: [exchangeFilters] = [] //Filters
        var permissions: [String] //Permissions
    }
    
    init() {
        
        guard let url = URL(string: exchangeInformationURL) else {
            print("URL is not valid")
            return
        }
        
        let request = URLRequest(url: url)
        
        let configuration = URLSessionConfiguration.ephemeral
        let session = URLSession(configuration: configuration)
        
        session.dataTask(with: request) { data, response, error in
            
            do {
                if let adat = data {
                    
                    if let decodedResult = try? JSONDecoder().decode(exchangeInformation.self, from: adat) {
                        
                        DispatchQueue.main.async {
                            
                            self.exchangeInformations = decodedResult
                            
                        }
                    }
                    
                }
            } catch {
                print("Error: \(error.localizedDescription)")
            }
        }.resume()
    }
}

var informacio = exchangeInfo()


if let serverTime = informacio.exchangeInformations?.serverTime {
    print (serverTime)
}

我什么都没有。

标签: swiftclassvariablesclosuresself

解决方案


推荐阅读