2017-11-04 74 views
0

這裏是模型類的數據,其中我從所有字典中獲取數據,但無法從模型類中獲取ratingVotes,我需要獲取value鍵值對以用於在UI中顯示評分任何人都可以幫助我如何解決這個?無法在swift 3中從模型類獲取數據?

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() 
    } 

這裏是我的JSON數據

enter image description here

這裏是我的模型類代碼

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 

     } 
} 
+0

一切工作正常,除了'ratingVotes'。對? – trungduc

+0

是剩餘的數據我得到了@trungduc – user0246

回答

0

這只是錯字錯誤,你可能會複製粘貼代碼。您可以輕鬆地通過調試發現這裏是你做

觀察這裏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]() 
    } 

您使用此dict["reviews"]

關鍵reviews它應該是一個小錯誤rating_votesdict["rating_votes"]

希望對你有所幫助

+0

https://i.stack.imgur.com/Gs368.png但是在這裏崩潰 – user0246

+0

@ user0246您的回覆您在'ratting_votes'字典中使用'self'時有字符串值。 value = dict [「value」] as? Int'將永遠失敗。請在開始實際執行之前正確地注意響應 –

+0

只有這個問題我用字符串替換它然後工作正常 – user0246

0

你已經得到了tim e審查字典相同的價值。 如果讓customAttribute = dict [「reviews」] as? [[String:Any]因此,請將此處的'review'更改爲'rating_votes'

或者按照下面的代碼,它可以幫助您!

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"] 
} 
}