| Start |
|---|
| Use a module map to import C headers A module map (opens new window) can simply Place a file named Inside the module map file: Then Use the For more information about the module map syntax, see the Clang documentation about module maps (opens new window). |
| Using Objective-C classes from Swift code If MyFramework contains Objective-C classes in its public headers (and the umbrella header), then Bridging headersA bridging header makes additional Objective-C and C declarations visible to Swift code. When adding project files, Xcode may offer to create a bridging header automatically: To create one manually, modify the Objective-C Bridging Header build setting: Inside the bridging header, import whichever files are necessary to use from code: Generated InterfaceClick the Related Items button (or press ⌃1), then select Generated Interface to see the Swift interface that will be generated from an Objective-C header.
|
| Specify a bridging header to swiftc The |
| Fine-grained interoperation between Objective-C and Swift When an API is marked with The generated interface (opens new window) looks like this: Now you can replace the API with a more "Swifty" extension. In this case, we can use an optional (opens new window) return value, filtering out NSNotFound (opens new window): In most cases you might want to restrict whether or not an argument to an Objective-C function could be With that written, the compiler shall emit an error whenever we try to pass The opposite of To further help the compiler with optimising your code, you also might want to specify if the block is escaping: With this attribute we promise not to save the block reference and not to call the block after the function has finished execution. |
| Use the C standard library Swift's C interoperability allows you to use functions and types from the C standard library. On Linux, the C standard library is exposed via the |
Using Swift classes from Objective-C code In the same moduleInside a module named "MyModule", Xcode generates a header named Relevant build settings:
In another moduleUsing RemarksFor further information, see Apple's documentation on Using Swift with Cocoa and Objective-C (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference