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'endendend
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或者openclass,協議方法記得也要加上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 Swifttry 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 4Swift 4.2
UITableViewCellStyleUITableViewCell.CellStyle

UIEvent

Swift 4Swift 4.2
UIEventSubtypeUIEvent.EventSubtype

UITableView

Swift 4Swift 4.2
UITableViewScrollPositionUITableView.ScrollPosition
UITableViewAutomaticDimensionUITableView.automaticDimension
UITableViewCellEditingStyleUITableViewCell.EditingStyle
UITableViewRowAnimationUITableView.RowAnimation
UITableViewStyleUITableView.Style
UITableViewCellAccessoryTypeUITableViewCell.AccessoryType

UIControl

Swift 4Swift 4.2
UIControlEventsUIControl.Event

UIWindow

Swift 4Swift 4.2
UIWindowLevelAlertUIWindow.Level.alert
UIKeyboardFrameEndUserInfoKeyUIResponder.keyboardFrameEndUserInfoKey
UIKeyboardFrameBeginUserInfoKeyUIResponder.keyboardFrameBeginUserInfoKey
UIKeyboardAnimationDurationUserInfoKeyUIResponder.keyboardAnimationDurationUserInfoKey
UIKeyboardAnimationCurveUserInfoKeyUIResponder.keyboardAnimationCurveUserInfoKey
UIKeyboardIsLocalUserInfoKeyUIResponder.keyboardIsLocalUserInfoKey
UIWindowDidBecomeVisibleUIWindow.didBecomeVisibleNotification
UIWindowDidBecomeHiddenUIWindow.didBecomeHiddenNotification
UIWindowDidBecomeKeyUIWindow.didBecomeKeyNotification
UIWindowDidResignKeyUIWindow.didResignKeyNotification
UIKeyboardWillShowUIResponder.keyboardWillShowNotification
UIKeyboardDidShowUIResponder.keyboardDidShowNotification
UIKeyboardWillHideUIResponder.keyboardWillHideNotification
UIKeyboardDidHideUIResponder.keyboardDidHideNotification

UIViewController

Swift 4Swift 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 4Swift 4.2
UIActivityTypeUIActivity.ActivityType

UIActivityIndicatorView

Swift 4Swift 4.2
activityIndicator.activityIndicatorViewStyleactivityIndicator.style

UIAlertController

Swift 4Swift 4.2
UIAlertActionStyleUIAlertAction.Style
UIAlertControllerStyleUIAlertController.Style

UIPageViewController

Swift 4Swift 4.2
UIPageViewControllerNavigationDirectionUIPageViewController.NavigationDirection
UIPageViewControllerSpineLocationUIPageViewController.SpineLocation
UIPageViewControllerNavigationOrientationUIPageViewController.NavigationOrientation
UIPageViewControllerTransitionStyleUIPageViewController.TransitionStyle
UIPageViewControllerOptionsKeyUIPageViewController.OptionsKey

UINavigationController

Swift 4Swift 4.2
UINavigationControllerOperationUINavigationController.Operation

UIGestureRecognizer

Swift 4Swift 4.2
UIGestureRecognizerStatePossibleUIGestureRecognizer.State.possible
UIGestureRecognizerStateBeganUIGestureRecognizer.State.began
UIGestureRecognizerStateChangedUIGestureRecognizer.State.changed
UIGestureRecognizerStateEndedUIGestureRecognizer.State.ended
UIGestureRecognizerStateCancelledUIGestureRecognizer.State.cancelled
UIGestureRecognizerStateFailedUIGestureRecognizer.State.failed
UIGestureRecognizerStateRecognizedUIGestureRecognizer.State.recognized

NSLayoutFormat

Swift 4Swift 4.2
NSLayoutFormatOptionsNSLayoutConstraint.FormatOptions

UIEdgeInsets

Swift 4Swift 4.2
public func UIEdgeInsetsMake(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsetsUIEdgeInsets(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat)
public func UIEdgeInsetsInsetRect(_ rect: CGRect, _ insets: UIEdgeInsets) -> CGRectpublic func inset(by insets: UIEdgeInsets) -> CGRect

UIFontDescriptor

Swift 4Swift 4.2
UIFontDescriptorSymbolicTraitsUIFontDescriptor.SymbolicTraits

UIImage

Swift 4Swift 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 4Swift 4.2
UIApplicationDidEnterBackgroundUIApplication.didEnterBackgroundNotification
UIApplicationWillEnterForegroundUIApplication.willEnterForegroundNotification
UIApplicationDidFinishLaunchingUIApplication.didFinishLaunchingNotification
UIApplicationDidBecomeActiveUIApplication.didBecomeActiveNotification
UIApplicationWillResignActiveUIApplication.willResignActiveNotification
UIApplicationDidReceiveMemoryWarningUIApplication.didReceiveMemoryWarningNotification
UIApplicationWillTerminateUIApplication.willTerminateNotification
UIApplicationSignificantTimeChangeUIApplication.significantTimeChangeNotification
UIApplicationWillChangeStatusBarOrientationUIApplication.willChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationUIApplication.didChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarFrameUIApplication.didChangeStatusBarFrameNotification
UIApplicationBackgroundRefreshStatusDidChangeUIApplication.backgroundRefreshStatusDidChangeNotification
UIApplicationProtectedDataWillBecomeUnavailableUIApplication.protectedDataWillBecomeUnavailableNotification
UIApplicationProtectedDataDidBecomeAvailableUIApplication.protectedDataDidBecomeAvailableNotification
UIApplicationUserDidTakeScreenshotUIApplication.userDidTakeScreenshotNotification
UIApplicationOpenSettingsURLStringUIApplication.openSettingsURLString
UIApplicationLaunchOptionsKeyUIApplication.LaunchOptionsKey
UIInterfaceOrientationIsLandscape()UIApplication.shared.statusBarOrientation.isLandscape

UIView

Swift 4Swift 4.2
func bringSubview(toFront view: UIView)func bringSubviewToFront(_ view: UIView)
UIViewAnimationOptionsUIView.AnimationOptions()

Foundation

NSAttributedString

Swift 4Swift 4.2
NSAttributedStringKeyNSAttributedString.Key

QuartzCore

CAShapeLayer

Swift 4Swift 4.2
kCALineCapRoundCAShapeLayerLineCap.round
kCALineCapButtCAShapeLayerLineCap.butt
kCALineCapSquareCAShapeLayerLineCap.square
kCALineJoinMiterCAShapeLayerLineJoin.miter
kCALineJoinRoundCAShapeLayerLineJoin.round
kCALineJoinBevelCAShapeLayerLineJoin.bevel
kCAFillRuleNonZeroCAShapeLayerFillRule.nonZero
kCAFillRuleEvenOddCAShapeLayerFillRule.evenOdd

參考資料

Swift-Migration-4.2

轉載于:https://juejin.im/post/5ba0dfb9e51d450e4a1babcb

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/450669.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/450669.shtml
英文地址,請注明出處:http://en.pswp.cn/news/450669.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

學生管理系統Java版

簡單的學生管理系統 主界面編寫: 1.用輸出語句完成主界面的編寫 2.用Scanner語句實現鍵盤的錄入 3.用swich語句完成操作的選擇 4.用循環完成再次回到主界面 代碼實現: while (true) {//1.用輸出語句完成主界面的編寫System.out.println("--------…

dubbo 配置文件詳解

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 一、dubbo常用配置 <dubbo:service/> 服務配置&#xff0c;用于暴露一個服務&#xff0c;定義服務的元信息&#xff0c;一個服務可…

ASP.NET Core 實戰:Linux 小白的 .NET Core 部署之路

一、前言 最近一段時間自己主要的學習計劃還是按照畢業后設定的計劃&#xff0c;自己一步步的搭建一個前后端分離的 ASP.NET Core 項目&#xff0c;目前也還在繼續學習 Vue 中&#xff0c;雖然中間斷了很長時間&#xff0c;好歹還是堅持下來了&#xff0c;嗯&#xff0c;看了看…

學以致用十三-----Centos7.2+python3+YouCompleteMe成功歷程

歷經幾天的摸索&#xff0c;趟過幾趟坑之后&#xff0c;終于完成YouCompleteMe的安裝配置。 今天同樣是個不能忘記的日子&#xff0c;國恥日&#xff0c;勿忘國恥。&#xff08;9.18&#xff09; 服務器安裝好&#xff0c;基本配置配置好后&#xff0c;開始安裝。 一、檢查服務…

VC畫圖用到的主要方法

1。鼠標落下&#xff0c;記錄鼠標的起始位置 void CMyEasyDrawView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息處理程序代碼和/或調用默認值 //graph->m_nTypedlg-> m_bStartDraw true; m_PtPress m_PtLast point; CView::OnLButtonDown…

【最新版】Java學習路線(含B站口碑推薦視頻鏈接)

文章目錄關于如何自學一、計算機網絡二、數據結構與算法三、操作系統四、計算機組成原理五、編譯原理六、設計模式七、MySQL八、實操工具九、JAVA并發與JVM十、Redis十一、Linux十二、Java路線學習尚硅谷黑馬程序員動力節點狂神說十三、Java基礎十四、JavaWeb十五、框架十六、微…

記錄no static method cannot be reference

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 報錯如題&#xff1a; no static method cannot be reference 我一直以為是在靜態方法中調用了非靜態方法&#xff0c;實際上只是我在注…

文件存儲權限

Android 6.0及以上&#xff0c;需要動態申請權限&#xff1a; Manifest.permission.READ_EXTERNAL_STORAGE Manifest.permission.WRITE_EXTERNAL_STORAGE <uses-permission-sdk-23 android:name"android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permis…

從工具的奴隸到工具的主人

摘要&#xff1a;我們每個人都是工具的奴隸。隨著我們的學習&#xff0c;我們不斷的加深自己對工具的認識&#xff0c;從而從它們里面解脫出來。現在我就來說一下我作為各種工具的奴隸&#xff0c;以及逐漸擺脫它們的思想控制的歷史吧。 當我高中畢業進入大學計算機系的時候&am…

記錄A component required a bean named ‘studentService‘ that could not be found.

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 報錯如題&#xff1a; A component required a bean named studentService that could not be found. 出問題的代碼行&#xff1a; &l…

Java---利用程序實現在控制臺聊天

一.普通版&#xff08;不能實現隨意輸入&#xff09; 電腦A(服務器端) package day; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner;public class Mysever {public static void…

16.看板方法——三類改進機會筆記

00.三種常見的模型和它們一些變種&#xff1a;約束理論及其主要理念&#xff1b;還有聚焦于分析和減少變異性的模型及其變種等。 01.五步聚焦法 *a.識別約束 *b.作出決定&#xff0c;以最大化利用約束 *c.使系統中的其余一切部分都服從于b中做出決定 *d.突破約束 *e.避免惰性&a…

C/C++的64位整型

在C/C中&#xff0c;64為整型一直是一種沒有確定規范的數據類型。現今主流的編譯器中&#xff0c;對64為整型的支持也是標準不一&#xff0c;形態各異。一般來說&#xff0c;64位整型的定義方式有long long和__int64兩種(VC還支持_int64)&#xff0c;而輸出到標準輸出方式有pri…

記錄 Duplicate spring bean id dubbo

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 啟動工程 報錯如題&#xff1a; Duplicate spring bean id dubbo &#xff0c;意思是id 重復。 原因是我在加載配置文件時加載了兩個…

1.KafKa-介紹

轉載于:https://www.cnblogs.com/v-lcc/p/9674975.html

關于日志的123

寫在前面&#xff1a; 關于日志其實有很多想說的&#xff0c;不過將自己整理的文檔轉化為Blog還是比較花時間的&#xff0c;偶有疏漏&#xff0c;請多包涵。 本篇文章所講均只止于Java。 日志的作用&#xff1a; 1.定位問題&#xff0c;對于一個系統而言&#xff0c;總是會有些…

研究顯示每天工作超8小時得心臟病概率增加80%,生命很重要,工作不要那個累。

每天工作超過8小時的人患心臟病的風險最高可增加80%(資料圖) 據英國《每日郵報》9月12日報道&#xff0c;芬蘭職業保健研究所的科學家們近日進行了一項研究&#xff0c;他們發現每天工作超過8小時的人患心臟病的風險最高可增加80%。 研究人員表示&#xff0c;長時間的工作是許多…

SVN介紹

1.SVN介紹SVN是一個跨平臺的開源的版本控制系統&#xff0c;svn版本管理工具管理著隨時間改變的各種數據&#xff0c;這些數據放置在一個中央檔案庫&#xff08;repository&#xff09;中&#xff0c;svn會備份并記錄每個文件每一次的修改、更新、變動。這樣可以把任意一個時間…

記錄 Annotation processing is not supported for module cycles.

報錯&#xff1a;Error:java: Annotation processing is not supported for module cycles. Please ensure that all modules from cycle [A,B] are excluded from annotation processing 我是想啟動兩個 maven工程&#xff0c;相互作為服務提供方和消費方&#xff0c;于是在p…

沉淀再出發:Spring的架構理解

沉淀再出發:Spring的架構理解 一、前言 在Spring之前使用的EJB框架太龐大和重量級了&#xff0c;開發成本很高&#xff0c;由此spring應運而生。關于Spring&#xff0c;學過java的人基本上都會慢慢接觸到&#xff0c;并且在面試的時候也是經常遇到的&#xff0c;因為這個技術極…