Skip to content

Instantly share code, notes, and snippets.

@lucasfernandes
Last active May 13, 2020 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasfernandes/9305c4db538de9b66b663a857c16e739 to your computer and use it in GitHub Desktop.
Save lucasfernandes/9305c4db538de9b66b663a857c16e739 to your computer and use it in GitHub Desktop.
associatedType
protocol ItemStoring {
associatedtype DataType
var items: [DataType] { get set }
mutating func add(item: DataType)
}
extension ItemStoring {
mutating func add(item: DataType) {
items.append(item)
}
}
struct NameDataBase: ItemStoring {
var items = [String]()
}
var names = NameDataBase()
names.add(item: "Test 1")
names.add(item: "Item 2")
print(names)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment