XCode10 swift4.2 适配遇到的坑
以下是2018年10月23日更新
经过大约一个月的时间的适配,项目正式使用XCode10(以下简称为10 or XC10)大部分库都升级为Swift4.2(以下简称为 4.2 or S4.2),下面是适配过程中遇到的一些坑。
1. Swift4、Swift4.2混编
如果你对项目是小的独立项目,完全可以全部升级为4.2,你可以略过第一条;如果你依赖了一些第三方的库,且没有升级4.2,你可以继续看这一条。目前测试的结果来看,Swift4 和 S4.2的混编没有什么大的问题,如果你是通过cocoapod引入的可以在Podfile中加入如下代码:
swift_41_pod_targets = ['your_target_name']
post_install do |installer|
installer.pods_project.targets.each do |target|
if swift_41_pod_targets.include?(target.name)
target.build_configurations.each do |config|
config.build_settings['SWIFT_VERSION'] = '4.1'
end
end
end
end
2. NSDataAsset
升级XC10和S.2之前,项目里面有些对 'NSDataAsset' 的错误使用: 用‘NSDataAsset’读ImageAsset中的图片,这个是不正确的,但是却可以工作,这次升级修复了这个BUG。
正确的做法使用'DataAsset',然后才可以用‘NSDataAsset’读取数据,我由于不够认真且经验不足还以为是个BUG,给Apple提了个BUG。。。[捂脸]
3. 第三方库的重命名 typealias
为了方便的适配S4.2对UIKit中的重命名,有些第三方使用typealias对一些类型进行了重命名,以 RxSwift 为例子,RxSwift中就有如下代码:
#if swift(>=4.2)
public typealias UIControlEvents = UIControl.Event private
#endif
这会导致一些重命名的类型即使不改也不会报错,但是一旦去掉了对某个库的依赖就会引入新的问题。
4.Delegate 的 Access Modifier
在升级S4.2过程中,XC偶尔会提示需要给某些Delegate方法添加 private修饰符,不要为了消除这个⚠️添加private,可能会导致Delegate永远不被调到;另外,如果是一个public或者open的class,协议方法记得也要加上public,否则会出一样的问题,具体原因我还在测试,但是现象是这样的,有新的见解欢迎评论区讨论。
5. 机型适配问题,iPhone XS Max字体变大
有些同事遇到XC9构建的安装包在iPhone XS Max上会有字体变大的情况,这个貌似是普遍现象,微信也有,使用XC10构建安装包可以解决这个问题,但是会遇到问题6
6. iOS9.3以下系统Crash率飙升
使用XC10构建安装包可以解决问题5,但是iOS9.3以下的系统Crash到让你怀疑人生
以下是2018年9月18日内容
AVAudioSession.sharedInstance().setCategory()
disappeared
Swift 4.2 中 iOS10以下不能用 AVAudioSession.sharedInstance() setCategory
可选方案:
- 使用OC实现该部分,然后使用Swift调用
- 放弃 iOS9用户体验
do {
if #available(iOS 11.0, *) {
try audioSession.setCategory(.playback, mode: .default, policy: .longForm, options: [])
} else if #available(iOS 10.0, *) {
try audioSession.setCategory(.playback, mode: .default, options: [])
} else {
// Compiler error: 'setCategory' is unavailable in Swift
try audioSession.setCategory(AVAudioSession.Category.playback)
}
} catch let error {
print("Unable to configure audio sesson category: \(error)")
}
NSUnderlineStyle(.patternSolid、.none)
disappeared
可选方案:
- .none
mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.none.rawValue, range: range)
^~~~~ 'none' is unavailable: use [] to construct an empty option set
Wrong: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: [], range: range)
Right: mutableAttributedString.addAttribute(NSAttributedString.Key.underlineStyle, value: 0, range: range)
使用 CTUnderlineStyleModifiers
// 没有测试
NSUnderlineStyle.init(rawValue: Int(CTUnderlineStyleModifiers.patternSolid.rawValue))使用其他默认值
下面是Rename操作
UIKit
Swift4/UIKit
UITableViewCell
| Swift 4 | Swift 4.2 |
|---|---|
| UITableViewCellStyle | UITableViewCell.CellStyle |
UIEvent
| Swift 4 | Swift 4.2 |
|---|---|
| UIEventSubtype | UIEvent.EventSubtype |
UITableView
| Swift 4 | Swift 4.2 |
|---|---|
| UITableViewScrollPosition | UITableView.ScrollPosition |
| UITableViewAutomaticDimension | UITableView.automaticDimension |
| UITableViewCellEditingStyle | UITableViewCell.EditingStyle |
| UITableViewRowAnimation | UITableView.RowAnimation |
| UITableViewStyle | UITableView.Style |
| UITableViewCellAccessoryType | UITableViewCell.AccessoryType |
UIControl
| Swift 4 | Swift 4.2 |
|---|---|
| UIControlEvents | UIControl.Event |
UIWindow
| Swift 4 | Swift 4.2 |
|---|---|
| UIWindowLevelAlert | UIWindow.Level.alert |
| UIKeyboardFrameEndUserInfoKey | UIResponder.keyboardFrameEndUserInfoKey |
| UIKeyboardFrameBeginUserInfoKey | UIResponder.keyboardFrameBeginUserInfoKey |
| UIKeyboardAnimationDurationUserInfoKey | UIResponder.keyboardAnimationDurationUserInfoKey |
| UIKeyboardAnimationCurveUserInfoKey | UIResponder.keyboardAnimationCurveUserInfoKey |
| UIKeyboardIsLocalUserInfoKey | UIResponder.keyboardIsLocalUserInfoKey |
| UIWindowDidBecomeVisible | UIWindow.didBecomeVisibleNotification |
| UIWindowDidBecomeHidden | UIWindow.didBecomeHiddenNotification |
| UIWindowDidBecomeKey | UIWindow.didBecomeKeyNotification |
| UIWindowDidResignKey | UIWindow.didResignKeyNotification |
| UIKeyboardWillShow | UIResponder.keyboardWillShowNotification |
| UIKeyboardDidShow | UIResponder.keyboardDidShowNotification |
| UIKeyboardWillHide | UIResponder.keyboardWillHideNotification |
| UIKeyboardDidHide | UIResponder.keyboardDidHideNotification |
UIViewController
| Swift 4 | Swift 4.2 |
|---|---|
| open func addChildViewController(_ childController: UIViewController) | open func addChild(_ childController: UIViewController) |
| open func willMove(toParentViewController parent: UIViewController?) | open func willMove(toParent parent: UIViewController?) |
| open func didMove(toParentViewController parent: UIViewController?) | open func didMove(toParent parent: UIViewController?) |
| open func removeFromParentViewController() | open func removeFromParent() |
UIActivity
| Swift 4 | Swift 4.2 |
|---|---|
| UIActivityType | UIActivity.ActivityType |
UIActivityIndicatorView
| Swift 4 | Swift 4.2 |
|---|---|
| activityIndicator.activityIndicatorViewStyle | activityIndicator.style |
UIAlertController
| Swift 4 | Swift 4.2 |
|---|---|
| UIAlertActionStyle | UIAlertAction.Style |
| UIAlertControllerStyle | UIAlertController.Style |
UIPageViewController
| Swift 4 | Swift 4.2 |
|---|---|
| UIPageViewControllerNavigationDirection | UIPageViewController.NavigationDirection |
| UIPageViewControllerSpineLocation | UIPageViewController.SpineLocation |
| UIPageViewControllerNavigationOrientation | UIPageViewController.NavigationOrientation |
| UIPageViewControllerTransitionStyle | UIPageViewController.TransitionStyle |
| UIPageViewControllerOptionsKey | UIPageViewController.OptionsKey |
UINavigationController
| Swift 4 | Swift 4.2 |
|---|---|
| UINavigationControllerOperation | UINavigationController.Operation |
UIGestureRecognizer
| Swift 4 | Swift 4.2 |
|---|---|
| UIGestureRecognizerStatePossible | UIGestureRecognizer.State.possible |
| UIGestureRecognizerStateBegan | UIGestureRecognizer.State.began |
| UIGestureRecognizerStateChanged | UIGestureRecognizer.State.changed |
| UIGestureRecognizerStateEnded | UIGestureRecognizer.State.ended |
| UIGestureRecognizerStateCancelled | UIGestureRecognizer.State.cancelled |
| UIGestureRecognizerStateFailed | UIGestureRecognizer.State.failed |
| UIGestureRecognizerStateRecognized | UIGestureRecognizer.State.recognized |
NSLayoutFormat
| Swift 4 | Swift 4.2 |
|---|---|
| NSLayoutFormatOptions | NSLayoutConstraint.FormatOptions |
UIEdgeInsets
| Swift 4 | Swift 4.2 |
|---|---|
| public func UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsets | UIEdgeInsets(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) |
| public func UIEdgeInsetsInsetRect(_ rect: CGRect, _ insets: UIEdgeInsets) -> CGRect | public func inset(by insets: UIEdgeInsets) -> CGRect |
UIFontDescriptor
| Swift 4 | Swift 4.2 |
|---|---|
| UIFontDescriptorSymbolicTraits | UIFontDescriptor.SymbolicTraits |
UIImage
| Swift 4 | Swift 4.2 |
|---|---|
UIKIT_EXTERN NSData * __nullable UIImagePNGRepresentation(UIImage * __nonnull image); |
public func pngData() -> Data? |
NSData * __nullable UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality); |
public func jpegData(compressionQuality: CGFloat) -> Data? |
UIApplication
| Swift 4 | Swift 4.2 |
|---|---|
| UIApplicationDidEnterBackground | UIApplication.didEnterBackgroundNotification |
| UIApplicationWillEnterForeground | UIApplication.willEnterForegroundNotification |
| UIApplicationDidFinishLaunching | UIApplication.didFinishLaunchingNotification |
| UIApplicationDidBecomeActive | UIApplication.didBecomeActiveNotification |
| UIApplicationWillResignActive | UIApplication.willResignActiveNotification |
| UIApplicationDidReceiveMemoryWarning | UIApplication.didReceiveMemoryWarningNotification |
| UIApplicationWillTerminate | UIApplication.willTerminateNotification |
| UIApplicationSignificantTimeChange | UIApplication.significantTimeChangeNotification |
| UIApplicationWillChangeStatusBarOrientation | UIApplication.willChangeStatusBarOrientationNotification |
| UIApplicationDidChangeStatusBarOrientation | UIApplication.didChangeStatusBarOrientationNotification |
| UIApplicationDidChangeStatusBarFrame | UIApplication.didChangeStatusBarFrameNotification |
| UIApplicationBackgroundRefreshStatusDidChange | UIApplication.backgroundRefreshStatusDidChangeNotification |
| UIApplicationProtectedDataWillBecomeUnavailable | UIApplication.protectedDataWillBecomeUnavailableNotification |
| UIApplicationProtectedDataDidBecomeAvailable | UIApplication.protectedDataDidBecomeAvailableNotification |
| UIApplicationUserDidTakeScreenshot | UIApplication.userDidTakeScreenshotNotification |
| UIApplicationOpenSettingsURLString | UIApplication.openSettingsURLString |
| UIApplicationLaunchOptionsKey | UIApplication.LaunchOptionsKey |
| UIInterfaceOrientationIsLandscape() | UIApplication.shared.statusBarOrientation.isLandscape |
UIView
| Swift 4 | Swift 4.2 |
|---|---|
func bringSubview(toFront view: UIView) |
func bringSubviewToFront(_ view: UIView) |
| UIViewAnimationOptions | UIView.AnimationOptions() |
Foundation
NSAttributedString
| Swift 4 | Swift 4.2 |
|---|---|
| NSAttributedStringKey | NSAttributedString.Key |
QuartzCore
CAShapeLayer
| Swift 4 | Swift 4.2 |
|---|---|
| kCALineCapRound | CAShapeLayerLineCap.round |
| kCALineCapButt | CAShapeLayerLineCap.butt |
| kCALineCapSquare | CAShapeLayerLineCap.square |
| kCALineJoinMiter | CAShapeLayerLineJoin.miter |
| kCALineJoinRound | CAShapeLayerLineJoin.round |
| kCALineJoinBevel | CAShapeLayerLineJoin.bevel |
| kCAFillRuleNonZero | CAShapeLayerFillRule.nonZero |
| kCAFillRuleEvenOdd | CAShapeLayerFillRule.evenOdd |
参考资料
XCode10 swift4.2 适配遇到的坑的更多相关文章
- iOS11 与 iPhone X适配的那些坑(持更中...)
目录 问题列表 1.适配iPhoneX 屏幕原则 2.适配过程一些常量的设置 3..iPhone X 上运行有黑色区域问题 4.iOS11导航栏适配 5.出现UIScrollview 漂移问题(基本都 ...
- Xcode7.2与iOS9之坑 (持续更新)
GitHub地址 前几天升级OS X EI Capitan 10.11.1, 以及Xcode7.1,正好赶上公司新产品上线,要做iOS9的适配,遇到各种坑,各种查资料,随之记录总结一下遇到的坑. 先说 ...
- Android 文章合集 200+ 篇
code小生 一个专注大前端领域的技术平台 公众号回复Android加入安卓技术群 镇楼 2017 文章合集 2017 年度文章分类整理 下面是 2018 年公众号所发表的文章分类整理 面经 一年经验 ...
- 整理iOS9适配中出现的坑(图文)
原文: http://www.cnblogs.com/dsxniubility/p/4821184.html 整理iOS9适配中出现的坑(图文) 本文主要是说一些iOS9适配中出现的坑,如果只是要 ...
- 整理 iOS 9 适配中出现的坑(图文)(转)
作者:董铂然 本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着 ...
- 腾讯优测-优社区干货精选 | 那些年,我们在Android机型适配上遇到的坑之Camera拍照时快门咔嚓声
文/腾讯优测研发工程师 吴宇焕 优测小优有话说: android机型适配的坑自然是不少,不想掉坑快来优测优社区~ 现在Android手机一般都会带有照相功能,有很多朋友就发现手机照相时快门声音很响,想 ...
- 整理 iOS 9 适配中出现的坑
本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着有用户陆续升级 ...
- 整理 iOS 9 适配中出现的坑(图文)
作者:董铂然 授权本站转载. 本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iO ...
- 【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!
一.写在前面 前几天也是分享了一些学习必备干货(还没关注的,赶紧入坑:传送门),也好久没有与大家探讨技术方案了,心里也是挺痒痒的,这不,一有点闲暇之时,就迫不及待把最近测出来的坑分享给大家. 提起An ...
随机推荐
- BUPT复试专题—最值问题(2013计院)
题目描述 给出N个数,求出这N个数中最大值和次大值.注意这里的次大值必须严格小于最大值.输入保证N个数中至少存在两个不同的数. 输入格式 第一行为测试数据的组数T(T≤20).请注意,任意两组测试数据 ...
- PS 图层后面有索引两字怎么办
ps中图层后面有索引两字的怎么把它拖进别的图中?或怎么把索引去掉? 悬赏分:0 | 解决时间:2010-11-5 08:58 | 提问者:jk500pk 最佳答案 图像--模式 把索引颜色模式改成RG ...
- fetch 函数分装
1.fetch /** * 封装 fetch */ import { hashHistory } from 'react-router'; export default function reques ...
- 1.shell编程之变量的高级用法
1.1.变量替换 变量替换的六种形式 实例:非贪婪和贪婪的区别 从头部删除 [root@VM_0_9_centos shell_learn]# var_1="i love you,do yo ...
- 拒绝干扰 解决Wi-Fi的最大问题
本文转载至:http://www.ciotimes.com/net/rdjs/WI-FI/201006301920.html 射频干扰英文:RFI,(Radio Frequency Interfere ...
- linux 标准i2c接口(一)
一:I2C设备操作方式: 1. 应用程序操作法:i2c的设备的驱动可以直接利用linux内核提供的i2c-dev.c文件提供的ioctl函数接口在应用层实现对i2c设备的读写,但是在应用层使用ioc ...
- 设计模式学习笔记——State状态模式
从一个类中,将有关状态的处理分离出来,独立成类,并面向接口编程.作用是可以简化代码,避免过多的条件判断:if-else-
- Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP
D. The Bakery Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...
- CentOS笔记-系统概述
Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 当计算机打开电源后,首先是BIOS开机自检,按照BIOS中设置的启动设备(通常是硬盘)来启动. 操作系统接管 ...
- hive增加Update、Delete支持
一.配置hive-site.xml二.建表三.操作四.总结 一.配置hive-site.xml CDH版本先进入Hive配置页 选择高级,找到hive-site.xml 的 Hive 客户端高级配置代 ...