Swift removes the alloc / init initialization pattern from Objective-C and replaces it with a construction syntax similar to that seen in Java or C ++.
When in the interface of the Swift class (or in the Apple documentation) you see something like:
class Thing { init(number: Int) }
This means that you can create Thing by calling it with the following "build syntax":
let thingTwo = Thing(number: 2)
It doesn't matter if it is a class or struct , or if there are other things in front of init , such as convenience or required or public ...... how you use it, (Of course, replace Thing type you use, number with any labels (s) in the desired initializer that you want, and 2 with the corresponding value for this parameter.)
When classes originally defined in ObjC are imported into Swift, the Swift compiler does a ton of things to make these APIs look and feel more like Swift's own code. One of these things is to replace the general scheme for using class methods as convenience constructors with real initializers.
All examples from the question:
+ (NSDate *)date; + (NSURL *)urlWithString:(NSString *)string; + (instancetype)layerWithSession:(AVCaptureSession *)session;
... are such convenience constructors. This means that they import into Swift not as class methods ( NSDate.date() , NSURL.urlWithString() , etc.), but as initializers:
class NSDate { init() } class NSURL { init?(string: String) } class AVCaptureVideoPreviewLayer { init!(session: AVCaptureSession) }
... and you call them with the syntax for constructing the object:
let date = NSDate() let url = NSURL(string: "http://apple.com") let layer = AVCaptureVideoPreviewLayer(session: mySession)
In fact, when you see the "inaccessible, use object design" error messages, Xcode almost always offers an automatic fix to insert the correct syntax.
How it works? Swift looks for possible "base names" of the class and matches these names and method / initializer method names. (It also checks for class methods that return an instance of this class, initializers that start with "init ...", etc.)
For example, Swift sees the following as a convenience constructor:
@interface PDQMediaManager: NSObject + (PDQMediaManager *)manager; @end
This can be a problem if it is assumed that you (or the original PDQMediaManager developer) for this class should act as a singleton, and the +manager method to return a singleton instance - if the Swift developer writes PDQMediaManager() , they will create a new instance instead of retrieve singleton.
In this case, one good solution is to rename the singleton method:
+ (PDQMediaManager *)defaultManager;
This will not be considered a convenience constructor, so Swift clients can call PDQMediaManager.defaultManager() to get a singleton instance.
You can also use NS_SWIFT_NAME to rename the singleton method only for Swift clients:
+ (PDQMediaManager *)manager NS_SWIFT_NAME(defaultManager());
If you do not control the codebase using the insult method, the best option is probably to write your own ObjC code to send the singleton method and put it on your Swift code through the connecting header.