InterviewSolution
Saved Bookmarks
| 1. |
enum BookType { case biology case physics case maths case unknown } class Book { var name:String? var type:BookType = .unknown } class BookStorage { func fetchAllBooks() -> [Book] { // return all available books } } class BooksManager { func physicsBooks() -> [Book] { let books = BookStorage().fetchAllBooks() // identify the physics book and return those let physicsbooks … return physicsbooks } } |
|
Answer» It WOULD provide a compilation error as emp1 == emp2 cannot be RESOLVED by compiler. It depends if the structs/classes are ADAPTED to the Hashable protocol. The primitive data TYPE like Int, String by default implement the Hashable protocol, show they do not show any error if we compare instances of such data types. The corrected code would be: struct Employee: Hashable { var name:String var id:Int init(name:String, id:Int) { self.name = name self.id = id } var hashValue: Int { return self.id} } The printed result would be true. |
|