-
Mix Swift and C++
Learn how you can use Swift in your C++ and Objective-C++ projects to make your code safer, faster, and easier to develop. We'll show you how to use C++ and Swift APIs to incrementally incorporate Swift into your app.
Chapitres
- 0:40 - Basics of interoperability
- 2:22 - Adding Swift to a C++ codebase
- 4:10 - Calling a C++ method in Swift
- 4:50 - Calling a Swift method in C++
- 7:32 - Improving how C++ APIs are imported
- 12:15 - Foreign reference types
Ressources
Vidéos connexes
WWDC23
-
Rechercher dans cette vidéo…
-
-
4:10 - Calling a C++ method from Swift
func loadImage(_ image: UIImage) { // Load an image into the shared C++ class. CxxImageEngine.shared.pointee.loadImage(image) } -
4:20 - Import a C++ framework
import CxxImageKit -
4:45 - Import the Generated Header
#import "SampleApp-Swift.h" -
4:57 - Calling a Swift method in C++
- (IBAction)openPhotoLibrary:(UIButton *)sender { // Construct SwiftUI view SampleApp::ImagePicker::init().present(self); } -
8:22 - Using the SWIFT_COMPUTED_PROPERTY attribute
int getValue() const SWIFT_COMPUTED_PROPERTY; void setValue(int newValue); -
8:42 - Using the SWIFT_SHARED_REFERENCE attribute
struct SWIFT_SHARED_REFERENCE(retain, release) CxxReferenceType; -
8:52 - Using the SWIFT_RETURNS_INDEPENDENT_VALUE attribute
SWIFT_RETURNS_INDEPENDENT_VALUE std::string_view networkName() const; -
10:45 - Using a for-loop to iterate over a C++ std::vector in Swift
// Get every image out of the shared C++ class. for image in CxxImageEngine.shared.pointee.getImages() { let uiImage = CxxImageEngine.shared.pointee.uiImageFrom(image) UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil) } -
13:54 - Import swift/bridging
#import <swift/bridging> -
14:01 - Applying the SWIFT_SHARED_REFERENCE attribute to CxxImageEngine
struct SWIFT_SHARED_REFERENCE(IKRetain, IKRelease) CxxImageEngine { // ... }; -
14:53 - Applying the SWIFT_COMPUTED_PROPERTY attribute to getImages
/// \returns all images that have been loaded into the engine. Includes any modifications that were /// applied to the images. SWIFT_COMPUTED_PROPERTY inline std::vector<Image *_Nonnull> getImages() const; -
15:06 - Updated for-loop using the "images" computed property
// Get every image out of the shared C++ class. for image in CxxImageEngine.shared.pointee.images { let uiImage = CxxImageEngine.shared.pointee.uiImageFrom(image) UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil) }
-