Some Objective-C methods are invisible in Swift 3

205 views Asked by At

Why this method + (instancetype)arrayWithNSData:(NSData *)data;

in j2obj project

https://github.com/google/j2objc/blob/master/jre_emul/Classes/IOSPrimitiveArray.h#L252

invisible for Swift3?

but the other are visible. for example this:

+ (instancetype)newArrayWithBytes:(const jbyte *)buf count:(NSUInteger)count;

1

There are 1 answers

0
rickster On BEST ANSWER

Factory methods in ObjC — that is, class methods that return instancetype (or the actual type of the class) and start with the common name of the class — get imported to Swift as initializers.

So a method +(instancetype)arrayWithSomething: on a class whose name includes Array gets imported as init(something:). In your case, +arrayWithNSData: will get imported as init(nsData:). (You might think it'd drop the "NS", since it'll also convert the parameter type from NSData to its Swift value type equivalent Foundation.Data... but it doesn't.)

To find this and other importer effects, you can see the result for yourself in Xcode by looking at the "Generated Interface" view of your source. Check the related items button at the left of the jump bar, or the assistant editor.

For more details, read about Initialization in Apple's Using Swift with Cocoa and Objective-C doc.