XCode10 swift4.2 適配遇到的坑

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ò)誤使用: 用NSDataAssetImageAsset中的圖片箱舞,這個(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或者openclass,協(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

可選方案:
  • .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))
  • 使用其他默認(rèn)值

下面是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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末镊逝,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子嫉鲸,更是在濱河造成了極大的恐慌撑蒜,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異座菠,居然都是意外死亡染突,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門辈灼,熙熙樓的掌柜王于貴愁眉苦臉地迎上來份企,“玉大人,你說我怎么就攤上這事巡莹∷局荆” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵降宅,是天一觀的道長骂远。 經(jīng)常有香客問我,道長腰根,這世上最難降的妖魔是什么激才? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮额嘿,結(jié)果婚禮上瘸恼,老公的妹妹穿的比我還像新娘。我一直安慰自己册养,他們只是感情好东帅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著球拦,像睡著了一般靠闭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上坎炼,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天愧膀,我揣著相機(jī)與錄音,去河邊找鬼谣光。 笑死檩淋,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的抢肛。 我是一名探鬼主播狼钮,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼捡絮!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起莲镣,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤福稳,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后瑞侮,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體的圆,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鼓拧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了越妈。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片季俩。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖梅掠,靈堂內(nèi)的尸體忽然破棺而出酌住,到底是詐尸還是另有隱情,我是刑警寧澤阎抒,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布酪我,位于F島的核電站挡鞍,受9級特大地震影響时呀,放射性物質(zhì)發(fā)生泄漏素挽。R本人自食惡果不足惜忍啤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一镀脂、第九天 我趴在偏房一處隱蔽的房頂上張望赞季。 院中可真熱鬧沈跨,春花似錦篮奄、人聲如沸展氓。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽带饱。三九已至毡代,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間勺疼,已是汗流浹背教寂。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留执庐,地道東北人酪耕。 一個(gè)月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像轨淌,于是被迫代替她去往敵國和親迂烁。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 如果喜歡這篇文章递鹉,歡迎點(diǎn)贊或者點(diǎn)個(gè)關(guān)注:[我的微博](http://weibo.com/devlcd) 盟步,以后發(fā)布...
    devlcd閱讀 6,420評論 4 9
  • 片段 一: 選自《堅(jiān)持,一種可以養(yǎng)成的習(xí)慣》 I:對自己一天既定生活躏结、工作規(guī)律進(jìn)行分析却盘,結(jié)合自己偶發(fā)因素的影響,磨...
    自畫像55閱讀 123評論 0 0
  • 再次醒來之后,他看見他在空中飄著黄橘,前面有個(gè)小孩用繩子拉著他走兆览,嘴里還在嘟囔著:“好累啊,人間的路怎么這么長塞关,怎么還...
    廣電1701b劉玉靜閱讀 236評論 1 1
  • 焦慮抬探,自責(zé),內(nèi)疚帆赢,沒安全感小压,這些負(fù)面情緒集聚在這一段時(shí)間,原因一是對于新工作的不適應(yīng)匿醒,及對未來的更加迷茫场航。二是人際...
    英仔_6b07閱讀 3,364評論 0 0
  • 今天是2016年4月19日,陰天廉羔。我在大連溉痢,一個(gè)海濱城市。昨天在一個(gè)app上偶然翻到簡書憋他,基于好奇孩饼,翻了翻內(nèi)...
    欣欣592閱讀 251評論 0 0