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

推荐订阅源

Project Zero
Project Zero
GbyAI
GbyAI
Y
Y Combinator Blog
Google DeepMind News
Google DeepMind News
小众软件
小众软件
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
IT之家
IT之家
F
Fortinet All Blogs
博客园 - 【当耐特】
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Schneier on Security
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
O
OpenAI News
V
V2EX
博客园 - 三生石上(FineUI控件)
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Jina AI
Jina AI
H
Help Net Security
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tenable Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cisco Talos Blog
Cisco Talos Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Hacker News: Ask HN
Hacker News: Ask HN
S
Security @ Cisco Blogs
S
Securelist
T
The Blog of Author Tim Ferriss
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
雷峰网
雷峰网
V2EX - 技术
V2EX - 技术

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