A Swift Package Manager console app and library to convert Objective-C code into Swift.
| Platform | Build Status |
|----------|--------|
| macOS | |
| Linux |
|
A program that aims to aid in automatization of conversion of Objective-C code into equivalent Swift code.
For information about how it's structured, see the Architecture page.
Given the following files:
MyClass.h: ```objc /// A simple class to store names @interface MyClass : NSObject /// First name @property (nonnull) NSString *name; /// Last name @property (nonnull) NSString *surname;
MyClass.m:
objc @implementation MyClass - (instancetype)initWithName:(NSString*)name surname:(NSString*)surname { self = [super init]; if(self) { self.name = name; self.surname = surname; } return self; } - (void)printMyName { NSLog(@"%@ %@", self.name, self.surname); } @end
Running SwiftRewriter as shown:
$ swift run SwiftRewriter files --colorize --target stdout MyClass.h MyClass.m
will produce the following Swift file in the standard output:
/// A simple class to store names class MyClass: NSObject { /// First name var name: String /// Last name var surname: Stringinit(name: String, surname: String) { self.name = name self.surname = surname super.init() } /// Prints the full name to the standard output func printMyName() { NSLog("%@ %@", self.name, self.surname) }
}
Xcode 12 & Swift 5.3
$ swift run -c=release SwiftRewriter files --colorize --target stdout /path/to/MyClass.h /path/to/MyClass.m
$ swift run -c=release SwiftRewriter path /path/to/project/
swift run SwiftRewriter --helpto print the full reference of command line arguments SwiftRewriter accepts. Reference also available bellow (for
pathsubcommand):
Usage:
OVERVIEW:Examines a path and collects all .h/.m files to convert, before presenting a prompt to confirm conversion of files.
USAGE: SwiftRewriter path
ARGUMENTS: Path to the project to inspect
OPTIONS: -e, --exclude-pattern Provides a file pattern for excluding matches from the initial Objective-C files search. Pattern is applied to the full path. -i, --include-pattern Provides a pattern for including matches from the initial Objective-C files search. Pattern is applied to the full path. --exclude-pattern takes priority over --include-pattern matches. -s, --skip-confirm Skips asking for confirmation prior to parsing. -o, --overwrite Overwrites any .swift file with a matching output name on the target path. -c, --colorize Pass this parameter as true to enable terminal colorization during output. -e, --print-expression-types Prints the type of each top-level resolved expression statement found in function bodies. -p, --print-tracing-history Prints extra information before each declaration and member about the inner logical decisions of intention passes as they change the structure of declarations. -v, --verbose Prints progress information to the console while performing a transpiling job. -t, --num-threads Specifies the number of threads to use when performing parsing, as well as intention and expression passes. If not specified, thread allocation is defined by the system depending on usage conditions. --force-ll Forces ANTLR parsing to use LL prediction context, instead of making an attempt at SLL first. May be more performant in some circumstances depending on complexity of original source code. --emit-objc-compatibility Emits '@objc' attributes on definitions, and emits NSObject subclass and NSObjectProtocol conformance on protocols.
This forces Swift to create Objective-C-compatible subclassing structures which may increase compatibility with previous Obj-C code.
--diagnose-file Provides a target file path to diagnose during rewriting. After each intention pass and after expression passes, the file is written to the standard output for diagnosing rewriting issues. -w, --target Specifies the output target for the conversion. Defaults to 'filedisk' if not provided.
stdout Prints the conversion results to the terminal's standard output; filedisk Saves output of conversion to the filedisk as .swift files on the same folder as the input files.
-f, --follow-imports Follows #import declarations in files in order to parse other relevant files. -h, --help Show help information.
The program should output the contents of the files you pass into the standard output.
This is mostly a side project of mine to train my Swift and architecture chops. That being said, there are a couple of main goals that I have in mind going forward with this:
SwiftRewriter should:
Some other libraries and resources that are also concerned with automating the process of converting Objective-C to Swift to some degree include that I think are worth mentioning:
Yahoo's Objc2Swift makes language transformations, but it has shortcomings as far as making large transpilations go: Its grammar implementation lacks modern Objective-C features (such as generic types), and it makes no semantic transformations, doing AST conversions only. Main inspiration for writing this tool, I just thought augmenting it with a more modern Objective-C syntax and some semantical awareness would be nifty.
Objc2Swift.js is a more fully-fledged, nifty converter that is semantically-aware and attempts to produce working code while respecting semantics, but it does not currently support emitting Swift 3, 4 and 5-compatible code.
Swiftify is a comercial product which does many high and low-level transformations, producing code that appears to be (I haven't tested it much, tbh) nearly fully-functioning Swift code.