I am grabbing data from a url which is a PHP script that is JSON encoding my data from a MySQL database. My data is coming into my app and I know how to parse it but I am unsure how to place my data inside of each item's textField.stringvalue in my Collection View. Most of the information I have found on this subject is all for iOS and I am making an app for OS X.
ViewController.swift
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var collectionView: NSCollectionView!
var productCategories: [ProductCategory]?
let baseURL = "http://myURL"
override func viewDidLoad() {
super.viewDidLoad()
getJSON()
self.collectionView.reloadData()
}
func getJSON(){
let url = NSURL(string: baseURL)
let request = NSURLRequest(url: url! as URL)
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request as URLRequest) { (data,response,error) -> Void in
if error == nil {
let swiftyJSON = JSON(data:data!)
print(swiftyJSON)
let product = swiftyJSON[].arrayValue
for name in product{
let names = name["product_name"].stringValue
print(names)
}
} else{
print("Error")
}
}
task.resume()
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
extension ViewController: NSCollectionViewDataSource{
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = productCategories?.count {
return count
}
return 0
}
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: "ImageCollectionViewItem", for: indexPath)
item.textField?.stringValue = Product.product_name
return item
}
}
Model.swift
import Foundation
class ProductCategory: NSObject {
var name: String?
var productss: [Product]?
var type: String?
}
class Product: NSObject {
var id: NSNumber?
var product_name: String?
var product_price: NSNumber?
var product_description: String?
var product_image: String?
var product_download: String?
var product_video: String?
var product_featured: Int?
}
Related
I am working on a practice project with the CoinGecko API. I just figured out how to successfully fetch, parse, and display data from the API, but I have ran into an issue. Currently I am only fetching a single coin, using a single URL but I would like to be able to fetch multiple (3 in this case), all at once and display them. Not sure how to proceed, or if my explanation makes sense so I have attached my code below. Thanks in advance.
CoinListViewModel
import Foundation
class CoinListViewModel {
private(set) var coin: Coin
init(coin: Coin) {
self.coin = coin
}
func getCoins(url: URL) async {
do {
let coin = try await WebService().getCoins(url: url)
self.coin = coin
} catch {
print(error)
}
}
}
struct CoinViewModel {
private let coin: Coin
init(coin: Coin) {
self.coin = coin
}
var symbol: String {
coin.symbol
}
var name: String {
coin.name
}
var price: [String: Double] {
coin.marketData.currentPrice
}
}
WebService
import Foundation
enum CoinsError: Error {
case invalidServerResponse
}
class WebService {
func getCoins(url: URL) async throws -> Coin {
let (data, response) = try await URLSession.shared.data(from: url)
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
throw CoinsError.invalidServerResponse
}
return try JSONDecoder().decode(Coin.self, from: data)
}
}
Constants
import Foundation
struct Constants {
struct coinURLs {
static let allCoinURLs = [
URL(string: "https://api.coingecko.com/api/v3/coins/bitcoin")!,
URL(string: "https://api.coingecko.com/api/v3/coins/ethereum")!,
URL(string: "https://api.coingecko.com/api/v3/coins/litecoin")!
]
}
}
Coin
import Foundation
// MARK: - WelcomeElement
struct Coin: Codable {
let symbol, name: String
let marketData: MarketData
enum CodingKeys: String, CodingKey {
case symbol, name
case marketData = "market_data"
}
static var DefaultCoin = Coin(symbol: " ", name: " ", marketData: MarketData(currentPrice: ["usd": 0.0]))
}
struct Tion: Codable {
let en: String
enum CodingKeys: String, CodingKey {
case en
}
}
struct MarketData: Codable {
let currentPrice: [String: Double]
enum CodingKeys: String, CodingKey {
case currentPrice = "current_price"
}
}
CoinListViewController
import Foundation
import UIKit
class CoinListViewController: UITableViewController {
private let vm = CoinListViewModel(coin: Coin.DefaultCoin)
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
Task {
await getCoins()
}
}
private func configureUI() {
self.navigationController?.navigationBar.prefersLargeTitles = true
self.title = "CoinFlip"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "StockCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StockCell", for: indexPath)
let coin = vm.coin
var content = cell.defaultContentConfiguration()
content.text = coin.name
content.secondaryText = "$\(Int(coin.marketData.currentPrice["usd"] ?? 0))"
cell.contentConfiguration = content
return cell
}
private func getCoins() async {
await vm.getCoins(url: Constants.coinURLs.allCoinURLs[0])
print(vm.coin)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
You can spawn multiple network calls if you make a few changes to your WebService. This will also return a partial result if fetching a coin fails.
func getCoins(urls: [URL]) async throws -> [Coin] {
return try await withThrowingTaskGroup(of: Coin.self, body: { taskGroup in
var coins = [Coin]()
urls.forEach { url in
taskGroup.addTask {
let (data, response) = try await URLSession.shared.data(from: url)
guard
let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200
else { return Coin() }
return try JSONDecoder().decode(Coin.self, from: data)
}
}
for try await coin in taskGroup {
coins.append(coin)
}
return coins
})
}
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!
}
// 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.
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.
i am having trouble to reload my UICollectionview after i got my json data using TRON pod (it is like alamofire but in different structure i think)
and i parsed it using swiftyJSON
i am searching for the answer about three days and i dont know what i am missing
...
import UIKit
import SwiftyJSON
import TRON
class FeaturedAppViewController: UICollectionViewController ,
UICollectionViewDelegateFlowLayout {
private let cellID = "cellId"
var appCategories : [AppCategory]?
override func viewDidLoad() {
super.viewDidLoad()
fetchHomeApps()
collectionView?.backgroundColor = .white
collectionView?.register(CategoryCell.self, forCellWithReuseIdentifier: cellID)
}
class Home : JSONDecodable{
var apps : [App]
required init(json: JSON) throws {
print("now ready to parse :\n", json)
var apps = [App]()
let catJSON = json["categories"]
let array = json["categories"].array
for app in array!{
for index in 0...catJSON.count - 1 {
let name = app["apps"][index]["Name"].stringValue
let id = app["apps"][index]["Id"].intValue
let imageName = app["apps"][index]["ImageName"].stringValue
let category = app["apps"][index]["Category"].stringValue
let price = app["apps"][index]["Price"].doubleValue
let appsIdentification = App(iD: id, name: name, category: category, price: price, imageName: imageName)
apps.append(appsIdentification)
}
}
self.apps = apps
}
}
class JSONError : JSONDecodable {
required init(json: JSON) throws {
print("josn Error")
}
}
fileprivate func fetchHomeApps() {
print("123")
let request : APIRequest<AppCategory , JSONError> = tron.request("/appstore/featured")
request.perform(withSuccess: { (AppCategory) in
print("Successfully Fetched")
print(AppCategory.apps.count)
self.collectionview.reloaddata()
}) { (err) in
print("couldnt Fetch babe \n" , err)
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = appCategories?.count {
return count
}else{
return 0
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! CategoryCell
cell.appCategory = appCategories?[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width , height: 230)
}
let tron = TRON(baseURL: "http://www.statsallday.com")
}
this is my view controller and there is a models page
import UIKit
import SwiftyJSON
import TRON
class AppCategory : NSObject , JSONDecodable {
var name : String?
var type : String?
let Url = "http://www.statsallday.com/appstore/featured"
var apps : [App]
required init(json: JSON) throws {
print("now ready to parse :\n", json)
var apps = [App]()
let catJSON = json["categories"]
let array = json["categories"].array
for app in array!{
for index in 0...catJSON.count - 1 {
let name = app["apps"][index]["Name"].stringValue
let id = app["apps"][index]["Id"].intValue
let imageName = app["apps"][index]["ImageName"].stringValue
let category = app["apps"][index]["Category"].stringValue
let price = app["apps"][index]["Price"].doubleValue
let appsIdentification = App(iD: id, name: name, category: category, price: price, imageName: imageName)
apps.append(appsIdentification)
}
}
self.apps = apps
}
}
struct App {
let iD : Int
let name : String
let category : String
let price : Double
let imageName : String
}
i think i should add something in fetchHomeApps function but i dont know what...
actually i started programming since 34 days age and sorry if i my code is silly.
You should call self.yourCollectionView.reloadData() when you fetched data from API and parse with swiftyJson. Only when appCategories have data not empty array object.