3 ms·
They generate new Swift headers for Objective-C classes, and have translation rules in their toolset to remap/rename some arguments (all the init methods for ex
by timanglade 12y ago
They generate new Swift headers for Objective-C classes, and have translation rules in their toolset to remap/rename some arguments (all the init methods for example). The tool can even do that on your own Objective-C projects. I used this on a side project this weekend and it’s pretty rad.
- seanalltogether 12y agoAre these translation rules published somewhere? I'd really like to understand what keywords trigger what kind of mapping rules.
- JimDabell 12y agoI'm pretty sure that's just a misstatement in the documentation. The Swift code is equivalent to the UIColor initialiser, there's no need to auto-translate the factory method as well. The auto-translation process for initialisers is described immediately before the part you quote.
- seanalltogether 12y agoThe section above mentions specifically translating either "init" or "initWith" from initializers. I'm curious if they're also pattern matching the "colorWith" prefix or if its more complicated.
- JimDabell 12y agoI think you might have missed my point: the `initWith` translation produces the Swift constructor that the documentation you quoted presents as the factory method equivalent. The `colorWith` factory method is useless in this context because a matching `initWith` initialiser exists. If the factory method were pattern matched in the same way, it would break things because it would produce a constructor with the same method signature as the translated initialiser.
- jackhlaw 12y agoWe 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.
- seanalltogether 12y agoAre there other pattern matches that can get triggered? I've come across classes that initialize with "For" as in [MyClass classForString:str] or similar.
- jayrhynas 12y agoFrom 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)