Skip to content

Commit dfe0085

Browse files
committed
[docs] CToSwift: Finish swift_name section
Struct/union fields, ObjC properties, ObjC methods
1 parent 3afbe31 commit dfe0085

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

docs/CToSwiftNameTranslation.md

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ __attribute__((swift_name("SpacecraftCoordinates")))
252252
struct SPKSpacecraftCoordinates {
253253
double x, y, z, t; // space and time, of course
254254
};
255+
256+
// Usually seen as NS_SWIFT_NAME.
255257
```
256258
257259
```swift
@@ -287,12 +289,10 @@ The `swift_name` attribute can be used to give a C function a custom name. The v
287289
```objc
288290
__attribute__((swift_name("doSomething(to:bar:)")))
289291
void doSomethingToFoo(Foo *foo, int bar);
290-
291-
// Usually seen as NS_SWIFT_NAME.
292292
```
293293
294294
```swift
295-
func doSomething(foo: UnsafeMutablePointer<Foo>, bar: Int32)
295+
func doSomething(to foo: UnsafeMutablePointer<Foo>, bar: Int32)
296296
```
297297

298298
An underscore can be used in place of an empty parameter label, as in Swift.
@@ -430,4 +430,86 @@ Although enumerators always have global scope in C, they are often imported as m
430430

431431
_Currently, `swift_name` does not even allow importing an enum case as a member of the enum type itself, even if the enum is not recognized as an `@objc` enum, error code enum, or option set (i.e. the situation where a case is imported as a global constant)._
432432

433+
434+
### Fields of structs and unions; Objective-C properties
435+
436+
The `swift_name` attribute can be applied to rename a struct or union field or an Objective-C property (whether on a class or a protocol). The value of the attribute must be a valid Swift identifier.
437+
438+
```objc
439+
struct SPKSpaceflightBooking {
440+
const SPKLocation * _Nullable destination;
441+
bool roundTrip __attribute__((swift_name("isRoundTrip")));
442+
};
443+
```
444+
445+
```swift
446+
struct SPKSpaceflightBooking {
447+
var destination: UnsafePointer<SPKLocation>?
448+
var isRoundTrip: Bool
449+
}
450+
```
451+
452+
453+
### Objective-C methods
454+
455+
The `swift_name` attribute can be used to give an Objective-C method a custom name. The value of the attribute must be a full Swift function name, including parameter labels.
456+
457+
```objc
458+
- (void)doSomethingToFoo:(Foo *)foo bar:(int)bar
459+
__attribute__((swift_name("doSomethingImportant(to:bar:)")));
460+
```
461+
462+
```swift
463+
func doSomethingImportant(to foo: UnsafeMutablePointer<Foo>, bar: Int32)
464+
```
465+
466+
As with functions, an underscore can be used to represent an empty parameter label.
467+
468+
Methods that follow the NSError out-parameter convention may provide one fewer parameter label than the number of parameters in the original method to indicate that a parameter should be dropped, but they do not have to. The `swift_error` attribute is still respected even when using a custom name for purposes of transforming an NSError out-parameter and the method return type.
469+
470+
```objc
471+
- (BOOL)doSomethingRiskyAndReturnError:(NSError **)error
472+
__attribute__((swift_name("doSomethingRisky()")));
473+
- (BOOL)doSomethingContrived:(NSString *)action error:(NSError **)outError
474+
__attribute__((swift_name("doSomethingContrived(_:error:)")));
475+
```
476+
477+
```swift
478+
func doSomethingRisky() throws
479+
func doSomethingContrived(_ action: String, error: ()) throws
480+
```
481+
482+
A base name of "init" can be used on a *class* method that returns `instancetype` or the containing static type in order to import that method as an initializer. Any other custom name *prevents* a class method from being imported as an initializer even if it would normally be inferred as one.
483+
484+
```objc
485+
+ (Action *)makeActionWithHandler:(void(^)(void))handler
486+
__attribute__((swift_name("init(handler:)")));
487+
+ (instancetype)makeActionWithName:(NSString *)name
488+
__attribute__((swift_name("init(name:)")));
489+
```
490+
491+
```swift
492+
/* non-inherited */ init(handler: () -> Void)
493+
init(name: String)
494+
```
495+
496+
A no-argument method imported as an initializer can be given a dummy argument label to disambiguate it from the no-argument `init()`, whether the method is an init-family instance method or a factory class method in Objective-C.
497+
498+
```objc
499+
- (instancetype)initSafely
500+
__attribute__((swift_name("init(safe:)")));
501+
+ (instancetype)makeDefaultAction
502+
__attribute__((swift_name("init(default:)")));
503+
```
504+
505+
```swift
506+
init(safe: ())
507+
init(default: ())
508+
```
509+
510+
A custom name on an instance method with one of Objective-C's subscript selectors (`objectAtIndexedSubscript:`, `objectForKeyedSubscript:`, `setObject:atIndexedSubscript:`, or `setObject:forKeyedSubscript:`) prevents that method from being imported as a subscript or used as the accessor for another subscript.
511+
512+
_Currently, this only works if *both* methods in a read/write subscript are given custom names; if just one is, a read/write subscript will still be formed. A read-only subscript only has one method to rename._
513+
514+
433515
## More to come...

0 commit comments

Comments
 (0)