iOS 在OC旧项目中使用Swift进行混编

iOS 在OC旧项目中使用Swift进行混编

1、创建桥接文件

​ 第一次在Swift创建OC文件,或者第一次OC创建Swift时,xcode会提示桥接,Creat Bridging Header即可,这个文件用于Swift调用OC文件,与OC调用Swift无关。

2、在TARGETS中设置Defines Module

​ TARGETS ->Build Settings -> Packaging 中 设置Defines Module为YES

3、如何使用
  • 在OC中调用swift:在需要使用swift文件的oc类中,添加头文件 #import "项目名称-Swift.h"
objectivec 复制代码
#import "TestOcSwitchSwift-Swift.h"
SwiftViewController * tmpVC = [[SwiftViewController alloc]init];
[tmpVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:tmpVC animated:YES completion:nil];
  • 在swift中调用oc:在桥接文件中引入要使用的oc类
objectivec 复制代码
在 TestOcSwitchSwift-Bridging-Header.h 中
#import "FirstViewController.h"
//在需要使用的地方直接调用。例如:在这里直接跳转页面
 let tmpVC = FirstViewController()
 tmpVC.modalPresentationStyle = .fullScreen
 present(tmpVC, animated: true, completion: nil)

相关推荐
m0_641031059 小时前
在选择iOS代签服务前,你必须了解的三大安全风险
ios
开开心心loky11 小时前
[iOS] push 和 present Controller 的区别
ui·ios·objective-c·cocoa
大熊猫侯佩11 小时前
10 个 Xcode 神技:哥谭开发者必学的 IDE 对抗术
xcode·swift·apple
HarderCoder12 小时前
Swift Package Command Plugin 实战:一键生成 Package 元数据
swift
白玉cfc16 小时前
【iOS】push,pop和present,dismiss
macos·ios·cocoa
低调小一17 小时前
Swift 语法学习指南 - 与 Kotlin 对比
微信·kotlin·swift
HarderCoder17 小时前
Swift Package Plugin 深度实战:从原理到落地,自动生成字体枚举
swift
低调小一17 小时前
iOS 开发入门指南-HelloWorld
ios
2501_9159184117 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
用户870568130451 天前
iOS 异步渲染:从 CALayer 切入的实现与优化
ios