2016-10-21 17 views
1

AlamoFireObjectMapperを使用して、単純なJSONをいくつかのクラスモデルにマップしています。私は正しくJSONを取得して印刷することができたようですが、私のクラス型の配列に自分の応答を追加しようとすると動作しません。AlamoFireObjectMapperはオブジェクト配列内のデータレスポンスを保存できません

サンプルJSON: SampleJson

class MovieResults: Mappable { 

var movieResults: [MovieInfo]? 

required init?(map: Map) { 
    mapping(map: map) 
    } 

func mapping(map: Map) { 
    // An array of objects 
    self.movieResults <- map["results"] 
    } 
} 

class MovieInfo: Mappable { 

var movieTitle: String? 
var movieID: Int? 
var movieRating: String? 
var movieReleaseDate: String? 
var inTheaters: Bool? 

var movieResults: [MovieInfo]? 


required init?(map: Map) { 

} 

func mapping(map: Map) { 
    self.movieTitle <- map["title"] 
    self.movieID <- map["id"] 
    self.movieRating <- map["rating"] 
    self.movieReleaseDate <- map["release_date"] 
    self.inTheaters <- map["in_theaters"] 
    } 
} 

のViewController:

class resultViewController: UITableViewController { 

var searchTerm: String? 
var movieArray = [MovieInfo]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    fetchAPI(search: searchTerm!) 
} 

func fetchAPI(search: String) { 
    let gboxAPI = "rK5M0dCTUd268hk121BpNpEAxMOmFuNh" 
    let URL = "http://api-public.guidebox.com/v1.43/US/\(gboxAPI)/search/movie/title/\(searchTerm!)/fuzzy" 


    Alamofire.request(URL).responseObject { (response: DataResponse<MovieResults>) in 

     let result = response.result.value 

     if let movies = result?.movieResults { 
      for item in movies { 
       self.movieArray.append(item) //this doesn't work 
       print(item.movieTitle) //this works 
      } 
     } 
    } 

    /* This doesn't print anything 
     for some reason my array isn't truly being 
     popluated in the above fetchAPI call 
    */ 
    for item in movieArray { 
     print(item.movieTitle) 
    } 

} 
override func tableView(_ tablView: UITableView, 
         numberOfRowsInSection section: Int) -> Int { 
    return movieArray.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = UITableViewCell(style: .value1, reuseIdentifier: "UITableViewCell") 

    let movieItem = movieArray[indexPath.row] 

    cell.textLabel?.text = movieItem.movieTitle 
    cell.detailTextLabel?.text = movieItem.movieRating 

    return cell 

    } 
} 

私は私のデータを保存、印刷することはできませんが、なぜ私は私の人生のために把握することはできません。それだけで私はクリックしません。

答えて

0

Mappableクラスのオブジェクトを作成して、データを追加することができない場合があります。むしろ、別のモデルクラスを作成してそのオブジェクトを使用することができます。

class movieModel: NSObject { 
    var movieTitle: String? 
    var movieID: Int? 
    var movieRating: String? 
    var movieReleaseDate: String? 
    var inTheaters: Bool? 
} 

resultViewControllerにこのクラスのオブジェクトを作成し、それを使用してムービーの詳細を保存します。

class resultViewController: UITableViewController { 
    let movieArray: [movieModel]? //create array of movieModel object 
    ....... 
     ........ 
     if let movies = result?.movieResults { 
      for item in movies { 
       let movie = movieModel()//create object of movieModel 
       movie.movieTitle = item.movieTitle 
       movie.movieID = item.movieID 
       movie.movieRating = item.movieRating 
       movie.movieReleaseDate = item.movieReleaseDate 
       movie.inTheaters = item.inTheaters 

       self.movieArray.append(self.movie) 
       print(item.movieTitle) 
      } 
     } 
    ........ 
} 
+0

let movieArray:[movieModel]?配列を作成せずにmovieModelのプロパティを作成します。[movieModel]にも 'append'のメンバーはなく、 'let'の定数として定義されているので動作しません。助けてくれてありがとう、私は検索を続けます。 – Wrenzoe

0

要求は非同期です。ブロックは、サーバーから応答を受け取るまで実行されません。
for-loopは、要求を実行した直後に実行されます。新しいオブジェクトを追加した後でself.tableView.reloadData()を実行すると、それはうまくいくはずです。

関連する問題