// ViewController.swift
// AgileAPIPractice
import UIKit
import Alamofire
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var dictfromjson = NSDictionary()
@IBOutlet var tblView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getMethod()
// PostMethod()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getMethod()
{
let urlPath : String = "http://180.211.99.165/rs/ovg/api/v1/category_list"
let req = request(urlPath, method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil)
req.responseJSON { (response) in
if response.result.isFailure{
print("fail")
}
if response.result.isSuccess{
if let json = response.result.value as? NSDictionary
{
self.dictfromjson = json as NSDictionary
// print(dictfromjson)
let response = self.dictfromjson["response"] as! NSDictionary
// print(response)
let CategoryList = response["CategoryList"] as! NSArray
//print(CategoryList)
let firstIndex = CategoryList[0] as! NSDictionary
// print(firstIndex)
let category_id = firstIndex["category_id"] as! String
// print(category_id)
let category_name = firstIndex["category_name"] as! String
// print(category_name)
let subcategories = firstIndex["subcategories"] as! NSArray
// print(subcategories)
let name = (((self.dictfromjson["response"] as! NSDictionary).value(forKey: "CategoryList") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "category_name") as! String
print(name)
}
}
}
}
func PostMethod()
{
let dict = ["name":"aa","email":"aa@gmail.com","suggestion":"aaaaaaaa"]
let urlPath : String = "http://180.211.99.165/rs/ovg/api/v1/feedback"
let req = request(urlPath, method: .post, parameters: dict, encoding:URLEncoding.default , headers: nil)
req.responseJSON { (response) in
if response.result.isFailure{
print("fail")
}
if response.result.isSuccess{
if let json = response.result.value as? NSDictionary
{
print(json)
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if dictfromjson.count == 0
{
return 0
}
else
{
return (((self.dictfromjson.value(forKey: "response") as! NSDictionary).value(forKey: "CategoryList")) as! NSArray).count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : CustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomTableViewCell
cell.lbl.text = ((((self.dictfromjson.value(forKey: "response") as! NSDictionary).value(forKey: "CategoryList")) as! NSArray).object(at: (indexPath.row)) as! NSDictionary).value(forKey: "category_name") as? String
return cell
}
}
Comments
Post a Comment