2017-11-04 21 views
0

ここでは、私はすべての辞書からデータを得たが、その中でモデルクラス用のデータである、誰もがどのように私を助けることができます私は私のUIでの評価を表示するために使用されるようにvalueキーと値のペアを取得する必要のあるモデルクラスからratingVotesを取得できませんこれを解決する?スイフト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

はい残りのデータIは@trungduc – user0246

答えて

0

は、それはあなたのコードをコピー&ペースト可能性があるだけでタイプミスの間違いです。あなたがここで重要reviewsdict["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]() 
    } 

ここに守っを作った小さなミスであることがrating_votes

dict["rating_votes"]ようにする必要があり、ここでのデバッグで、それを簡単にあなたを見つけることができますあなたに役立つことを願っています

+0

https://i.stack.imgur.com/Gs368.pngを持っていますが、 '自己を使用している間は、' ratting_votes'辞書内の値に文字列を持っているuser0246あなたの応答@ここ – user0246

+0

墜落してしまいました。 value = dict ["value"] as? Int'は常に失敗します。実際の実装を開始する前に応答を正しく観察してください –

+0

これだけで問題があります。 – user0246

0

eレビュー辞書と同じ値。 let customAttribute = dict ["reviews"] as? [[文字列:任意]だから、それはあなたを助けるかもしれない「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"] 
} 
} 
関連する問題