[swift-users] Checking/getting custom objects from a collection
Milos Rankovic
milos at milos-and-slavica.net
Fri Apr 8 09:29:44 CDT 2016
Hi Adriano,
I’m glad if you are finding this useful. I’ll get back to you on `add` and `remove`, but just let me confirm with you: You actually want to allow multiple, say, `Health` components to be added to your `Character`? Most of the complication in my suggested code comes from trying to prevent that! Things would be simpler if this is not your requirement. I just need to double check that with you, since I can not think of a reason that would be a good thing. I expressly sketched out the implementation so that you can keep `Health` and such immutable, so that you can update the character’s state simply by `add`ing (and therefore replacing) the existing `Health` value.
milos
> On 8 Apr 2016, at 15:12, Adriano Ferreira <adriano.ferreira at me.com> wrote:
>
> Milos,
>
> Thanks for taking a look at it, I appreciate you suggestion.
>
> It’s protocol-oriented, quite different from the idea I was trying to emulate — the one provided by GameplayKit.
>
> Well, I tried it and it works great.
>
>
> Now, would you implement those methods differently?
>
> mutating func add<T: Component>(component: T) {
> self.components[T.name] = component
> }
>
> mutating func remove<T: Component>(_: T.Type) {
> self.components[T.name] = nil
> }
>
> Also, since the key to the components dictionary is the name, adding a component of the same type will replace the exiting one.
>
> How would you change that so it would be possible to add some components more than once, as Jens mentioned?
>
> Best,
>
> —A
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160408/eff2aebf/attachment.html>
More information about the swift-users
mailing list