{"id":318,"date":"2020-02-09T22:57:22","date_gmt":"2020-02-09T14:57:22","guid":{"rendered":"http:\/\/1989lifejourney.com\/?p=318"},"modified":"2020-02-09T22:57:22","modified_gmt":"2020-02-09T14:57:22","slug":"ios-importing-swift-into-objective-c","status":"publish","type":"post","link":"https:\/\/1989lifejourney.com\/?p=318","title":{"rendered":"IOS &#8211; Importing Swift into Objective-C"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4e00\u822c\u5e38\u898b\u7684\u90fd\u662f<strong>Importing Objective-C into Swift<\/strong> (ex: Swift base project\u4f7f\u7528 Objective-C library),\u4f46\u9019\u6b21\u7684\u5c08\u6848\u9700\u6c42,\u9700\u8981<strong>Importing Swift into Objective-C<\/strong>(i.e \u5728Objective-C\u4e0b\u4f7f\u7528Swift library)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b98\u65b9\u6709\u6587\u4ef6\u8aaa\u660e,\u4f46\u662f\u5c11\u4e86\u4e00\u4e9b\u95dc\u9375\u7684\u90e8\u5206(Note 1),\u4ee5\u4e0b\u63cf\u8ff0\u6574\u500b\u6d41\u7a0b:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Importing Swift into Objective-C<\/strong> \u6d41\u7a0b:<\/h3>\n\n\n\n<figure class=\"wp-block-embed is-type-rich\"><div class=\"wp-block-embed__wrapper\">\n<a href=\"https:\/\/docs-assets.developer.apple.com\/published\/15bb0c3f78\/fd9cb730-e33c-43e0-b518-a990c745d57d.png\" rel=\"nofollow\">https:\/\/docs-assets.developer.apple.com\/published\/15bb0c3f78\/fd9cb730-e33c-43e0-b518-a990c745d57d.png<\/a>\n<\/div><figcaption>Import Swift into Objective-C\u67b6\u69cb\u5716 (from \u5b98\u65b9\u6587\u4ef6)<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u8655\u7406\u6d41\u7a0b\u5c31\u662f\u7528Xcode\u958b\u555fObjective-C project,\u4e26\u65b0\u589e\u6216\u62d6\u79fb\u4e00\u500bSwift\u6a94,\u7cfb\u7d71\u5c31\u6703\u81ea\u52d5\u5e6b\u6211\u5011\u7522\u751f<strong>ProductModuleName-Swift.h<\/strong>\u3002\u7136\u5f8c\u6211\u5011\u5c31\u60f3\u8981\u4f7f\u7528Swift Code\u7684Objective-C\u7684.m\u6216.h\u6a94\u53bbimport <strong>ProductModuleName-Swift.h<\/strong>,\u6211\u5011\u5c31\u53ef\u4ee5\u5728Objective-C\u6a94\u4e2d\u53bb\u4f7f\u7528Swift\u7684code\u3002<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\u5728.m\u6a94\u4e2d\u548c.h\u6a94\u4e2d\u53bbimport <strong>ProductModuleName-Swift.h<\/strong>\u7684\u5beb\u6cd5\u8981\u4e0d\u4e00\u6a23\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5728.m\u6a94\u4e2d\u7528 <strong>#import &lt;ProductModuleName-Swift.h&gt;<\/strong><\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5728.h\u6a94\u4e2d\u7528 <strong>@class MySwiftClass;<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">.h\u6a94\u4e2d\u4e0d\u7528#import\u662f\u56e0\u70ba\u8981\u907f\u514dcyclical reference,\u56e0\u70ba\u6211\u5011\u7684Swift\u6a94\u548cObjective-C\u90fd\u662f\u5728\u540c\u4e00\u500bbuild target\u4e2d,\u5982\u679c\u7528import,\u5c31\u6703\u5c0e\u81f4cyclical reference\u3002<\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Note:<\/h4>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>ProductModuleName-Swift.h<\/strong>\u9019\u500b\u6a94\u6848\u5728\u6211\u5011\u7684Objective-C project\u4e0d\u6703\u770b\u5230,\u4e14\u5b98\u65b9\u6587\u4ef6\u672a\u63d0\u5230\u5982\u4f55\u627e\u5230\u9019\u500b<strong>ProductModuleName-Swift.h<\/strong>\u3002\u8981\u770b\u5230\u9019\u500b\u6a94\u6848,\u53ea\u8981\u6211\u5011\u5148\u5728Objective-C project\u4e2d\u4efb\u4e00\u500b\u6a94\u6848\u64b0\u5beb <strong>#import &lt;ProductModuleName-Swift.h&gt;<\/strong>,\u7136\u5f8c\u6211\u5011\u6309\u4e0bcommand\u4e26\u7528\u6ed1\u9f20\u9ede\u64ca<strong>ProductModuleName-Swift.h<\/strong> (p.s \u540c\u6211\u5011\u67e5\u770bAPI definition\u7684\u65b9\u6cd5),\u9019\u6a23\u6211\u5011\u5c31\u53ef\u4ee5\u770b\u5230<strong>ProductModuleName-Swift.h<\/strong>\u5167\u5bb9<\/li><li>\u6211\u5011\u53ef\u4ee5\u900f\u904e\u9019\u500b<strong>ProductModuleName-Swift.h<\/strong>\u4f86\u6aa2\u67e5\u6211\u5011\u7684Swift code\u662f\u5426\u6709\u6210\u529fexpose to Objective-C\u6a94\u3002 (\u7bc4\u4f8bproject\u540d\u7a31\u662fTestObjc,\u5373\u70baProductModuleName)<ol><li>\u5982\u679c\u6709\u6210\u529f\u7684\u8a71\uff0c\u6703\u5728<strong>ProductModuleName-Swift.h<\/strong>\u770b\u5230\u5982\u4e0b\u7684\u7a0b\u5f0f\u78bc:<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"505\" data-attachment-id=\"334\" data-permalink=\"https:\/\/1989lifejourney.com\/?attachment_id=334\" data-orig-file=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/222.png?fit=1544%2C780&amp;ssl=1\" data-orig-size=\"1544,780\" data-comments-opened=\"0\" data-image-title=\"222\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/222.png?fit=1024%2C517&amp;ssl=1\" class=\"wp-image-334\" style=\"width:1000px;\" src=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/222.png?resize=1000%2C505&#038;ssl=1\" alt=\"undefined\"><\/li><li>\u5426\u5247\u6703\u770b\u5230\u5982\u4e0b\u7684\u7a0b\u5f0f\u78bc:  <img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"518\" data-attachment-id=\"335\" data-permalink=\"https:\/\/1989lifejourney.com\/?attachment_id=335\" data-orig-file=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/111.png?fit=1516%2C786&amp;ssl=1\" data-orig-size=\"1516,786\" data-comments-opened=\"0\" data-image-title=\"111\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/111.png?fit=1024%2C531&amp;ssl=1\" class=\"wp-image-335\" style=\"width:1000px;\" src=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/111.png?resize=1000%2C518&#038;ssl=1\" alt=\"undefined\"><\/li><\/ol><\/li><li><strong>ProductModuleName-Swift.h<\/strong>\u5167\u5bb9\u7121\u6cd5\u4fee\u6539,\u6240\u4ee5\u52d9\u5fc5\u4f9d\u7167<strong>Apple\u5b98\u65b9\u6587\u4ef6(Ref (1))<\/strong>\u7684\u898f\u5247\u53bb\u64b0\u5bebSwift code<\/li><li><strong>ProductModuleName-Swift.h<\/strong>\u9019\u500b\u81ea\u52d5\u7522\u751f\u7684\u6a94\u6848,\u5728Build setting\u4e2d,\u6709\u6b04\u4f4d\u53bb\u53c3\u8003\u9019\u500b\u6a94\u6848\u7684\u4f4d\u7f6e:<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"244\" data-attachment-id=\"333\" data-permalink=\"https:\/\/1989lifejourney.com\/?attachment_id=333\" data-orig-file=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/e89ea2e5b995e5bfabe785a7-2020-02-09-e4b88be58d8810.15.12.png?fit=1918%2C468&amp;ssl=1\" data-orig-size=\"1918,468\" data-comments-opened=\"0\" data-image-title=\"\u87a2\u5e55\u5feb\u7167 2020-02-09 \u4e0b\u534810.15.12\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/e89ea2e5b995e5bfabe785a7-2020-02-09-e4b88be58d8810.15.12.png?fit=1024%2C250&amp;ssl=1\" class=\"wp-image-333\" style=\"width:1000px;\" src=\"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2020\/02\/e89ea2e5b995e5bfabe785a7-2020-02-09-e4b88be58d8810.15.12.png?resize=1000%2C244&#038;ssl=1\" alt=\"undefined\"> <\/li><li>\u5176\u4ed6\u88dc\u5145\uff1a <strong>ProductModuleName-Swift.h<\/strong>\u662fImporting Swift to Objective-C\u9700\u8981\u7528\u7684<\/li><li>\u5176\u4ed6\u88dc\u5145\uff1a<strong> ProductModuleName-Bridging-Header.h<\/strong>\u662fImporting Objective-C to Swift\u9700\u8981\u7528\u7684<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9644\u4e0aSample Project,\u5176\u5167\u5bb9\u662f\u628a\u4e00\u500bDataModel.Swift\u6a94import\u5230\u4e00\u500bObjective-C project\u4e2d,\u5c0d\u9019\u500bDataModel\u9032\u884c\u521d\u59cb\u5316,\u4e26\u628adata\u7684\u503c\u5370\u51fa\u4f86:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">link: <a href=\"https:\/\/github.com\/tgnivekucn\/SwiftToObjC\">https:\/\/github.com\/tgnivekucn\/SwiftToObjC<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Ref:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">(1)\u5b98\u65b9\u6587\u4ef6: <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/imported_c_and_objective-c_apis\/importing_swift_into_objective-c\">https:\/\/developer.apple.com\/documentation\/swift\/imported_c_and_objective-c_apis\/importing_swift_into_objective-c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u822c\u5e38\u898b\u7684\u90fd\u662fImporting Objective-C into Swift (ex: Swift base [&hellip;]<\/p>\n","protected":false},"author":139426785,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_postgate_lock_override":"inherit","_postgate_lock_mode":"inherit","_postgate_preview_mode":"inherit","_postgate_preview_limit":0,"_postgate_product_ids":[],"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_wpcom_ai_launchpad_first_post":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[37324612],"tags":[],"post_folder":[],"class_list":["post-318","post","type-post","status-publish","format-standard","hentry","category-ios-development"],"jetpack_publicize_connections":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paR5vw-58","jetpack-related-posts":[{"id":6558,"url":"https:\/\/1989lifejourney.com\/?p=6558","url_meta":{"origin":318,"position":0},"title":"IOS Objective-C\u3001Swift\u53ca\u591a\u57f7\u884c\u7dd2\u548c\u8a18\u9ad4\u9ad4\u7ba1\u7406","author":"Kevin","date":"1 2 \u6708, 2023","format":false,"excerpt":"\u4ee5\u4e0b\u66f8\u76ee\u53c3\u8003\u81ea https:\/\/www.zhihu.com\/question\/28491905 (P\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3917,"url":"https:\/\/1989lifejourney.com\/?p=3917","url_meta":{"origin":318,"position":1},"title":"Objective-C\u548cSwift\u7684\u5171\u540c\u9ede\u8207\u4e0d\u540c\u9ede","author":"Kevin","date":"8 12 \u6708, 2021","format":false,"excerpt":"Objective-C\u548cSwift\u7684\u5171\u540c\u9ede\u8207\u4e0d\u540c\u9ede \u4ee5\u4e0b\u8cc7\u6599\u4f86\u81ea[ref]\u7db2\u9801\u5167\u5bb9\uff0c\u9019\u88e1\u7d14\u7cb9\u505a\u91cd\u9ede\u6574\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4899,"url":"https:\/\/1989lifejourney.com\/?p=4899","url_meta":{"origin":318,"position":2},"title":"AnyObject v.s. Any in Swift","author":"Kevin","date":"24 3 \u6708, 2022","format":false,"excerpt":"AnyObject AnyObject can represent an instance of a\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4982,"url":"https:\/\/1989lifejourney.com\/?p=4982","url_meta":{"origin":318,"position":3},"title":"Difference between value type and a reference type in iOS swift\ufffc","author":"Kevin","date":"29 3 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u8cc7\u6599\u7686\u53c3\u8003\u81eaRef1\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406: Definition of Value type \u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7606,"url":"https:\/\/1989lifejourney.com\/?p=7606","url_meta":{"origin":318,"position":4},"title":"ChatGPT\u6e2c\u8a66_11\uff08\u8a62\u554fsendmail third party libraries for Swift)","author":"Kevin","date":"31 3 \u6708, 2023","format":false,"excerpt":"\u554fChatGPT: ios swift recommend third-party librarie\u2026","rel":"","context":"\u5728\u300cChatGPT\u300d\u4e2d","block_context":{"text":"ChatGPT","link":"https:\/\/1989lifejourney.com\/?cat=760577830"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2023\/03\/e688aae59c96-2023-03-31-e4b88ae58d8810.21.33.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2023\/03\/e688aae59c96-2023-03-31-e4b88ae58d8810.21.33.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2023\/03\/e688aae59c96-2023-03-31-e4b88ae58d8810.21.33.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2023\/03\/e688aae59c96-2023-03-31-e4b88ae58d8810.21.33.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2023\/03\/e688aae59c96-2023-03-31-e4b88ae58d8810.21.33.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/1989lifejourney.com\/wp-content\/uploads\/2023\/03\/e688aae59c96-2023-03-31-e4b88ae58d8810.21.33.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":4009,"url":"https:\/\/1989lifejourney.com\/?p=4009","url_meta":{"origin":318,"position":5},"title":"Static vs Dynamic Dispatch in Swift","author":"Kevin","date":"6 1 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u5167\u5bb9\u53c3\u8003\u81ea[Ref]\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u6458\u8981\u6574\u7406: Method Dispatch\u5b9a\u7fa9: Method\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/users\/139426785"}],"replies":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=318"}],"version-history":[{"count":20,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/318\/revisions\/342"}],"wp:attachment":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=318"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fpost_folder&post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}