I have appended JSON
into array and it shows blank when I print the array from viewdidload
or somewhere else.
I have created a custom function for fetching data and called it in the viewdidload
.
Following is my entire code:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var Usernames = NSMutableArray()
var emails = [String]()
let url_Posts = "https://jsonplaceholder.typicode.com/posts"
let url_Users = "https://jsonplaceholder.typicode.com/users"
override func viewDidLoad() {
super.viewDidLoad()
fetchJSONData()
print("usernames are",(Usernames))
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Usernames.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//TableView Codes
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PostsCell
cell.lblEmail.text = self.emails[indexPath.row]
cell.lblUsername.text = self.Usernames[indexPath.row] as! String
return cell
}
func fetchJSONData() {
Alamofire.request(url_Users).responseJSON {
(response) in
if let rawData = response.result.value as! [[String:Any]]? {
for one in rawData {
let name = one["name"]
self.Usernames.add(name!)
}
}
}
}
}