The following is updated on October 23, 2018

After about a month of adaptation, XCode10(hereinafter referred to as 10 or XC10) was officially used in the project. Most libraries were upgraded to Swift4.2 (hereinafter referred to as 4.2 or S4.2). Here are some problems encountered during the adaptation process.

1. Swift4 and Swift4.2

If you are working on a small, stand-alone project, you can upgrade to 4.2 altogether. You can skip section 1. If you rely on some third party library and have not upgraded 4.2, you can continue to see this article. So far the Swift4 and S4.2 combinations are working fine. If you’re using Cocoapod, you can add the following code to your Podfile:

swift_41_pod_targets = ['your_target_name']
post_install do |installer|
  installer.pods_project.targets.each do |target|
    ifswift_41_pod_targets.include? (target.name) target.build_configurations.eachdo |config|
        config.build_settings['SWIFT_VERSION'] = '4.1'
      end
    end
  end
end
Copy the code

2. NSDataAsset

Before upgrading XC10 and S.2, there were some incorrect uses of ‘NSDataAsset’ in the project: using ‘NSDataAsset’ to read images in ImageAsset was incorrect, but it worked. This update fixes this BUG.

The right thing to do is to use ‘DataAsset’, and then you can use ‘NSDataAsset’ to read data. I thought it was a BUG due to my lack of care and experience, and I gave Apple a BUG… [his face]

3. Rename the third-party library TypeAlias

In order to easily adapt S4.2 to UIKit renaming, some third parties use TypeAlias to rename some types. For example, RxSwift has the following code:

# if swift (> = 4.2)
    public typealias UIControlEvents = UIControl.Event  private
#endif
Copy the code

This can cause some renamed types to remain unchanged without error, but removing a dependency on a library can introduce new problems.

4. The Delegate Access Modifier

When upgrading S4.2, XC will occasionally prompt you to add private modifiers to some Delegate methods. Do not add private to eliminate this ⚠️, as it may cause the Delegate never to be called; In addition, if it is a public or open class, remember to add public to the protocol method, otherwise the same problem will occur. I am still testing the specific reason, but the phenomenon is like this, new insights welcome to discuss in the comments section.

5. IPhone XS Max font has become larger due to model adaptation problems

It seems to be a common phenomenon for some colleagues to encounter the font enlargement in the installation package built by XC9 on iPhone XS Max. Wechat also has this problem. Using XC10 to build the installation package can solve this problem, but there will be problem 6

Using XC10 to build installers can fix problem 5, but under iOS9.3 systems Crash enough to make you doubt your life

The following is from September 18, 2018

AVAudioSession.sharedInstance().setCategory()

disappeared

Swift iOS10 in 4.2 the following cannot use AVAudioSession. SharedInstance setCategory ()

Options:
  • Implement this section using OC, and then use the Swift call
  • Abandon the iOS9 user experience

Refer to the address

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)") }Copy the code

NSUnderlineStyle (patternSolid, none).

disappeared

Options:
  • .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)

  • Using CTUnderlineStyleModifiers

    / / no test NSUnderlineStyle. Init (rawValue: Int (CTUnderlineStyleModifiers. PatternSolid. RawValue))

  • Use other defaults

The following is the Rename operation

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

The resources

Swift – Migration – 4.2