get variable outside of the loop



i have a problem with my code…


//  ViewController.swift
//  Copyright © 2017 Producer. All rights reserved.

import UIKit
import Foundation

struct CurrencyStore {
    var name: String
    var currencies: [Currency]
    struct Currency: Codable{
        var symbol1: String
        var symbol2: String
        var minLotSize: Double
        var minLotSizeS2: Double
        var maxLotSize: Double?
        var maxPrice: String
        var minPrice: String

let json = """
""".data(using: .utf8)!

struct CurrencyStoreService: Decodable {
    let e: String
    let ok: String
    let data: NestedData
    struct NestedData: Decodable {
        let pairs: [CurrencyStore.Currency]

extension CurrencyStore {
    init(from CurrencyStoreService: CurrencyStoreService) {
        name = CurrencyStoreService.ok
        currencies = []

class ViewController: UIViewController {
    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        do {
            let serverData = try JSONDecoder().decode(CurrencyStoreService.self, from: json)
        } catch let jsonErr {
    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.


The problem is row 55.

serverData only exists in the do loop, how can i define that as a gloabl variable? i mean how shoud i do it that the variable can be used in all classes.

My problem is that i musst use this object in other classes and functions… in some other languages its simple to define an global Variable but i dont found any related topic for swift.

Powered by WPeMatico

You may also like...

Comments are closed.