// 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()
}
}
。 は、あなたが選択したデータセットにアクセスすることができSecondViewControllerで
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)
}
を使用してdidSelect内の項目を選択します。
[JSONファイルの解析方法](https://stackoverflow.com/questions/24013410/how-to-parse-a-json-file-in-swift) – KSigWyatt