[英]unable to get data from model class in swift 3?
Here is the data for model class in which I got data from all dictionaries but unable to get ratingVotes from model class in which I need to get value
key value pair to be used for displaying rating in my UI Can anyone help me how to resolve this ? 这是模型类的数据,在其中我从所有词典中获取了数据,但无法获得评级从模型类中获得的投票中,我需要获取value
键值对以用于在UI中显示评级,谁能帮我解决这个问题?
func ReviewApiDownloadJsonwithURL(reviewApi : String){
print(reviewApi)
let url = URL(string: reviewApi)!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil { print(error!); return }
do {
if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] {
for item in jsonObj {
self.reviewModel = Review.init(dict: item)
}
DispatchQueue.main.async {
guard let obj = self.reviewModel else { return }
let itemsCount = obj.reviews
for i in 0..<itemsCount.count {
let customAttribute = obj.reviews[i].ratingVotes
// for j in 0..<customAttribute.count {
self.ratingvalue.append(customAttribute[0].value!)
// }
}
print(self.ratingvalue)
self.reviewTableView.delegate = self
self.reviewTableView.dataSource = self
self.activityIndicator.stopAnimating()
self.activityIndicator.hidesWhenStopped = true
self.reviewTableView.reloadData()
self.initialCollectionData()
}
}
} catch {
print(error)
}
}
task.resume()
}
Here is my Json data 这是我的Json数据
Here is my model class code 这是我的模型类代码
struct Review {
let ratingPercent : Any?
let count : Any?
let reviews : [Reviews]
init(dict : [String:Any]) {
if let customAttribute = dict["reviews"] as? [[String: AnyObject]] {
var result = [Reviews]()
for obj in customAttribute {
result.append(Reviews(dict: (obj as? [String : Any])!))
}
self.reviews = result
} else {
self.reviews = [Reviews]()
}
self.ratingPercent = dict["avg_rating_percent"]
self.count = dict["count"]
}
}
struct Reviews {
let reviewId : Any?
let createdAt : Any?
let entityId : Any?
let entityValue : Any?
let statusId : Any?
let detailId : Any?
let title : String?
let detail : String?
let nickName : String?
let customerId : Any?
let entityCode : Any?
let ratingVotes : [RatingVotes]
init(dict : [String:Any]) {
if let customAttribute = dict["rating_votes"] as? [[String: Any]] {
var result = [RatingVotes]()
for obj in customAttribute {
result.append(RatingVotes(dict: (obj as? [String : Any])!))
}
self.ratingVotes = result
} else {
self.ratingVotes = [RatingVotes]()
}
self.reviewId = dict["review_id"]
self.createdAt = dict["created_at"]
self.entityId = dict["entity_id"]
self.entityValue = dict["entity_pk_value"]
self.statusId = dict["status_id"]
self.detailId = dict["detail_id"]
self.title = dict["title"] as? String
self.detail = dict["detail"] as? String
self.nickName = dict["nickname"] as? String
self.customerId = dict["customer_id"]
self.entityCode = dict["entity_code"]
}
}
struct RatingVotes {
let voteId : Int?
let optionId : Int?
let remoteIp : Any?
let remoteIpLong : Int?
let customerId : Any?
let entityPkValue : Any?
let ratingId : Int?
let reviewId : Int?
let percent : Int?
let value: Int?
let ratingCode : String?
let storeId : Int?
let code : Int?
let postion : Int?
init(dict : [String:Any]) {
self.voteId = dict["vote_id"] as? Int
self.optionId = dict["option_id"] as? Int
self.remoteIp = dict["remote_ip"]
self.remoteIpLong = dict["remote_ip_long"] as? Int
self.customerId = dict["customer_id"]
self.entityPkValue = dict["entity_pk_value"]
self.ratingId = dict["rating_id"] as? Int
self.reviewId = dict["review_id"] as? Int
self.percent = dict["percent"] as? Int
self.value = dict["value"] as? Int
self.ratingCode = dict["rating_code"] as? String
self.storeId = dict["store_id"] as? Int
self.code = dict["code"] as? Int
self.postion = dict["position"] as? Int
}
}
It is just Typo mistake you might copy paste your code. 您可能会复制粘贴代码,这只是Typo错误。 You can easily find it by debug here is a small mistake you made 您可以通过调试轻松找到它,这是您犯的一个小错误
Observe here in struct Reviews
在struct Reviews
观察
if let customAttribute = dict["reviews"] as? [[String: Any]] {
var result = [RatingVotes]()
for obj in customAttribute {
result.append(RatingVotes(dict: (obj as? [String : Any])!))
}
self.ratingVotes = result
} else {
self.ratingVotes = [RatingVotes]()
}
You are using key reviews
here dict["reviews"]
您在此处使用关键reviews
dict["reviews"]
it should be rating_votes
like dict["rating_votes"]
应该像dict["rating_votes"]
一样为rating_votes
Hope it is helpful to you 希望对您有帮助
You have getting both time review dictionary same value. 您将两个时间查看字典的值都设为相同。 if let customAttribute = dict["reviews"] as? 如果让customAttribute = dict [“ reviews”]为? [[String: Any] So change here 'review' to 'rating_votes' [[String:Any]所以在这里将'review'更改为'rating_votes'
Or follow below code may it help you!! 或者按照下面的代码可能会帮助您!
init(dict : [String:Any]) {
if let customAttribute = dict["rating_votes"] as? [[String: Any]] {
var result = [RatingVotes]()
for obj in customAttribute {
result.append(RatingVotes(dict: (obj as? [String : Any])!))
}
self.ratingVotes = result
} else {
self.ratingVotes = [RatingVotes]()
}
self.reviewId = dict["review_id"]
self.createdAt = dict["created_at"]
self.entityId = dict["entity_id"]
self.entityValue = dict["entity_pk_value"]
self.statusId = dict["status_id"]
self.detailId = dict["detail_id"]
self.title = dict["title"] as? String
self.detail = dict["detail"] as? String
self.nickName = dict["nickname"] as? String
self.customerId = dict["customer_id"]
self.entityCode = dict["entity_code"]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.