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

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
Jina AI
Jina AI
T
Tenable Blog
S
Securelist
S
Schneier on Security
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
C
Cisco Blogs
The Hacker News
The Hacker News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
O
OpenAI News
V
Vulnerabilities – Threatpost
V
Visual Studio Blog
Security Latest
Security Latest
T
Threatpost
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Hacker News: Front Page
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
I
InfoQ
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
Know Your Adversary
Know Your Adversary
Martin Fowler
Martin Fowler
Forbes - Security
Forbes - Security
F
Full Disclosure
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
V
V2EX
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Threat Research - Cisco Blogs
S
Secure Thoughts
量子位
博客园 - 【当耐特】

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