日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

XCode10 swift4.2 适配遇到的坑

發布時間:2023/12/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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或者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 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

總結

以上是生活随笔為你收集整理的XCode10 swift4.2 适配遇到的坑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。