Declaring and using custom attributes in Swift

swift

Solution

If we take the iBook as definitive, there appears to be no developer-facing way of creating arbitrary new attributes in the way you can in Java and .NET. I hope this feature comes in later, but for now, it looks like we're out of luck. If you care about this feature, you should file an enhancement request with Apple (Component: Swift Version: X)

FWIW, there's really not a way to do this in Objective-C either.

Problem

I would like to be able to annotate my types and methods with meta-data and read those at runtime. The language reference explains how to declare attribute usages, but is it actually possible to declare your own attributes? Reading would require some kind of reflection mechanism, which I was not able to find in the reference at all, so the second part of the question probably is - is there reflection possible. If these features are not available in Swift, can they be done with Objective-C code (but on Swift instances and types)? A relatively unrelated note: The decision of what has been modelled as an attribute and what has been added to the core syntax strikes me as pretty arbitrary. It feels like two different teams worked on the syntax and on some attributes. E.g. they put `weak` and `unowned` into the language as modifiers, but made `@final` and `@lazy` attributes. I believe that once they actually add access modifiers, they will probably be attributes likes `final`. Is all of this somehow related to Objective-C interoperability?

Original source