0

I'm learning how to get data using Github API and making a simple app in swift. Tutorials I've been watching make an empty array and geting response function in the same controller file. However, I want my view controller to keep as small as possible, and I decided to create getting data function in another file. My question is how can I access the empty array in a different file after I fetch data. Maybe this explanation make you confused, so I put my code below.

This is my view controller file, and I want to populate the repositories array after fetch the data using an API call.

import UIKit

class RepositoryListVC: UIViewController {

    var tableView = UITableView()
    var repositories: [Repository] = []

    override func viewDidLoad() {
        super.viewDidLoad()
    
        title = "AAAAAA"
    
        configureTableView()
    
        Service.fetchData()
    
    }


    func configureTableView() {
    
        view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 100
        tableView.register(RepositoryCell.self, forCellReuseIdentifier: Cells.repositoryCell)
        tableView.pin(to: view)
    
    }

}

extension RepositoryListVC: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: Cells.repositoryCell) as! RepositoryCell
    
        cell.set()
    
        return cell
    }

}

This is the file for fetching data using an API call. I want to populate the repository valuable in the view controller file above. I have successfully get data and parse JSON data, so I want to push Repository(...) to repository array, but I was not able to figure out how...

import UIKit
import SwiftyJSON
import Alamofire


struct Service {

    static func fetchData() {

        AF.request(API.gitHubEndpoint).responseJSON { (response) in
            switch response.result {
            case .success(let value):
                let json = JSON(value)
            
                let repositoryItemArr = json["items"].arrayValue
                
                for item in repositoryItemArr {                
                
                    Repository(userImageUrl: item["owner"]["avatar_url"].stringValue, userName: item["owner"]["user_name"].stringValue, repositoryName: item["owner"]["repository_name"].stringValue, starNum: item["owner"]["star_number"].intValue)

                }
            
            case .failure(let error):
                print(error)
            }
        
        }

    }
zzzzou
  • 177
  • 2
  • 15
  • You need a completion handler, see https://stackoverflow.com/questions/25203556/returning-data-from-async-call-in-swift-function – vadian Oct 13 '20 at 07:18
  • @vadian I made a completion handler and now it works!! Thank you!! – zzzzou Oct 13 '20 at 13:35

0 Answers0