Last active
January 5, 2018 12:21
-
-
Save vialyx/ccff9fc4a09ae3f02a29eac8acac2b92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol NameType { | |
var name: String { get } | |
} | |
class UserShort: NameType { | |
var id: UInt | |
var name: String | |
init(id: UInt, name: String) { | |
self.id = id | |
self.name = name | |
} | |
} | |
final class UserLong: UserShort { | |
var status: String | |
var isOnline: Bool | |
init(id: UInt, name: String, status: String, isOnline: Bool) { | |
self.status = status | |
self.isOnline = isOnline | |
super.init(id: id, name: name) | |
} | |
} | |
protocol UserProtocol { | |
func sortedByName() -> [UserShort] | |
} | |
final class UserCollection: UserProtocol { | |
let users: [UserShort] | |
init(users: [UserShort]) { | |
self.users = users | |
} | |
func sortedByName() -> [UserShort] { | |
return users.sorted { $0.name > $1.name } | |
} | |
} | |
let follower = UserShort(id: 1, name: "Medium Guest") | |
let currentUser = UserLong(id: 999, name: "Maxim Vialykh", status: "Work on SOLID Arcticle", isOnline: true) | |
let userCollection = UserCollection(users: [currentUser, follower]) | |
let sorted = userCollection.sortedByName() | |
for case let user as UserLong in sorted { | |
print("UserLong: \(user.name)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment