Post

Replies

Boosts

Views

Activity

Pass Swift Array To C++ As A std::vector
Hi -- I would like to be able to pass a Swift Array to C++ code. I know it should be possible, but having trouble figuring out the right way to do it. Here is an example class showing what I want to do. C++ code: class Foo { public: Foo(const std::vector<uint8_t> & values) : _values(values) {} protected: std::vector<uint_8> _values; }; Swift code: let values: [UInt8] = [ 1, 2, 3, 4, 5, 6 ] var foo = Foo(values) If I do this, I get the following error message: Cannot convert value of type '[UInt8]' to expected argument type 'std.__1.vector<UInt8, allocator<UInt8>>'.
4
0
1.7k
Jan ’24
Swift and C++ In The Same Project
Hi -- I am attempting to use C++ and Swift in a single project but I am struggling with finding the proper way to do this. Ideally I want to have both my C++ and Swift code in the same project and not use a framework to keep them separate. As an example, how would I create an object of the following C++ class: class Foo { public: Foo() { // do stuff } } From what I read, it should be as simple as this: let foo = Foo() But Xcode says it Cannot find 'Foo' in scope. How do I import Foo into my Swift code? Is there a project setting that needs to be changed to automatically make my C++ classes available? Or do I need to create a Clang module (as described in this page: https://www.swift.org/documentation/cxx-interop/#importing-c-into-swift) to expose the C++ code to Swift? If so, where in my Xcode project should that go? I am using Xcode 15.2 on macOS 14.2.1. I have also set the C++ and Objective-C Interoperability setting for my project to C++/Objective-C++.
3
0
3.3k
Jan ’24