There are some differences between Objective-C and Swift. During the conversion, I found that the customized constructor or initializer in ObjC is different from Swift. For example, in ObjC, the initializer is in the format of initWith...:...
parameters, while in Swift, all initializers are in the same name of init(...)
, like other modern languages.
This presents a challenge, how this kind of initWith...:...
ObjC constructor be directly converted to Swift?
Class Factory
I come to Design Pattern to find a solution: Class Factory. In Swift, a static function can be defined as class factory to create an instance of the class!
For example, in the following swift file, all init() constructors are private. As a result, I force to use class methods to create instance of the class
MyLogger
:- import Foundation
- //FIXME: Remove objc and inheritance from NSObject after convertion to Swift is completed
- @objc
- class MyLogger : NSObject {
- //MARK: Init methods
- //Make all init as private constructors
- private override init() {
- self.level = MyLoggerLevel.LogLevelDebug
- }
- private convenience init(loggingContext: String) {
- self.init()
- self.context = loggingContext
- }
- //Force to use these two class methods to create instance!
- static func instance(loggingContext: String) -> MyLogger {
- return MyLogger(loggingContext:loggingContext)
- }
- ...
[[MyLogger alloc] initWith...]
. In this way, the converted ObjC codes looks like this.- #import "MyiOSApp-Swift.h"
- #import "ObjC2SwiftHelper.h"
- @interface MyObjCClass ()
- @property (strong, nonatomic) MyLogger* logger;
- @synthesize logger = _logger;
- - (MyLogger *) logger {
- if (!_logger) {
- /* Remove alloc init codes
- _logger = [[MyLogger alloc] init];
- _logger.context = CALLER_CONTEXT; */
- //Use class method to get instance
- _logger = [MyLogger instance:CALLER_CONTEXT];
- }
- return _logger;
- }
- ...
Note
Even I made my
Actually it is good to find it out. If I did not write this blog, I would not go so deep to find the issue.
Even I made my
init(...)
constructor in swift as private, but I found this is still accessible from ObjC. Not sure why.Actually it is good to find it out. If I did not write this blog, I would not go so deep to find the issue.
Method Signature Mapping
In Swift, the method signature includes both method name and parameter names. Therefore, the conversion to ObjC has to be matched completely in both.I find out that a method definition in Swift, as in the following example:
- //Case 1: method with two named parameters
- func swiftMethod(para1 sValue : String, para2 aInt : Int) {
- ...
- }
where:
swiftMethod
is the name of method, para1
and para2
are parameter names, and sValue
and aInt
are parameter value names.
The first parameter name can be omitted without a name, which is very common in both swift and ObjC:
- //Case 1: method with 1st parameter with no name, 2nd with a name
- func swiftMethod(sValue : String, para2 aInt : Int) {
- ...
- }
The 2nd case is very simple to map to ObjC:
- [swiftMethod:@"Test" para2: 1];
How about the first case, the first parameter with a specified name? I found out the interesting point, the first parameter name has to be linked
with
With in between method name and parameter name.
- [swiftMethodWithPara1:@"Test" para2: 1];
Note
Notice that camel case of capital letter of the parameter name in part of method name(
Notice that camel case of capital letter of the parameter name in part of method name(
swiftMethodWithPara1
), even it is defined in Swift in lower case as para1
.The above swift example can be further simplified with all parameters with no names
- //Case 3: method with all parameters with no names
- func swiftMethod(sValue : String, para2 : Int) {
- ...
- }
To convert the 3rd case to ObjC, the parameter names after 1st parameter have to use the swift parameter value names as default names:
- [swiftMethod:@"Test" para2: 1];
Based on above findings, it seems that an alternative ObjC solution for swift
init(...)
customized constructors is found, as in the following example:- let defaultInitInt = 1
- class MyClass {
- //init with two named parameters
- init(para1 sValue : String, para2 aInt : Int) {
- ...
- }
- //init with only one parameter without name
- convenience init(sValue : String) {
- self.init(para1: sValue, para2: defaultInitInt)
- }
- }
The corresponding ObjC code can be something like this:
- //Create instance with init of two named parameters
- MyClass* instance1 = [[MyClass alloc] initWithPara1: @"Test" para2:1];
- //Create instance with init of only one parameter
- MyClass* instance2 = [[MyClass alloc] initWithSValue:@"Test2"];
0 comments:
Post a Comment