2019年5月11日優(yōu)化顯示錯(cuò)誤
2019年4月26日更新部分說明
以下是2018年10月23日更新
經(jīng)過大約一個(gè)月的時(shí)間的適配,項(xiàng)目正式使用XCode10大部分庫都升級為Swift4.2踩麦,下面是適配過程中遇到的一些坑棱貌。
1. Swift4、Swift4.2混編
如果你對項(xiàng)目是小的獨(dú)立項(xiàng)目,完全可以全部升級為Swift4.2口芍,你可以略過第一條弃榨;如果你依賴了一些第三方的庫菩收,且沒有升級Swift4.2,你可以繼續(xù)看這一條鲸睛。目前測試的結(jié)果來看娜饵,Swift4 和 Swift4.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
升級XCode10和Swift4.2之前官辈,項(xiàng)目里面有些對 NSDataAsset
的錯(cuò)誤使用: 用NSDataAsset
讀ImageAsset
中的圖片箱舞,這個(gè)是不正確的遍坟,但是卻可以工作,這次升級修復(fù)了這個(gè)BUG晴股。
正確的做法使用DataAsset
愿伴,然后才可以用NSDataAsset
讀取數(shù)據(jù),我由于不夠認(rèn)真且經(jīng)驗(yàn)不足還以為是個(gè)BUG电湘,給Apple提了個(gè)BUG隔节。。寂呛。[捂臉]
3. 第三方庫的重命名 typealias
為了方便的適配Switf4.2對UIKit中的重命名怎诫,有些第三方使用typealias對一些類型進(jìn)行了重命名,以 RxSwift 為例子贷痪,RxSwift中就有如下代碼:
#if swift(>=4.2)
public typealias UIControlEvents = UIControl.Event private
#endif
這會導(dǎo)致一些重命名的類型即使不改也不會報(bào)錯(cuò)刽虹,但是一旦去掉了對某個(gè)庫的依賴就會引入新的問題。
4.Delegate 的 Access Modifier
在升級Swift4.2過程中呢诬,XCode10偶爾會提示需要給某些Delegate
方法添加 private
修飾符涌哲,不要為了消除這個(gè)??添加private
,可能會導(dǎo)致Delegate
永遠(yuǎn)不被調(diào)到尚镰;另外阀圾,如果是一個(gè)public
或者open
的class
,協(xié)議方法記得也要加上public
狗唉,否則會出一樣的問題初烘,具體原因我還在測試,但是現(xiàn)象是這樣的分俯,有新的見解歡迎評論區(qū)討論肾筐。
5. 機(jī)型適配問題,iPhone XS Max字體變大
有些同事遇到XCode9構(gòu)建的安裝包在iPhone XS Max上會有字體變大的情況缸剪,這個(gè)貌似是普遍現(xiàn)象吗铐,微信也有,使用XCode10構(gòu)建安裝包可以解決這個(gè)問題杏节,但是會遇到問題6
6. iOS9.3以下系統(tǒng)Crash率飆升
使用XCode10構(gòu)建安裝包可以解決問題5唬渗,但是iOS9.3以下的系統(tǒng)Crash到讓你懷疑人生
以下是2018年9月18日內(nèi)容
AVAudioSession.sharedInstance().setCategory()
disappeared
Swift 4.2 中 iOS10以下不能用 AVAudioSession.sharedInstance() setCategory
可選方案:
- 使用OC實(shí)現(xiàn)該部分,然后使用Swift調(diào)用
- 放棄 iOS9用戶體驗(yàn)
參考地址
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
可選方案:
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 |
參考資料
Swift-Migration-4.2