I want to get the value from the API in swift and display the response in Controller. I have created the table and I am getting the response from API using Alarmofire. My Question is How to use the Success response of Alarmofire and display in Array. My code is here :
class NotificationHistory : UIViewController, UITableViewDelegate,UITableViewDataSource {
let urlStringChecking = "http://www.XXXXXXXX.com/uhf/getnotification.php"
func GetNotificationHistory(completionHandler: @escaping (AnyObject?, NSError?) -> ()) {
getNotifiction(completionHandler: completionHandler)
}
func getNotifiction(completionHandler: @escaping (AnyObject?, NSError?) -> ()){
Alamofire.request(urlStringChecking,method: .get)
.responseJSON{ response in
switch response.result{
case .success:
let list = response.result.value;
completionHandler(list as AnyObject, nil)
case .failure( _):
print(response.result.error as Any)
}
}
}
let list = self.GetNotificationHistory(); // If I get success list here then my problem is solved but I am getting an error.
ERROR: Value of type '(NotificationHistory) -> () -> (NotificationHistory)' has no member 'list'
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return(list.count)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let reuseIdentifier = "NotificationCell"
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: reuseIdentifier)
}
cell?.textLabel?.numberOfLines = 0
cell!.textLabel?.text = list[indexPath.row]
cell!.detailTextLabel?.text = list[indexPath.row]
return cell!
}
override func viewDidLoad(){
super.viewDidLoad()
print("Notification History Loaded")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I am very new to Swift. Thanks
To handle json data use SwiftyJSON
install this third party library using cocoapods
First declare your list
as variable inside NotificationHistory
class as
var list = [[String: AnyObject]]()
Add this code to your completion handler getNotifiction
now your code look like this
func getNotifiction(completionHandler: @escaping (AnyObject?, NSError?) -> ()){
Alamofire.request(urlStringChecking,method: .get)
.responseJSON{ response in
switch response.result{
case .success:
if ((response.result.value) != nil){
let jsonData = JSON(response.result.value!)
if let mydata = jsonData["list"].arrayObject {
self.list = mydata as! [[String: AnyObject]]
}
self.tableView.reloadData()
completionHandler(self.list, nil)
}
case .failure( _):
print(response.result.error as Any)
}
}
}
And to call this function write self.GetNotificationHistory()
inside your viewDidLoad()
This time we have proper data in list
variable, now it's time to implement new code in tableview
No need to change here
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
Need some changes in your code
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let reuseIdentifier = "NotificationCell"
let newListValue = list[indexPath.row] // assign new values in diff variable
var cell:UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as UITableViewCell?
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: reuseIdentifier)
}
cell?.textLabel?.numberOfLines = 0
cell!.textLabel?.text = newListValue[indexPath.row] as? String
cell!.detailTextLabel?.text = newListValue[indexPath.row] as? String
return cell!
}
response.result.value
contain simple array to treat it as JSON we can either use it as
response.result.value as? [[String:String]]
or your can use SwiftyJSON
: D