How are Objective-C factory methods converted into Swift convenience initializers?
NickName:Jayson Ask DateTime:2014-06-14T05:32:26

How are Objective-C factory methods converted into Swift convenience initializers?

Apple's documentation is quite clear about how Objective-C initialization methods get converted into Swift intializers:

The “init” prefix gets sliced off and becomes a keyword to indicate that the method is an initializer. For init methods that begin with “initWith,“ the “With” also gets sliced off. The first letter of the selector piece that had “init” or “initWith” split off from it becomes lowercase, and that selector piece is treated as the name of the first argument. The rest of the selector pieces also correspond to argument names.

It is also possible to use factory class methods as initializers; however, there is much less information on how these selector names are mapped to Swift functions:

For consistency and simplicity, Objective-C factory methods get mapped as convenience initializers in Swift. This mapping allows them to be used with the same concise, clear syntax as initializers. For example, whereas in Objective-C you would call this factory method like this:

OBJECTIVE-C

UIColor *color = [UIColor colorWithRed:0.5 green:0.0 blue:0.5 alpha:1.0];

In Swift, you call it like this:

SWIFT

let color = UIColor(red: 0.5, green: 0.0, blue: 0.5, alpha: 1.0)

What are the rules for mapping Objective-C factory methods to Swift initializers?

Copyright Notice:Content Author:「Jayson」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24214061/how-are-objective-c-factory-methods-converted-into-swift-convenience-initializer

More about “How are Objective-C factory methods converted into Swift convenience initializers?” related questions

How are Objective-C factory methods converted into Swift convenience initializers?

Apple's documentation is quite clear about how Objective-C initialization methods get converted into Swift intializers: The “init” prefix gets sliced off and becomes a keyword to indicate that the

Show Detail

How to call a factory method of an Objective-C class from Swift?

I have an obj-c class that uses a factory method to instantiate itself as a singleton. I added that class to my Swift bridge header and want to call this factory method from a swift class. However,...

Show Detail

Expose a Swift convenience initializer as an Objective-C factory constructor

I'm trying to make the new Swift version of a library source-compatible with the legacy Objective-C version of the library. I know that if an Objective-C convenience initializer and Objective-C fac...

Show Detail

Obj-C factory method not exposed to Swift subclasses

More specifically imageNamed does not get exposed to Swift subclasses of NSImage even when all other convenience inits are inherited. According to Apple's docs Objective-C factory methods "get map...

Show Detail

Build error with Swift public convenience initializers in frameworks

I have a question regarding swift convenience initializers. I have a project ComputerAlgebra. This is a Cocoa Framework (for iOS at the moment) and it contains this code: public class CANumber{

Show Detail

Adding Convenience Initializers in Swift Subclass

As a learning exercise I am trying to implement a subclass of SKShapeNode that provides a new convenience initializer that takes a number and constructs a ShapeNode that is a square of number width...

Show Detail

Are convenience initializers really necessary in Realm?

Lately while reading through the swift Realm documentation I've stumbled upon this passage: Custom initializers for Object subclasses: When creating your model Object subclasses, you may somet...

Show Detail

ObjC <-> Swift bridging and factory methods

I've some Objective-C class that I want to use from Swift. For reasons that are beyond the scope of this question, my class init method is marked as unavailable. Instead, there is a factory method....

Show Detail

Subclassing factory methods of URLSession in Swift

I am trying to create a subclass of URLSession in Swift (reason does not matter, but has to do with testing). I need it to work with a delegate and a specific URLSessionConfiguration, which is a read-

Show Detail

Swift Objects initialization (Class factory method, default init, convenience init)

I'm trying to figure out the best pattern to work with objects in Swift. i think i got it right with the initializers, both convenience and default... but what happen with the class factory methods...

Show Detail