0

I have two ViewControllers. First is the MoviesVC which shows the lists of movies. The second is the BookmarkedVC which shows the movies which was bookmarked by user.

I have movies:[Movie] property in MoviesVC. Movie class has a bookmark = false flag to determine whether movie is bookmarked or not. When user click bookmark button on cell, I update flag to true.

Since BookmarkedVC is also showing the lists of bookmarked movie, I inherit BookmarkVC: MoviesVC.

In BookmarkedVC viewWillAppear() method, I fetch the bookmarked movies by calling the function below

func loadBookmarkedMovie() -> [Movie] {
    let bookmarkedMovies = self.movies.filter { return $0.bookmark == true }
    return bookmarkedMovies
}

The problem is bookmark of self.movies is still false although they were bookmarked.

To make sure, I call loadBookmarkedMovie() in MoviesVC after I had bookmarked some movies and it actually return the bookmarked movies but in BookmarkedVC it return nil

noob
  • 545
  • 6
  • 23
  • Have you confirmed that the bookmark property is actually getting changed in your MoviesVC when you interacts with the cell? – Rakshith Nandish Sep 26 '17 at 05:49
  • Can i assume that `movies` is initialized when it was declared ? – roy Sep 26 '17 at 05:52
  • yeah the property is changed. I check it by calling filter method on array in `viewWillAppear()` – noob Sep 26 '17 at 05:59
  • @Roy movies is declared as ? and download movies json from server. – noob Sep 26 '17 at 06:01
  • 1
    Have a look at: [Apple Developer Link](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Inheritance.html) – Ganpat Sep 26 '17 at 06:05
  • Are you sure it makes to have the `favourite` property reside in the `Movie` objects? For example, what if you eventually introduce multiple users? If a movie is marked favourite, who's favourite is it? – Alexander Sep 26 '17 at 15:01

1 Answers1

0

As a result of Inheritance you only can get properties ( functions and variables ) of Super Class .

So If you inherit MoviesVC . .

  1. Now BookmarkVC has movies:[Movie] (Lets assume movies is initialized when it was declared ) .
  2. All bookmark == false (By default false as you didn't update bookmark yet in BookmarkVC )

So . . func loadBookmarkedMovie() will definitely return nil . To pass data from MoviesVC to BookmarkedVC all you need to use Segue or Delegate .

roy
  • 6,685
  • 3
  • 26
  • 39