Swift struct extension & AssociatedObject

None of these are quite the correct answer for Question 1 (sorry).

Swift allows you to declare methods in an extension that conflict with another module's methods in case you were there first. If the standard library didn't have append and then added it, the meaning of your own code shouldn't change. [EDIT: As @TannerJin points out, though, it doesn't always work.]

Unfortunately, this breaks down a little when you start getting into multiple modules (there's no longer a clear choice of which one you meant), and it's questionable if it's even a good idea within one module, since it means your code looks standard but does something different.


For Question 2, Swift will convert values to Objective-C objects when asked to, but it doesn't guarantee whether it will convert it to the same instance or make a new instance each time. It is therefore not safe to use associated objects with structs (or enums, or anything except class instances), even though the compiler will allow you to do it and it appears to work in the array case.

7 Likes