惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
量子位
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网
B
Blog RSS Feed
宝玉的分享
宝玉的分享
Security Latest
Security Latest
小众软件
小众软件
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
博客园_首页
美团技术团队
T
Tailwind CSS Blog
The Cloudflare Blog
爱范儿
爱范儿
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
V
Vulnerabilities – Threatpost
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
S
Securelist
阮一峰的网络日志
阮一峰的网络日志
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Schneier on Security
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Scott Helme
Scott Helme
S
SegmentFault 最新的问题
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Schneier on Security
Schneier on Security
Jina AI
Jina AI
N
News and Events Feed by Topic
C
Cisco Blogs
L
Lohrmann on Cybersecurity
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Cisco Talos Blog
Cisco Talos Blog
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
O
OpenAI News
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - super1250

技术支持网址 (URL) 填写的地址 通过Homebrew安装CocoaPods iOS游戏内购(记录官网链接) iOS的证书创建 Apple开发者账号注册 __swift_FORCE_LOAD_$_swiftCompatibility50 Validation failed (409) 拒审原因2.3.4 git生成ssh秘钥 project.pbxproj文件配置详解 录音功能iosSDK iOS内购支付 打包framwork 上架总结 Guideline 3.2.1 清理Cocoapods缓存mac flutter插件 lanh libarclite_iphonesimulator.a模拟器跑不起的问题SDK does not contain 'libarclite' at the path
在 Pod 库中同时使用 Swift 和 Objective-C
super1250 · 2025-04-17 · via 博客园 - super1250

1. 在 Pod 库中同时使用 Swift 和 Objective-C

情况一:Pod 库以 Swift 为主,需要调用 Objective-C 代码

  1. 创建桥接头文件

    • 在 Swift 项目中创建一个名为 YourPodName-Bridging-Header.h 的文件

    • 在该文件中导入需要暴露给 Swift 的 Objective-C 头文件

  2. 配置 Podspec

    Pod::Spec.new do |s|
      s.name = 'YourPodName'
      

情况二:Pod 库以 Objective-C 为主,需要调用 Swift 代码

  1. 创建模块映射文件

    • 确保你的 Pod 库有一个 modulemap 文件

    • 在 Swift 文件中使用 @import YourPodName; 导入 Objective-C 模块

  2. 配置 Podspec

    Pod::Spec.new do |s|
      s.name = 'YourPodName'
      

2. 主项目中使用混编的 Pod 库

  1. 如果 Pod 包含 Swift 代码

    • 确保主项目有一个 Swift 文件(即使空文件也行),这会自动创建桥接头文件

    • 在 Podfile 中添加 use_frameworks! 指令

    use_frameworks!
    
    target 'YourApp' do
      pod 'YourMixedPod'
    end
  2. 从 Swift 调用 Objective-C 代码

    • 通过桥接头文件自动暴露的 Objective-C 类可以直接在 Swift 中使用

  3. 从 Objective-C 调用 Swift 代码

    • 在 Objective-C 文件中导入自动生成的 Swift 头文件:

      #import <YourMixedPod/YourMixedPod-Swift.h>
    • 确保 Swift 类和方法标记为 @objc 或继承自 NSObject

3. 常见问题解决

  1. 头文件找不到

    • 检查桥接头文件路径是否正确

    • 确保 Pod 安装后头文件搜索路径包含正确路径

  2. Swift 类在 Objective-C 中不可见

    • 确保 Swift 类继承自 NSObject

    • 使用 @objc 标记需要暴露的属性和方法

    • 检查 YourPod-Swift.h 文件是否生成

  3. 模块映射问题

    • 如果出现模块相关错误,尝试在 podspec 中添加:

      s.module_map = 'Sources/module.modulemap'
  4. Swift 版本兼容性

    • 在 podspec 中明确指定 Swift 版本:

通过以上配置,你应该能够在 CocoaPods 库中成功实现 Swift 和 Objective-C 的混编。