Guest User

Untitled

a guest
Jan 12th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. protocol NameType {
  2. var name: String { get }
  3. }
  4.  
  5. class UserShort: NameType {
  6.  
  7. var id: UInt
  8. var name: String
  9.  
  10. init(id: UInt, name: String) {
  11. self.id = id
  12. self.name = name
  13. }
  14.  
  15. }
  16.  
  17. final class UserLong: UserShort {
  18.  
  19. var status: String
  20. var isOnline: Bool
  21.  
  22. init(id: UInt, name: String, status: String, isOnline: Bool) {
  23. self.status = status
  24. self.isOnline = isOnline
  25. super.init(id: id, name: name)
  26. }
  27.  
  28. }
  29.  
  30. protocol UserProtocol {
  31. func sortedByName() -> [UserShort]
  32. }
  33.  
  34. final class UserCollection: UserProtocol {
  35.  
  36. let users: [UserShort]
  37.  
  38. init(users: [UserShort]) {
  39. self.users = users
  40. }
  41.  
  42. func sortedByName() -> [UserShort] {
  43. return users.sorted { $0.name > $1.name }
  44. }
  45.  
  46. }
  47.  
  48. let follower = UserShort(id: 1, name: "Medium Guest")
  49. let currentUser = UserLong(id: 999, name: "Maxim Vialykh", status: "Work on SOLID Arcticle", isOnline: true)
  50. let userCollection = UserCollection(users: [currentUser, follower])
  51. let sorted = userCollection.sortedByName()
  52.  
  53. for case let user as UserLong in sorted {
  54. print("UserLong: \(user.name)")
  55. }
Add Comment
Please, Sign In to add comment