How to display JSON into UILabel using UIPickerView in Swift 3.0? - json

I have these JSON output using PHP.
[
{"number":"001","name":"MIKE"},
{"number":"002","name":"JOSH"}
]
In Swift, I managed to select "name" value and display it into UIPickerView like below.
DropdownJSON.swift
import UIKit
class DropdownJSON: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet var dropdownTxt: UITextField!
#IBOutlet var dropdownPV: UIPickerView!
#IBOutlet var numberLbl: UILabel!
#IBOutlet var nameLbl: UILabel!
var persons = [Person]()
struct Person {
var number:String
var name: String
init?(dict: [String:Any]) {
guard let number = dict["number"] as? String, let name = dict["name"] as? String else {
return nil
}
self.number = number
self.name = name
}
}
override func viewDidLoad() {
super.viewDidLoad()
getDropdownJSON()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.persons.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return persons[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.numberLbl.text = self.persons[row].number
self.nameLbl.text = self.persons[row].name
self.dropdownPV.isHidden = true
self.dropdownTxt.resignFirstResponder()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.dropdownTxt{
self.dropdownPV.isHidden = false
}
}
func getDropdownJSON() {
let url = URL(string: "http://localhost/DropdownJSON.json")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
guard let data = data, error != nil else {
print(error?.localizedDescription)
return
}
if let array = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [[String:Any]] {
self.persons = array.flatMap(Person.init)
DispatchQueue.main.async {
self.dropdownPV.reloadAllComponents()
}
}
}
task.resume()
}
}
But my goal is to display JSON output "number" into numberLbl and "name" into nameLbl in function pickerView (didSelectRow).
The reason is I want to post the numberLbl only later on. nameLbl is just to display on the screen page.
Is it possible ? Appreciate if someone can help on this matters.
UPDATE
Thanks.

First of all you need to use URLSession.dataTask to get response from URL instead of NSData(contentsOf:). Also in Swift 3 use native URL and Data instead of NSURL and NSData.
Now the problem is you are not storing the number value form dictionary. The simplest way to manage this situation is to create struct and store both number and name value with it. After that create Array of that struct instead of Array of AnyObject.
struct Person {
var number:String
var name: String
init?(dict: [String:Any]) {
guard let number = dict["number"] as? String, let name = dict["name"] as? String else {
return nil
}
self.number = number
self.name = name
}
}
Now declare one array of struct Person and with your getDropdownJSON method add data with in it.
var persons = [Person]()
func getDropdownJSON() {
let url = URL(string: "http://localhost/getDropdownJSON.json")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
guard let data = data, error == nil else {
print(error?.localizedDescription)
return
}
if let array = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [[String:Any]] {
self.persons = array.flatMap(Person.init)
DispatchQueue.main.async {
dropdownPV.reloadAllComponents()
}
}
}
task.resume()
}
Now in PickerViewDelegate method use this array to fill its component.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.persons.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return persons[row].name
}
Now in didSelectRow you need to simply access array object and you will be get the number and name both.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row >= self.persons.count {
return
}
self.numberLbl.text = self.persons[row].number
self.nameLbl.text = self.persons[row].name
self.dropdownPV.isHidden = true
self.dropdownTxt.resignFirstResponder()
}

Related

How to decode custom type inside dictionary value with JSON?

my JSON:
https://www.cbr-xml-daily.ru/daily_json.js
my code:
struct CoinData: Decodable {
let Valute: [String: CoinInfo]
}
struct CoinInfo: Decodable {
let Name: String
let Value: Double
}
if let safeData = data {
if let coinData = self.parseJSON(safeData) {
print(coinData)
}
}
func parseJSON(_ data: Data) -> [String: CoinInfo]? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(CoinData.self, from: data)
return decodedData.Valute
} catch {
delegate?.didFailWithError(error: error)
return nil
}
}
In debug console following gets printed:
["PLN": CurrencyConverter.CoinInfo(Name: "X", Value: 19.6678), ...]
This way I can't reach Name and Value properties of a coin. What's wrong?
I am going to do for-loop to check if a key contains certain symbols. If it does - I will need to be able to access to both Name and Value
You don't actually need a for loop. Since coinData is a dictionary, you can use its subscript, together with optional binding to do this. For example, to check if the key "PLN" exists, and access its name and value:
if let coinInfo = coinData["PLN"] {
print(coinInfo.Name)
print(coinInfo.Value)
} else {
// "PLN" does not exist
}
StoyBoard
Code
import UIKit
import Alamofire
// MARK: - CoinData
struct CoinData: Codable {
let date, previousDate: String
let previousURL: String
let timestamp: String
let valute: [String: Valute]
enum CodingKeys: String, CodingKey {
case date = "Date"
case previousDate = "PreviousDate"
case previousURL = "PreviousURL"
case timestamp = "Timestamp"
case valute = "Valute"
}
}
// MARK: - Valute
struct Valute: Codable {
let id, numCode, charCode: String
let nominal: Int
let name: String
let value, previous: Double
enum CodingKeys: String, CodingKey {
case id = "ID"
case numCode = "NumCode"
case charCode = "CharCode"
case nominal = "Nominal"
case name = "Name"
case value = "Value"
case previous = "Previous"
}
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
var getCoinData = [CoinData]()
var coinNameArr = [String]()
var coinDataArr = [Valute]()
#IBOutlet weak var tblDataList: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getData()
}
func getData()
{
let url = "https://www.cbr-xml-daily.ru/daily_json.js"
AF.request(url, method: .get, encoding: URLEncoding.default).responseJSON { response in
let json = response.data
do{
let decoder = JSONDecoder()
self.getCoinData = [try decoder.decode(CoinData.self, from: json!)]
let response = self.getCoinData[0]
if response.valute.count != 0 {
self.coinNameArr.removeAll()
self.coinDataArr.removeAll()
for (coinName, coinData) in response.valute {
self.coinNameArr.append(coinName)
self.coinDataArr.append(coinData)
}
self.tblDataList.reloadData()
} else {
}
}catch let err{
print(err)
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return coinDataArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:coinTblCell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath as IndexPath) as! coinTblCell
cell.accessoryType = .disclosureIndicator
cell.tintColor = .black
let rowData = coinDataArr[indexPath.row]
cell.lblName.text = rowData.name
cell.lblValue.text = String(rowData.value)
return cell
}
}
class coinTblCell: UITableViewCell {
#IBOutlet weak var lblName: UILabel!
#IBOutlet weak var lblValue: UILabel!
}

swift json help in displaying data on table view controller

// my url
// https://fetch-hiring.s3.amazonaws.com/hiring.json
/*
my json
[
{"id": 383, "listId": 4, "name": ""},
{"id": 472, "listId": 1, "name": ""},
{"id": 625, "listId": 2, "name": null}
]
*/
// my main vc class for table view controller
import UIKit
class HeadlLinesTableViewController: UITableViewController {
var parse = [HiringElement]()
override func viewDidLoad() {
// Do any additional setup after loading the view.
super.viewDidLoad()
// Do any additional setup after loading the view.
let urlString = "https://fetch-hiring.s3.amazonaws.com/hiring.json"
guard let url = URL(string: urlString) else { return }
// 2
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
// 3
//Decode data
self.Elements = try? JSONDecoder().decode(HiringElement.self, from: data)
print(data)
// 4
//Get back to the main queue
DispatchQueue.main.async {
self.tableView.reloadData()
}
// 5
}.resume() // fires of request
}
My model struct for my api this is something I used from quickTypeIo api generator
struct HiringElement: Codable {
let id, listID: Int
let name: String?
enum CodingKeys: String, CodingKey {
case id
case listID
case name
}
} typealias Hiring = [HiringElement]
And my table view controller method here I can't display data on and some some errors. I am using tableview controller so doesn't need tableview delegate or datasource
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
guard let articles = Elements else { return 0 }
return return parse.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as? newsTableViewCell else {
fatalError(" cell not found ") }
// here I have errors thanks
cell.titleLabel.text = parse[indexPath.row].name
print(cell.titleLabel.text)
return cell
}
}
Here is my table view cell class
import UIKit
class newsTableViewCell: UITableViewCell {
//var article:Article!
#IBOutlet weak var avator: UIImageView!
#IBOutlet weak var titleLabel: UILabel!
#IBOutlet weak var newsLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
I think you should work on the decoding part. Here is a solution:
struct HiringElement: Decodable {
let id, listId: Int
let name: String?
}
#propertyWrapper
struct IgnoreFailure<Value: Decodable>: Decodable {
var wrappedValue: [Value] = []
private struct _None: Decodable {}
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
if let decoded = try? container.decode(Value.self) {
wrappedValue.append(decoded)
}
else {
try? container.decode(_None.self)
}
}
}
}
Then write the following code in your HeadlLinesTableViewController.swift.
typealias ArrayIgnoringFailure<Value: Decodable> = IgnoreFailure<Value>
Then try decoding as:
guard let objects = try? JSONDecoder().decode(ArrayIgnoringFailure<HiringElement>.self, from: data) else { return }
self.elements = objects.wrappedValue
Hope it will solve your problems.

pickerView fails loading JSON data that fetch from Alamofire 4.8.2 using SwiftyJSON in Swift 5 code

I am calling API to fill the data in UIPickerView using Swift 5 and Alamofire 4.8.2.
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var my_array = [[String : AnyObject]]()
#IBOutlet weak var pickerArray: UIPickerView!
#IBOutlet weak var labelName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string:"https://restcountries.eu/rest/v2/all") else {
return
}
Alamofire.request(url, method: .get, encoding: JSONEncoding.default, headers: nil).responseJSON { (resData) -> Void in
let swiftyJsonVar = JSON(resData.result.value!)
print("Full Output", swiftyJsonVar)
let finalData = swiftyJsonVar[0]["callingCodes"].arrayValue
print("final data", finalData)
let finalData2 = swiftyJsonVar[0]["borders"].arrayValue
print("single dimensional array output: ", finalData2)
let sampData = JSON(resData.result.value!)
self.my_array = finalData2 as [AnyObject] as! [[String : AnyObject]]
self.pickerArray.reloadAllComponents()
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return my_array.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return my_array[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
labelName.text = "You Selected : " + my_array[row]
}
}
Finally, I need that
* "finalData2" result into my picker view
* also any data I need to take from that nested JSON data and pass to picker view

How to load all data into picker view

How to load all data into picker view, now I can load first data only
this is my data from JSON​ response
**jsonResponse ==> Optional([["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118], ["PlanDateFullFormat": 20190119, "PlanDate": 19/01/2019]])
jsonArray ==>[["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118], ["PlanDateFullFormat": 20190119, "PlanDate": 19/01/2019]]
jsonDictionary ==>["PlanDate": 18/01/2019, "PlanDateFullFormat": 20190118]
planDate ==> 18/01/2019. ==> I want load all plant date into picker view
Loop json ==> (key: "PlanDateFullFormat", value: 20190118)
Loop json ==> (key: "PlanDate", value: 18/01/2019)**
I cannot load all data into picker view
func getPlanDatetoPickerview(ptruckID: String)-> Void {
.....
//check data shipment for json Dictionary
let planDate: String = jsonDictionary["PlanDate"] as! String
print("planDate ==> \(planDate)")
//show on pickerView
for myplanDate in jsonDictionary{
print("Loop json ==> \(myplanDate)")
}//for
self.getpPlandate = [jsonDictionary["PlanDate"] as! String]
.....
}catch let myerror{
print(myerror)
// check display plandate in database
....
}//DispatchQueue
}//catch
}//task
task.resume()
}//getPlanDatetoPickerview
I'm assuming your pickerView is set up properly but you are only seeing one row? If that's the case that's probably because of this line of code:
//change array to dictionary
guard let jsonDictionary:Dictionary = jsonArray.first else{
return
}//guard
print("jsonDictionary ==>\(jsonDictionary)")
You are only getting the first element of your array.
What I would do instead is just use jsonResponse directly like this:
var planDates = [Any]()
if let jsonResponse = jsonResponse {
for dictionary in jsonResponse {
planDates.append(dictionary["PlanDate"])
}
}
Then you can use planDates to populate your pickerView.
Or maybe, you are trying to load a pickerView with data from a dictionary?
First, your ViewController has to subclass UIPickerViewDataSource, UIPickerViewDelegate.
Then in ViewDidLoad, set your ViewController as the delegate/datasource for your UIPickerView:
repeatPicker.delegate = self
repeatPicker.dataSource = self
Then implement your Delegate/Datasource methods:
// The number of components of your UIPickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// Return number of planDate entries
}
// The data to return for the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
}
Actually its pretty easy to load data to a picker view. It's like UITableView.
class YourViewController {
var yourArray: [YourObject] = []
var yourPicker = UIPicker()
override func viewDidLoad() {
super.viewDidLoad()
yourPicker.dataSource = self
yourPicker.delegate = self
}
}
// MARK: - UIPickerViewDataSource
extension YourViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return yourArray.count
}
}
// MARK: - UIPickerViewDelegate
extension YourViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return yourArray[row].title
}
}

My object array is nil while my data are correct

I try to display my data in a tableView using no framework to parse my data, but when I add my data to my table and debug it, it is nil at the output while my data I retrieve are well parses, have I forgotten something to do?
I use a structure for my parameters as this :
enum Types {
case School
case Hospital
case Station_Essence
case Restaurant
}
struct Adresse {
public var title: String
public var details: String?
public var type: Types
public var coordinate: [String: Any]
}
and in my ViewController, i proced as this :
class ListMapViewController: UIViewController {
#IBOutlet var TitleTableView: UITableView!
#IBOutlet var MapView: MKMapView!
var adresse: [Adresse]?
override func viewDidLoad() {
super.viewDidLoad()
self.TitleTableView.register(UINib(nibName: "ListMapTableViewCell", bundle: nil), forCellReuseIdentifier: "Adresse")
self.TitleTableView.delegate = self
self.TitleTableView.dataSource = self
guard let POI = URL(string: "https://moc4a-poi.herokuapp.com/") else {
return
}
let task = URLSession.shared.dataTask(with: POI) { (data, response, error) in
guard let dataResponse = data else { return }
if let json = try! JSONSerialization.jsonObject(with: dataResponse, options:[]) as? [[String: Any]] {
for data in json {
let title = data["title"] as! String
let details = data["details"] as? String
guard let type = data["type"] as? Int else { return }
let valueType = self.valueType(dataType: type)
guard let coordinates = data["coordinates"] as? [String: Any] else { return }
self.adresse?.append(Adresse(title: title, details: details, type: valueType, coordinate: coordinates))
}
}
print(self.adresse)
}
self.TitleTableView.reloadData()
task.resume()
}
private func valueType(dataType: Int) -> Types {
if(dataType == 1) {
return Types.School
} else if (dataType == 2) {
return Types.Hospital
} else if (dataType == 3) {
return Types.Station_Essence
} else {
return Types.Restaurant
}
}
}
extension ListMapViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.adresse?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Adresse", for: indexPath) as! ListMapTableViewCell
if let adresse = self.adresse?[indexPath.row] {
cell.draw(adresse: adresse)
}
return cell
}
}
extension ListMapViewController: UITableViewDelegate {
}
You have two big problems.
self.adresse is nil. You never assign it a value. So all of the self.adresse?... do nothing.
You call reloadData too soon. It needs to be done inside the completion block, after you update the data. And it needs to be on the main queue.
To fix #1, change var adresse: [Adresse]? to var adresse = [Adresse](). Then you can get rid of all the ? after uses of adresse.
To fix #2, add:
DispatchQueue.main.async {
self.TitleTableView.reloadData()
}
just after the print at the end of the completion block. Don't forget to remove the current call to reloadData.