             })
         }
     }
+    
+    func bookDelegate(book: Book, favoriteValueChanged newValue: Bool) {
+        if newValue {
+            dictionary[favoritesTag]?.append(book)
+        } else {
+            dictionary[favoritesTag] = dictionary[favoritesTag]?.filter({ $0 !== book })
+        }
+        
+        isLibraryChanged = true
+
+        let favoritesArray = dictionary[favoritesTag]!.map({ favoriteBook in
+            favoriteBook.proxyForComparasion
+        })
+        
+        let userDefaults = NSUserDefaults.standardUserDefaults()
+        userDefaults.setObject(favoritesArray, forKey: favoritesUserDefaultsKey)
+    }
 }
            }
            
            sort()
        }
    }
    


import Foundation

typealias BooksArray = [Book]

class Library: BookDelegate {
    let favoritesTag = "favorites"
    let favoritesUserDefaultsKey = "FavoriteBooks"
    
    var tags = [String]()
    var dictionary = [String: BooksArray]()
    var isLibraryChanged = false
    
    init (json: NSData) {
        if let jsonArray = try? JSONManager.loadFromData(json) {
            
            let userDefaults = NSUserDefaults.standardUserDefaults()
            let favoritesArray = userDefaults.arrayForKey(favoritesUserDefaultsKey) as? [String]
            
            dictionary[favoritesTag] = BooksArray()
            
            for dict:JSONDictionary in jsonArray {
                if let title = dict["title"] as? String,
