Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active January 5, 2018 12:21
Show Gist options
  • Save vialyx/ccff9fc4a09ae3f02a29eac8acac2b92 to your computer and use it in GitHub Desktop.
Save vialyx/ccff9fc4a09ae3f02a29eac8acac2b92 to your computer and use it in GitHub Desktop.
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