The applications have been used long time can not be written with only Swift. They ofter contains C/C++ or Objective-C codes. My mac application is also written with C/C++ and Objective-C.
This post says that tips for combining Objective-C and Swift.
Declare the code supports ARC
If your code expects that is compiled with ARC, you should declare it. The following code reports the error when you compile without the ARC.
#if !__has_feature(objc_arc) #error This source file must be compiled with ARC. #endif
If you write the warning “This source file must be compiled with ARC” with the comment only, it doesn’t help someone merge your code, I think. They may miss your comment. However, if the compiler reports error, they can found it.
Specify the nullability
Swift has an option but Objective-C doesn’t. The message to the NIL is ignored in Objective-C.
Objective-C now has specifier. It specify the nullability of any variables, properties, results and parameters of methods.
It can help the mapping to the Swift.
“nullable” is mapped to optional and “nonnull” is mapped to non-optional.
@interface MyObject : NSObject @property (nonnull, nonatomic, strong) NSString *firstName; @property (nonnull, nonatomic, strong) NSString *lastName; @property (nullable, nonatomic, strong) NSString *middleName; - (nullable MyObject *)nextObject; - (nonnull String *)fullNameWithPrefix:(nullable NSString *)prefix suffix:(nullable NSString *)suffix; @end
Elements Type of a collection
The collection of the Objective-C is mapped to the collection of Any in the Swift. For example, the NSArray is mapped to [Any].
The Objective-C can now specify the element type of the collection.
// The array of the NSString NSArray<NSString *> *stringsArray; // The key is NSNumber, and the value is NSString NSDictionary<NSNumber *, NSString *> *mappingTable;
- Representative of RK Kaihatsu. I am a professional developer specializing in macOS Apps, iOS Apps, SDKs and middleware development. I often use ObjC, Swift and C++. Based on development experience, I provide e-learning contents, technical books and technical guidance. I am also a technical seminar instructor, in-house training instructor, and administrative / local goverment staff training instructor.