-5

I'm trying to fetch the first 30 GitHub users using https://api.github.com/users and add it to a list of fetched users. For each user; display their username, avatar image and number of repositories.

When a user is selected, I need to open another screen containing a list of all their repositories.

How can I have the access to nested URL ("repos_url") in GitHub API following this JSON format?

{
    "login": "mojombo",
    "id": 1,
    "repos_url": "https://api.github.com/users/mojombo/repos"
} 
KSigWyatt
  • 1,368
  • 1
  • 16
  • 33
iOSDev
  • 1
  • 3
  • Possible duplicate of [How to parse a JSON file in swift?](https://stackoverflow.com/questions/24013410/how-to-parse-a-json-file-in-swift) – KSigWyatt Nov 24 '17 at 23:11

1 Answers1

0
// don't forget 
import Alamofire
import SwiftyJSON

// initialise variables

var fetchResult = [[String:AnyObject]]()
struct User{
var name:String?
var id:Int?
var repo_url:String

init(name:String,id:Int,repoURL:String){

self.name = name
self.id = id
self.repo_url = repoURL

}
}

let user = [User]


fun getData(){

    Alamofire.request("https://api.github.com/users").responseJSON { (responseData) -> Void in
                if((responseData.result.value) != nil) {
                    let response = JSON(responseData.result.value!)
                    print(response)

                    if let resData = response.arrayObject {

                      self.fetchResult = resData as! [[String:AnyObject]]

                       print(self.fetchResult)
                    }

                    for item in self.fetchResult {

                        print(item)

                       guard let login = item["login"] as! String else {return}

                       guard let id = item["id"] as! Int else {return}

                       guard let repos_url = item["repos_url"] as! String else {return}

                        let gitUser = User(name: login , id: id, repoURL: repos_url)
                        self.user.append(gitUser)
                    }

                }
              if self.user.count>0{
             self()
    }

}

Once you selected a row in tableView. get selected item in didSelect by using

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {

    let selected = user[indexPath.row]


   let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let newViewController = 
    storyBoard.instantiateViewController(withIdentifier: "SecondVC") as! SecondVC
    newViewController.data = selected
    self.navigationController?.pushViewController(newViewController, animated: true)

    }

In the SecondViewController you can access the selected data set.

Junaid Ali
  • 175
  • 1
  • 2
  • 14
  • Thank you for your support but your project does not provide the information how many repositories belong to a user. It should result from the dictionary: "repos_url": "https://api.github.com/users/mojombo/repos". My question is how should I access this data? – iOSDev Nov 24 '17 at 23:28