SLN SLN - 6 months ago 21
Swift Question

How to create a shared part between two object

List
and
User
are 2 classes.
List
contains title and a set of movies someone may likes. The class
User
has a
AddList(_: List)
method. It add new movie list to object of
User
type

class List {
var title : String = "General"
var movies = [String]()

init(title: String){
self.title = title
}

init(title: String, movies: [String]){
self.title = title
self.movies = movies
}
}

class User {
private let userID: Int
let userName: String
private var movieList = [String: [String]]() // the list is a Dictionaly, the value of the dictionalry is a string array

init(userID: Int, userName: String){
self.userID = userID
self.userName = userName
}

func AddList(newMovieList: List) -> Dictionary<String, [String]> {
self.movieList.updateValue(newMovieList.movies, forKey: newMovieList.title)
return self.movieList
}

}


3 movie list had beend inilizied as follow

var actionMovieJhon = List(title: "Action", movies: ["The Equalizer", "Mad Max: Fury Road", "Star Wars"])
var actionMovieAnna = List(title: "Action", movies: ["Batman v Superman: Dawn of Justice", "American Sniper", "The Martia"])
var shareMovie = List(title: "ShareAction", movies: ["MovieA", "MovieB", "MovieC"])


And then 2
User
objdects
Jhon
and
Anna
with ther own movie list was created

var Jhon = User(userID: 1001, userName: "Jhon")
var Anna = User(userID: 1002, userName: "Anna")

Jhon.AddList(actionMovieJhon)
Anna.AddList(actionMovieAnna)


Question: How can I add the shared movie list (
sharMovie
) into the dictionalry of
Jhon
and
Anna
, so that anything changed in the shared list of
Jhon
will get updated in the side of
Anna
and vice versa.

In anther word, the "dictionary" of
Jhon
and
Anna
has their own part and shared part. But the shared part is referenced to the same list object

More Question: I had feelings that the method
self.movieList.updateValue
is not
update by reference


Thanks a lot for your help

SLN SLN
Answer

The code is based on the sorece code provided by "Swift Apprentice, Beginning programming with Swift 2.2", chapter 14: Classes

import Foundation

class List {
    let name: String
    var movies: [String] = []

    init(name: String){
        self.name = name
    }

    func printList() {
        print("Movie List: \(name)")
        for movie in movies{
            print(movie)
        }
        print("\n")
    }
}


class User {
    var lists: [String: List] = [:]//****

    func addList(list: List){
        lists[list.name] = list
    }

    func listForName(name: String)-> List? {
        print(lists[name]?.movies)
        return lists[name]
    }

}

//create user
let jane = User()
let john = User()
let people = User()

//create list
var johnActionMovie = List(name: "ActionJhon")
var janeActionMovie = List(name: "ActionJane")
var sharedMovie = List(name: "SharedMovie")
var publicIntrest = sharedMovie

//call addList(_:) method on user object
jane.addList(janeActionMovie)
john.addList(johnActionMovie)

jane.addList(sharedMovie)
john.addList(sharedMovie)
people.addList(publicIntrest)


//private part appending
jane.lists["ActionJane"]?.movies.append("janeA")
jane.lists["ActionJane"]?.movies.append("janeB")
jane.lists["ActionJane"]?.movies.append("janeC")


john.lists["ActionJhon"]?.movies.append("johnA")
john.lists["ActionJhon"]?.movies.append("johnB")
john.lists["ActionJhon"]?.movies.append("johnB")

//Shared part appending
john.lists["SharedMovie"]?.movies.append("shareA")
jane.lists["SharedMovie"]?.movies.append("shareB")
people.lists["SharedMovie"]?.movies.append("shareC")


//print out the result
john.listForName("ActionJhon")
john.listForName("SharedMovie")

jane.listForName("ActionJane")
jane.listForName("SharedMovie")

people.listForName("SharedMovie")


//further check
print(jane.lists["ShareMovie"] === people.lists["ShareMovie"])
print(jane.lists["ActionJane"] === john.lists["ActionList"])