My json data is like that and I'm using alamofire for loading data and objectmapper for mapping.I just try to parsing json data and show it on CollectionView.However Im getting error.
Here is my viewcontroller
import UIKit
import Alamofire
import ObjectMapper
import AlamofireObjectMapper
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return schedules?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = UICollectionViewCell()
cell.textLabel?.text = schedules?[indexPath.row].title
}
@IBOutlet weak var collectionViewData: UICollectionView!
var schedules: [Schedule]?
override func viewDidLoad() {
super.viewDidLoad()
collectionViewData.dataSource = self
collectionViewData.delegate = self
loadData()
}
func loadData() {
let jsonDataUrl = "https://jsonplaceholder.typicode.com/posts"
Alamofire.request(jsonDataUrl).responseJSON { response in
self.schedules = Mapper<Schedule>().mapArray(JSONObject: response.result.value)
self.collectionViewData.reloadData()
}
}
}
Here is my schedule file codes for mapping.
import Foundation
import ObjectMapper
import AlamofireObjectMapper
class Schedule: Mappable {
var userId: String
var id: String
var title: String
var body: String
required init?(map: Map) {
userId = ""
id = ""
title = ""
body = ""
}
func mapping(map: Map) {
userId <- map["userId"]
id <- map["id"]
title <- map["title"]
body <- map["body"]
}
}
When I try to run it I'm getting error like that: Value of type 'UICollectionViewCell' has no member 'textLabel'
I tried to add "textLabel" to viewcontroller but it didn't work.Should I add a new class for CollectionView?