This article mainly chats some of the unspoken rules brought by some objective-c and Swift mixed projects, hoping to help the friends who are puzzled about this. Below we begin to enter the topic:
Named
The official guide is simply a narrative (using Swift with Cocoa and Objective-c), that is, the Swift compiler automatically turns it into Swift-style A when we use the Objective-c API PI (that is, some method names, enumeration names, and so on do some regular deletions, that is, rename).
Single-instance method naming
When referencing the Objective-c singleton in Swift, a compilation error occurs if the Singleton method is included in the class name, let's look at a few examples below.
Example 1
@interface TXLocationManager : NSObject+ (instancetype)manager;@end
TXLoginManager
Class has a single-instance method named Manager, a compile error occurs when the manager method is referenced in Swift:
Plainly, the manager method has been wasted ...
Example 2
On the basis of Example 1, we changed the name of the single-instance method:
@interface TXLocationManager : NSObject+ (instancetype)shareInstance;@end
After the single-instance method name is changed to Shareinstance, the compiler passes. At this point, at least the problem has been solved, now let's look briefly at what is the reason? Why can't the manager method reference, but shareinstance can reference it?
Example 3
On the basis of Example 1, change the manager single-instance method name to Sharemanager:
@interface TXLocationManager : NSObject+ (instancetype)shareManager;@end
We can find that when referenced in Swift, the Sharemanager method name is renamed to share:
Summary
At this point, we can draw a simple naming unspoken rule: When quoting Objective-c Singleton in Swift, if the Singleton method is included in the class name, then a compilation error will occur, if the name of the Singleton method is exactly the suffix of the hump named by the class name, then the Swift A compilation error occurs when the Singleton method is referenced in the
Why is there such a problem with referencing the Objective-c class API in Swift? This is described on the official guide:
The Swift compiler automatically imports objective-c code as conventional swift code. It imports Objective-c class factory methods as Swift initializers, and Objective-c enumeration cases truncated names.
Because the Swift compiler automatically turns it into swift-style code when it uses OBJECTIVE-C code, it does some regular pruning of some method names, enumeration names, and so on.
There may is edge cases in your code that is not automatically handled. If you need to change the name imported by Swift of a Objective-c method, enumeration case, or option set value, you can Use the Ns_swift_name macro to customize how a declaration is imported.
According to the official guide, this case is special. So how to solve this problem, Swift provides a macro that deals with the kind of case--we encounterNS_SWIFT_NAME
@interface TXLocationManager : NSObject+ (instancetype)manager NS_SWIFT_NAME(shareInstance());@end
In this way, the manager of this singleton method, when we reference in Swift, is renamed to Shareinstance.
let _ = TXLocationManager.shareInstance()
Common method naming
Sometimes, when we refer to the API of a class in Objective-c in Swift, the method name may be renamed, and we'll look at the example below.
Class method
@interface TXLocationManager : NSObject+ (instancetype)managerWithCoordinateY:(CGFloat)y// Or// + (TXLocationManager *)managerWithCoordinateY:(CGFloat)y@end
When the class method of the class returns an instance object of its own type, the above method is renamed. This should be quoted as:
// 方式一:let _ = TXLocationManager.init(coordinateY: 9)// 方式二:let _ = TXLocationManager(coordinateY: 9)// 错误引用方式,编译失败let _ = TXLocationManager.manager(withCoordinateY: 9)
With this practice, we can see that the manager prefix in the class method is deleted and becomes the Init method in Swift. What if the class method of the class does not return an instance object of its own type?
@interface TXLocationManager : NSObject+ (void)managerWithCoordinateY:(CGFloat)y;// Or// + (NSObject *)managerWithCoordinateY:(CGFloat)y;// + (CGFloat)managerWithCoordinateY:(CGFloat)y;@end
In practice, it can be found that this can be referred to in Swift:
TXLocationManager.manager(withCoordinateY: 9)
The reference to this approach is consistent with our general approach, and is no different.
Instance method
The renaming rules for instance methods are somewhat similar to class methods, which are no longer detailed here, and friends of interest can practice them themselves. (Of course, the renaming of methods we can usually NS_SWIFT_NAME
specify)
Reference links
How to call an Objective-c singleton from Swift?
Small tips for objective-c and Swift mixed projects (i)