Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring and using custom attributes in Swift

Tags:

swift

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?

like image 212
Sebastian Avatar asked Jun 04 '14 06:06

Sebastian


People also ask

What is the use of custom attributes?

A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.

How do you write a custom attribute?

In this example, name is a positional parameter. Any public read-write fields or properties are named parameters. In this case, version is the only named parameter. Note the use of the AttributeUsage attribute to make the Author attribute valid only on class and struct declarations.

What are attributes in Swift?

There are two kinds of attributes in Swift—those that apply to declarations and those that apply to types. An attribute provides additional information about the declaration or type.

What is frozen keyword Swift?

Introduced with Swift 5, the @unknown attribute is particularly useful when switching over a nonfrozen enumeration. While the @frozen attribute is used on familiar enums such as Result or Optional within the standard library.. @frozen.


2 Answers

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.

like image 64
ipmcc Avatar answered Sep 30 '22 09:09

ipmcc


You can now do something like this! Check out "Property Wrappers" - https://docs.swift.org/swift-book/LanguageGuide/Properties.html

Here's an example from that page:

@propertyWrapper struct TwelveOrLess {     private var number = 0     var wrappedValue: Int {         get { return number }         set { number = min(newValue, 12) }     } }   struct SmallRectangle {     @TwelveOrLess var height: Int     @TwelveOrLess var width: Int }  var rectangle = SmallRectangle() print(rectangle.height) // Prints "0"  rectangle.height = 10 print(rectangle.height) // Prints "10"  rectangle.height = 24 print(rectangle.height) // Prints "12" 
like image 31
Max Chuquimia Avatar answered Sep 30 '22 10:09

Max Chuquimia