How to load and get data from 2 files I want to load 2 files and show this data in 2 strings. I trying to do it like this and it works fine. But I think it is not the best solution... How to do more elegant solution?
var saleNumber1 = &quot;&quot;
var saleNumber2 = &quot;&quot;

override func viewDidLoad() {
        super.viewDidLoad()
        
        sale1()
        sale2()
}

func sale1() {
        
        let url = URL(string: &quot;http://example.com/folder/sale1.txt&quot;)!

        let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
            guard let data = data else { return }
            saleNumber1 = &quot;\(String(data: data, encoding: .utf8)!)&quot;
        }

        task.resume()
        
    }
    
    func sale2() {
        
        let url = URL(string: &quot;http://example.com/folder/sale2.txt&quot;)!

        let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
            guard let data = data else { return }
            saleNumber2 = &quot;\(String(data: data, encoding: .utf8)!)&quot;
        }

        task.resume()
        
    }
Time to learn how to DRY off your WET code!
(DRY -&gt; &quot;Don't Repeat Yourself&quot;; WET -&gt; &quot;Write Everything Twice&quot;)
Note how the only difference between your two functions is a URL and a change in data, effectively &quot;changing input results in changing output.&quot; Consider consolidating the two into one generic function that takes a variable url in as input and returns data which can be store in a variable of your choice.
var saleNumber1 = &quot;&quot;
var saleNumber2 = &quot;&quot;

override func viewDidLoad() {
    super.viewDidLoad()
        
    saleNumber1 = sale(&quot;http://example.com/folder/sale1.txt&quot;)
    saleNumber2 = sale(&quot;http://example.com/folder/sale2.txt&quot;)
}

func sale(path: String) -&gt; String {
    let url = URL(string: path)!
    let saleNumber = &quot;&quot;
    let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
        guard let data = data else { return }
        saleNumber = &quot;\(String(data: data, encoding: .utf8)!)&quot;
    }

    task.resume()

    return saleNumber
}