Some additional background in addition to the already given answers:
The Objective-C method
+ (id)buttonWithType:(UIButtonType)buttonType
returns id
. This was the "traditional" way to declare a "factory method" in a way that it
can be used from subclasses as well. There is no type cast necessary in
UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
because id
can be converted to any Objective-C pointer.
Now the equivalent type to id
in Swift is AnyObject
, and the above method is mapped to
class func buttonWithType(buttonType: UIButtonType) -> AnyObject!
Swift is much more strict and does not implicitly convert types, therefore the return value has to be cast to UIButton
explicitly:
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
The "modern" approach to declare factory methods is instancetype
(see for example http://nshipster.com/instancetype/ or Would it be beneficial to begin using instancetype instead of id?). A simple example is
the NSString
method
+ (instancetype)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc
which is mapped in Swift to
class func stringWithCString(cString: CString, encoding enc: UInt) -> Self!
Self
is the type of the object on which the method is called, so that
the return type of
NSString.stringWithCString("foo", encoding: NSUTF8StringEncoding)
is NSString!
and the return type of
NSMutableString.stringWithCString("bar", encoding: NSUTF8StringEncoding)
is NSMutableString!
. No type cast is necessary in Swift. In the following
example, the Swift compiler "knows" that str
is an NSString
:
var str = NSString.stringWithCString("foo", encoding: NSUTF8StringEncoding)
var cs = str.UTF8String
The Foundation framework headers already use instancetype
in many places, but not
yet everywhere where possible (as in buttonWithType:
). This may be improved in
future releases of the SDKs.