自从Xcode10
正式版发布以后, 先吃螃蟹的朋友赞叹了Dark Mode
的惊艳, 同时也报告说, 打包上传到APPStroe
后, 监测到线上 iOS9.3
设备大面积crash的记录, 最后被证明是Xcode10
的问题.swift
出于此缘由考虑, 我便一直在使用Xcode9.4.1
及 Swift4
进行混编项目的开发.session
然而往往使用低版本的Xcode
打包上传APPStore
时, 就会收到苹果的官方警告邮件 函数
如邮件内容所示, 到2019
年的3
月份便不能够再使用低版本Xcode
进行打包上传操做了.ui
因而, 我只好开始了迁移之路.spa
使用当前 Xcode10.1(10B61)
打开以后, 在Build Setting
中搜索 Swift Language Version
, 将对应的值改成Swift 4.2
,而后开始编译, 此时会出现很是多的Error
, 多为ABI
的变更, 根据提示进行修改便可.code
然而有一处例外:
当项目中经过Swift
使用了 AVAudioSession setCategory
这个方法时, 会被告知方法在Swift
中不可用.跳转才发现 API
已经变化成了cdn
/* set session category and mode with options */
@available(iOS 10.0, *)
open func setCategory(_ category: AVAudioSession.Category, mode: AVAudioSession.Mode, options: AVAudioSession.CategoryOptions = []) throws
复制代码
为了兼容低版本, 思来想去, 比较合适的方案就是使用OC编写一个AVAudioSession
的分类用来桥接:blog
// AVAudioSession+Swift.h:
@import AVFoundation;
NS_ASSUME_NONNULL_BEGIN
@interface AVAudioSession (Swift)
- (BOOL)swift_setCategory:(AVAudioSessionCategory)category error:(NSError **)outError NS_SWIFT_NAME(setCategory(_:));
- (BOOL)swift_setCategory:(AVAudioSessionCategory)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError NS_SWIFT_NAME(setCategory(_:options:));
@end
NS_ASSUME_NONNULL_END
// AVAudioSession+Swift.m:
#import "AVAudioSession+Swift.h"
@implementation AVAudioSession (Swift)
- (BOOL)swift_setCategory:(AVAudioSessionCategory)category error:(NSError **)outError {
return [self setCategory:category error:outError];
}
- (BOOL)swift_setCategory:(AVAudioSessionCategory)category withOptions:(AVAudioSessionCategoryOptions)options error:(NSError **)outError {
return [self setCategory:category withOptions:options error:outError];
}
@end
复制代码
而后在你项目的<#target_name#>-Bridging-Header.h
中import
这个分类:继承
#import "AVAudioSession+Swift.h"
复制代码
而后就能够像以前同样调用了.开发
try AVAudioSession.sharedInstance().setCategory(.playback)
复制代码
这类问题分两种, 一种是module
是其余的target
, 如Pods
中的, 另外一种是 module
是本身的建立的target
.
第一种 只须要将Error
所指向的三方库更新到最新版本便可, Xcode10
已经发布了快半年了, 这些问题以前也有, 半年的时间, 基本上流行的三方库都已经适配了Swift4.2
而第二种比较棘手, 通常都是因为 Error
中的Class
所指向的类, 本身是Swift
类, 可是却继承自Objective-C
声明编写的类. 我在本身试过一些微调以后发现于事无补, 因此只好将出错的类使用Objective-C
重写, 而后在桥接文件中引入, 好在报错的很少, 没用费太多力气.
迁移完成后 项目跑起来时, 还会出现不少奇怪的问题. 好比调用了某个方法A 会报unrecognize selector *
, 以及莫名的函数调用
, 如:
除开以上的问题, 项目编译时还会在Pods
引用的第三方的类中报ABI须要修改的Error
, 这时, 只须要找到队形的Target
, 在其Build Settings
中修改 Swift Language Version
为其对应版本便可.