update: 使用 XCFramework 做 swift 向前兼容
在经历了 abi stability
,
然后到了 swift 5.1
, 看到了这么一句话
With module stability it’s now possible to create and share binary frameworks that will work with future releases of Swift.
是 module stability
?
然后下了 Beta Xcode
试了一下, 不可以. 哈哈哈
Module compiled with Swift 5.1 cannot be imported by the Swift 5.1.2
上次求着那个 sdk
小公司帮忙 build
个新版用了半个月时间, xcode 11.1
到 xcode 11.2
才几天, 前面 build
出来的后面又用不了了, 哈哈哈.
偶然间看了一篇文章, ABI stability
稳定了, oc
一直是稳定的, 只是 swiftmodule
一直有问题.
所以把 swift framework
用 oc framework
包裹起来不就可以向后兼容了吗?
试了一下, 可以的, 开心!
下面说一下具体实现:
关于 framework
之间的相互引用请看: Framework 嵌套与依赖
- 我手中有一个 Swift 5.1 编译出来的动态
framework
- 但这个
framwork
中所有代码没有暴露给oc
根据这两点: - 首先把原来的
framework
再用swift
包裹一层, 但使用@objc
把接口暴露出来 - 再使用
oc
包裹一个framework
丢给当前项目使用
具体实现
-
swift
暴露给oc
原来方法
@objc public class FWrapper: NSObject {
@objc public static func version() -> String {
return SDKWrapper.versionString()
}
}
-
oc
包裹原来方法
#import "Wrapper.h"
@import FWrapper;
@implementation Wrapper
- (NSString *)version {
return [FWrapper new].version;
}
@end
然后在原来的项目中同时拖入以上三个 framework
, 大功告成
为什么要拖入三个 framework
, Framework 嵌套与依赖, 具体实现一次就明白了
源码
网友评论