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

推荐订阅源

Recorded Future
Recorded Future
小众软件
小众软件
C
Check Point Blog
MyScale Blog
MyScale Blog
V
Visual Studio Blog
博客园_首页
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
腾讯CDC
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
量子位
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
博客园 - 叶小钗
H
Help Net Security
T
The Blog of Author Tim Ferriss
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
美团技术团队
有赞技术团队
有赞技术团队
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
M
MIT News - Artificial intelligence
S
Secure Thoughts
U
Unit 42
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Announcements
Recent Announcements
F
Full Disclosure
The GitHub Blog
The GitHub Blog
V2EX - 技术
V2EX - 技术
D
DataBreaches.Net
Webroot Blog
Webroot Blog
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
aimingoo的专栏
aimingoo的专栏
W
WeLiveSecurity
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
V2EX
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 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 的混编。