Re:Using Swift with Cocoa and Objective-C
by jackhlaw on Friday June 13, 2014 @12:58PM
(#7889765)
We actually _do_ pattern match UIColor colorWith and turn it into an initializer. If there are two initializers after we pattern match (e.g., UIColor colorWith and initWith), we eliminate the duplicate.
[ Parent
| on HN ]
Re:Using Swift with Cocoa and Objective-C
by seanalltogether on Friday June 13, 2014 @01:33PM
(#7889930)
Are there other pattern matches that can get triggered? I've come across classes that initialize with "For" as in [MyClass classForString:str] or similar.
From what I've been able to figure out just by playing around, the following rules are used:
1. Return type is `instancetype` or `MyClassName *`
2. Method takes at least one argument
3. Method name starts with a "class suffix"; that is, a suffix of the class name, with the restriction that you can't have partial words. The first letter may optionally be lower case.
The class suffix (optionally followed by "With" like in the `initWith` conversion) is stripped off and the rest of the method name is used for the first parameter, with the first letter lower-cased.
For example, the following conversions apply:
+[MyClassName myClassNameWithObject:obj] -> MyClassName(object: obj)
+[MyClassName classNameWithObject:obj] -> MyClassName(object: obj)
+[MyClassName nameObject:obj] -> MyClassName(object: obj);
However, since those all map to the same swift initializer, only one will be available (generally the first one declared)