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

推荐订阅源

T
Troy Hunt's Blog
P
Proofpoint News Feed
Help Net Security
Help Net Security
T
The Exploit Database - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News: Ask HN
Hacker News: Ask HN
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Hacker News - Newest:
Hacker News - Newest: "LLM"
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
S
Secure Thoughts
爱范儿
爱范儿
Jina AI
Jina AI
H
Heimdal Security Blog
量子位
罗磊的独立博客
人人都是产品经理
人人都是产品经理
T
Threat Research - Cisco Blogs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Cisco Talos Blog
Cisco Talos Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MyScale Blog
MyScale Blog
T
Tor Project blog
博客园_首页
T
Tenable Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Latest news
Latest news
AWS News Blog
AWS News Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
Last Week in AI
Last Week in AI
V
Visual Studio Blog
The Hacker News
The Hacker News
I
Intezer
L
LINUX DO - 最新话题
L
LangChain Blog
W
WeLiveSecurity

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