3 ms·
Others explained what benefit this has, but it's also helpful to understand the Objective-C heritage here. Swift on Apple platforms run on top of the Objective-
by jurip 7y ago
Others explained what benefit this has, but it's also helpful to understand the Objective-C heritage here. Swift on Apple platforms run on top of the Objective-C runtime, making it possible to call Objective-C from Swift and vice versa. Objective-C uses named parameters.
An Objective-C class interface might look like
@interface MyClass
- (NSString *)methodWithParam1:(NSString *)str1 param2:(NSString *)str2;
@end
And you'd call it like this:
NSString *newString = [myClassInstance methodWithParam1:val1 param2:val2];
The Swift translation layer lets you call that like this:
let newString = myClassInstance.method(param1: val1, param2: val)
And the translation works the other way around too, making Objective-C able to call a subset of Swift methods that use types representable in Objective-C. You need the named parameters to make it work, because the names of the parameters are part of the method name.
So it's a nice feature, but it was also necessary for Apple to be able to make Swift an incremental addition to their platform (yes I know of the Python/Ruby/etc translation layers for Objective-C but they're nowhere near as nice.)