{"id":518,"date":"2024-06-04T20:13:32","date_gmt":"2024-06-04T12:13:32","guid":{"rendered":"https:\/\/www.ixriver.com\/?p=518"},"modified":"2024-06-05T10:09:53","modified_gmt":"2024-06-05T02:09:53","slug":"%e5%9c%a8swift%e4%b8%ad%e8%b0%83%e7%94%a8c-c%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/www.v.ixriver.com\/?p=518","title":{"rendered":"\u5728swift\u4e2d\u8c03\u7528C\/C++\u4ee3\u7801"},"content":{"rendered":"\n<p>swift\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528C\u4ee3\u7801\uff0c\u4f46\u65e0\u6cd5\u76f4\u63a5\u8c03\u7528C++\u4ee3\u7801\uff0c\u8c03\u7528C++\u4ee3\u7801\u9700\u8981\u901a\u8fc7OC\u4e2d\u8f6c<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_65 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.v.ixriver.com\/?p=518\/#Swift%E8%B0%83%E7%94%A8C%E4%BB%A3%E7%A0%81\" title=\"Swift\u8c03\u7528C\u4ee3\u7801\">Swift\u8c03\u7528C\u4ee3\u7801<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.v.ixriver.com\/?p=518\/#Swift%E8%B0%83%E7%94%A8C%E4%BB%A3%E7%A0%81-2\" title=\"Swift\u8c03\u7528C++\u4ee3\u7801\">Swift\u8c03\u7528C++\u4ee3\u7801<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Swift%E8%B0%83%E7%94%A8C%E4%BB%A3%E7%A0%81\"><\/span>Swift\u8c03\u7528C\u4ee3\u7801<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>File-&gt;New-&gt;Files \u9009\u62e9\u65b0\u5efaC \u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u9009\u62e9hello_c\uff0c\u9996\u6b21\u521b\u5efaC\u6587\u4ef6\uff0c\u4f1a\u81ea\u52a8\u63d0\u793a\u521b\u5efaBridging Header<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"728\" src=\"https:\/\/www.ixriver.com\/wp-content\/uploads\/2024\/06\/image-1024x728.png\" alt=\"\" class=\"wp-image-521\" srcset=\"https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-1024x728.png 1024w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-300x213.png 300w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-768x546.png 768w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-1536x1092.png 1536w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image.png 1562w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"728\" src=\"https:\/\/www.ixriver.com\/wp-content\/uploads\/2024\/06\/image-2-1024x728.png\" alt=\"\" class=\"wp-image-525\" srcset=\"https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-2-1024x728.png 1024w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-2-300x213.png 300w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-2-768x546.png 768w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-2.png 1466w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ hello_c.h\n\n#ifndef hello_c_h\n#define hello_c_h\n\n#include &lt;stdio.h&gt;\n\nint AddInt(int a, int b);\n\n#endif \/* hello_c_h *\/\n\n\/\/ hello_c.c\n#include \"hello_c.h\"\n\nint AddInt(int a, int b) {\n  printf(\"sum is %d\", a+b);\n  return a+b;\n}\n\n\/\/ mix_demo-Bridging-Header.h\n#import \"hello_c.h\"\n\n\/\/ **.swift\n\/\/ \u76f4\u63a5\u8c03\u7528\u5373\u53ef\uff0c\u8fd0\u884c\u8f93\u51fa\"sum is 15\"\nvar sum = AddInt(7, 8)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Swift%E8%B0%83%E7%94%A8C%E4%BB%A3%E7%A0%81-2\"><\/span>Swift\u8c03\u7528C++\u4ee3\u7801<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>File->New->Files \u9009\u62e9\u65b0\u5efaCocoa Touch Class\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u8f93\u5165HelloWrapper<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"727\" src=\"https:\/\/www.ixriver.com\/wp-content\/uploads\/2024\/06\/image-3-1024x727.png\" alt=\"\" class=\"wp-image-529\" srcset=\"https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-3-1024x727.png 1024w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-3-300x213.png 300w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-3-768x545.png 768w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-3.png 1456w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"723\" src=\"https:\/\/www.ixriver.com\/wp-content\/uploads\/2024\/06\/image-4-1024x723.png\" alt=\"\" class=\"wp-image-530\" srcset=\"https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-4-1024x723.png 1024w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-4-300x212.png 300w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-4-768x542.png 768w, https:\/\/www.v.ixriver.com\/wp-content\/uploads\/2024\/06\/image-4.png 1470w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ mix_demo-Bridging-Header.h\n#import \"HelloWrapper.h\"\n\n\/\/ HelloWrapper.h\n#import &lt;Foundation\/Foundation.h&gt;\n\nNS_ASSUME_NONNULL_BEGIN\n\n@interface HelloWrapper : NSObject\n\n-(NSString*) getVersion;\n\n@end\n\nNS_ASSUME_NONNULL_END\n\n\/\/ \u4fee\u6539HelloWrapper.m\u6587\u4ef6\u4e3aHelloWrapper.mm\n#import \"HelloWrapper.h\"\n#include &lt;string&gt;\n\n\/\/ \u6dfb\u52a0\u7684c++\u51fd\u6570\nstd::string GetVersion() {\n  return \"version from cpp\";\n}\n\n@implementation HelloWrapper\n\n-(NSString*) getVersion {\n  std::string version = GetVersion();\n  NSLog(@\"%s\", version.c_str());\n  return &#91;NSString stringWithUTF8String: version.c_str()];\n}\n\n@end\n\n\/\/ **.swift\n\/\/ \u76f4\u63a5\u8c03\u7528\u5373\u53ef\uff0c\u8fd0\u884c\u8f93\u51fa\"version from cpp\"\nvar version = HelloWrapper().getVersion()\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/github.com\/oc-sample\/mix_swift.git\">\u793a\u4f8b\u4ee3\u7801github\u5730\u5740<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>swift\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528C\u4ee3\u7801\uff0c\u4f46\u65e0\u6cd5\u76f4\u63a5\u8c03\u7528C++\u4ee3\u7801\uff0c\u8c03\u7528C++\u4ee3\u7801\u9700\u8981\u901a\u8fc7OC\u4e2d\u8f6c Swift\u8c03\u7528C\u4ee3\u7801  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"blocksy_meta":[],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"post_excerpt_stackable":"<p>swift\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528C\u4ee3\u7801\uff0c\u4f46\u65e0\u6cd5\u76f4\u63a5\u8c03\u7528C++\u4ee3\u7801\uff0c\u8c03\u7528C++\u4ee3\u7801\u9700\u8981\u901a\u8fc7OC\u4e2d\u8f6c \u76ee\u5f55 Toggle Swift\u8c03\u7528C\u4ee3\u7801Swift\u8c03\u7528C++\u4ee3\u7801 Swift\u8c03\u7528C\u4ee3\u7801 File-&gt;New&hellip;<\/p>\n","category_list":"<a href=\"https:\/\/www.v.ixriver.com\/?cat=22\" rel=\"category\">ios<\/a>","author_info":{"name":"admin","url":"https:\/\/www.v.ixriver.com\/?author=1"},"comments_num":"0 comments","_links":{"self":[{"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts\/518"}],"collection":[{"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=518"}],"version-history":[{"count":6,"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":533,"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=\/wp\/v2\/posts\/518\/revisions\/533"}],"wp:attachment":[{"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.v.ixriver.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}