swift - 无法从闭包中修改类的变量
问题描述
我想将 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)
}
我什么都没有。
解决方案
推荐阅读
- python - 为什么我在使用 Pickle 和 Socket 库 Python 时出现 Unpickling 错误
- sql - 表模式和名称的正则表达式验证
- python - sam deploy 失败并出现错误存储桶位于此区域:us-east-2。请将此区域用于 us-east-2
- python - 'builtin_function_or_method' 对象没有属性 'apply' cv2
- c++ - 用C++从一个段落中找到一个句子中的最大单词数
- rest - 使用 Bitrix 24 REST API 将 CRM 项目链接到日历事件
- angular - Highcharts:在 X/Y 轴末端添加箭头
- reactjs - React js路由未显示组件
- winforms - 将字符串转换为数学表达式 [arduino]
- python-asyncio - 使用 asyncio.run,多次运行是否安全?