不符合协议 'Decodable' 和 'Encodable'
does not conform to protocol 'Decodable' and 'Encodable'
嗨程序员实际上当我将我的json格式化为可编码时我遇到了这个错误“类型'HomeViewControler'不符合协议'Decodable'和'Encodable'”我看到了类似的问题但我没有结果,很高兴看到你的帮助谢谢
这是我的代码:
class HomeViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var txt : String = " "
var cid : String = " "
override func viewDidLoad() {
super.viewDidLoad()
print("\n iiiiiiiiiid is \(cid)")
collectionView.dataSource = self
}
struct welcomeJson: Codable { // this is where i have this error
let userId : UserID
let isoGld , check : Bool
let image : [String]
let date, id , title,countryid,about , cityid, telephone, countryText,cityText, category, address, dateOfExpire , dateOfStart , offer , createdAt, updateAt,v : String
enum CodingKeys : String , CodingKey {
case userId
case isoGld
case image
case check
case date
case id = "_id"
case title
case countryID = "country"
case cityID = "city"
case telephone
case countryText
case cityText
case category
case address
case dateOfExpire = "dateOfEx"
case dateOfStart
case offer
case about
case createdAt
case updateAt
case v = "__v"
}
}
struct UserID : Codable {
let ID, name, userName , email, phone : String
enum CodingKeys : String , CodingKey {
case ID = "Id"
case name
case userName
case email
case phone
}
}
struct Data: Codable {
var data : [welcomeJson]
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OfferCollectionViewCell", for: indexPath)
return cell
}
func fetchOffer() {
if let url = URL(string: "http://5.63.13.16:8080/api/product/search/getByquerys?page=1&limit=10&city=\(cid)") {
URLSession.shared.dataTask(with: url) {data,response,error in
if let data = data {
let jsondec = JSONDecoder()
do {
let parsedJS = try jsondec.decode(Data.self, from: data)
for element in parsedJS.data {
print("adresssssetooo begooo : \(element.address) ")
}
} catch {
print(error)
}
}
}
}
而且我还在评论中显示了我遇到此错误的地方
是,countryid,about , cityid,
所以
case countryID = "country"
case cityID = "city"
应替换为
case countryid = "country"
case cityid = "city"
嗨程序员实际上当我将我的json格式化为可编码时我遇到了这个错误“类型'HomeViewControler'不符合协议'Decodable'和'Encodable'”我看到了类似的问题但我没有结果,很高兴看到你的帮助谢谢
这是我的代码:
class HomeViewController: UIViewController, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var txt : String = " "
var cid : String = " "
override func viewDidLoad() {
super.viewDidLoad()
print("\n iiiiiiiiiid is \(cid)")
collectionView.dataSource = self
}
struct welcomeJson: Codable { // this is where i have this error
let userId : UserID
let isoGld , check : Bool
let image : [String]
let date, id , title,countryid,about , cityid, telephone, countryText,cityText, category, address, dateOfExpire , dateOfStart , offer , createdAt, updateAt,v : String
enum CodingKeys : String , CodingKey {
case userId
case isoGld
case image
case check
case date
case id = "_id"
case title
case countryID = "country"
case cityID = "city"
case telephone
case countryText
case cityText
case category
case address
case dateOfExpire = "dateOfEx"
case dateOfStart
case offer
case about
case createdAt
case updateAt
case v = "__v"
}
}
struct UserID : Codable {
let ID, name, userName , email, phone : String
enum CodingKeys : String , CodingKey {
case ID = "Id"
case name
case userName
case email
case phone
}
}
struct Data: Codable {
var data : [welcomeJson]
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OfferCollectionViewCell", for: indexPath)
return cell
}
func fetchOffer() {
if let url = URL(string: "http://5.63.13.16:8080/api/product/search/getByquerys?page=1&limit=10&city=\(cid)") {
URLSession.shared.dataTask(with: url) {data,response,error in
if let data = data {
let jsondec = JSONDecoder()
do {
let parsedJS = try jsondec.decode(Data.self, from: data)
for element in parsedJS.data {
print("adresssssetooo begooo : \(element.address) ")
}
} catch {
print(error)
}
}
}
}
而且我还在评论中显示了我遇到此错误的地方
是,countryid,about , cityid,
所以
case countryID = "country"
case cityID = "city"
应替换为
case countryid = "country"
case cityid = "city"