Custom annotation types should be added to Swift
| Originator: | bryan | ||
| Number: | rdar://18664041 | Date Originated: | 10/15/2014 |
| Status: | Open | Resolved: | No |
| Product: | Developer Tools | Product Version: | iOS SDK 8.X |
| Classification: | Enhancement | Reproducible: | Yes |
Summary:
Annotations are a great feature that allows metadata to be added to classes, methods, variables, parameters, and packages. Annotations can be both used by developer tools at compile-time as well as at run-time, by your code itself.
First, let’s use an annotation to tell the compiler that we’re overriding a method that belongs on our superclass (don’t be confused by the @ symbol which obviously has all sorts of meanings in Objective-C but is only used for annotations in Java):
@Override
public String someMethod(Array array, Integer integer) {
}
Why is this useful? Let’s say that a typo was included in the method name, or a wrong type was used for one of the arguments. Normally this wouldn’t generate a warning or error, a new method would simply be declared instead of overriding an existing one. By using the @Override annotation, the compiler can tell us that a mistake has been made rather than failing silently.
The @Override annotation actually ships with the Java SDK, but you can define your own custom annotation types as well. Annotations can include a list of optional key-value pairs, which allow us to build something like the following:
@ResponseField(key = "blog_name", trim = true, required = true)
String blogName;
Say you’re implementing a mobile app that parses data from an API. Annotations allow this to be done declaratively instead of procedurally. Instance variables can be marked up with a custom @ResponseField annotation that indicates that they should be populated with values from the API responses. The response parsing logic can now be completely generic and reused across all of your different model objects:
* Introspect an instance for variables with the @ResponseField annotation
* Use the key field to determine where the desired value can be found in the API response dictionary
* Use other fields to do other things, e.g. trim strings if trim = true, abort if a non-nullable field is missing, run a regular expression to ensure a field contains a valid value, etc.
Steps to Reproduce:
Try to use custom annotation types in Swift
Expected Results:
Swift would have support for custom annotation types
Actual Results:
Swift doesn't have support for custom annotation types
Version:
All versions of the SDK with support for Swift
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!