如果喜欢这篇文章,欢迎点赞或者点个关注:[我的微博](http://weibo.com/devlcd) ,以后发布文章,会第一时间在微博通知
## 依赖库
> 如果你看的时候项目依赖的所有库都支持了 Swift 4.2 请忽略这部分内容,直接从下节开始
因为现在Xcode正式版还没放出来,第三方库也都没有支持 Swift 4.2 ,第一步就是先通过修改podfile让不支持 Swift 4.2 的第三方库在4.1下编译,等到所有依赖的第三方库都支持 Swift 4.2 之后,再把 podfile 改回去
```
swift_41_pod_targets = ['SnapKit','MonkeyKing','RxCocoa', ...]
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
```
## 修改工程配置
在 Build Setting 中搜索 `Swift Language Version` 将 Swift 版本号改为 Swift 4.2
注:如果项目包含多个 Target 的,记得把所有的 target 按以上步骤,将 Swift 版本改为 Swift 4.2
## 系统代理方法变更
每次升级 Swift 最坑的就是系统代理方法变更,而自己没有发现,修改完语法之后以为没问题,结果因为系统代理方法变更引起各种奇怪的 bug
建议升级版本时,先搞定已变更的代理方法
4.1 -> 4.2同样也有方法变更,以下是我迁移过程中发现的变更,如有遗漏,欢迎补充:
### UIImagePickerControllerDelegate
在 Swift 4.2 中 `UIImagePickerControllerReferenceURL` `UIImagePickerControllerOriginalImage` 等由常量变为了 Struct:
```
public struct InfoKey : Hashable, Equatable, RawRepresentable {
public init(rawValue: String)
}
```
所以以下方法也需要跟着修改,如果不改是不会执行该代理方法的:
```
// Swift 4.1
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
```
改为:
```
// Swift 4.2
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
```
### AppDelegate
同理,还有 AppDelegate 中的方法:
#### 1
```
// Swift 4.1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
```
修改为:
```
// Swift 4.2
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
```
#### 2
```
// Swift 4.1
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
```
修改为:
```
// Swift 4.2
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
```
#### 3
```
// Swift 4.1
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool
```
修改为:
```
// Swift 4.2
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool
```
## 其他变更
以下是我在升级过程中遇到的变更情况,大致整理为「通知相关,常量变更,类型变更,方法变更」四类,共大家参考:
## 通知相关
#### Notification.Name.UIApplicationWillResignActive
```
// Swift 4.1
Notification.Name.UIApplicationWillResignActive
```
```
// Swift 4.2
UIApplication.willResignActiveNotification
```
#### Notification.Name.UITextViewTextDidChange
```
// Swift 4.1
Notification.Name.UITextFieldTextDidChange
```
```
// Swift 4.2
UITextField.textDidChangeNotification
```
#### Notification.Name.UIKeyboardWillShow
```
// Swift 4.1
Notification.Name.UIKeyboardWillShow
```
```
// Swift 4.2
UIResponder.keyboardWillShowNotification
```
#### Notification.Name.UIKeyboardWillHide
```
// Swift 4.1
Notification.Name.UIKeyboardWillHide
```
```
// Swift 4.2
UIResponder.keyboardWillHideNotification
```
## 常量变更
##### UILayoutFittingExpandedSize
```
UIKIT_EXTERN const CGSize UILayoutFittingCompressedSize NS_AVAILABLE_IOS(6_0);
UIKIT_EXTERN const CGSize UILayoutFittingExpandedSize NS_AVAILABLE_IOS(6_0);
```
UILayoutFittingExpandedSize 由常量变为了UIView 的 class 属性
```
// Swift 4.1
UILayoutFittingExpandedSize
```
```
// Swift 4.2
UIView.layoutFittingExpandedSize
```
```
// Swift 4.1
UILayoutFittingCompressedSize
```
```
// Swift 4.2
UIView.layoutFittingCompressedSize
```
##### AVAudioSessionRouteChangeReason
```
// Swift 4.1
AVAudioSessionRouteChangeReason
```
```
// Swift 4.2
AVAudioSession.RouteChangeReason
```
#### UIKeyboardFrameEndUserInfoKey
```
// Swift 4.1
UIKeyboardFrameEndUserInfoKey
```
```
// Swift 4.2
UIResponder.keyboardFrameEndUserInfoKey
```
#### UIKeyboardAnimationDurationUserInfoKey
```
// Swift 4.1
UIKeyboardAnimationDurationUserInfoKey
```
```
// Swift 4.2
UIResponder.keyboardAnimationDurationUserInfoKey
```
#### UIKeyboardAnimationCurveUserInfoKey
```
// Swift 4.1
UIKeyboardAnimationCurveUserInfoKey
```
```
// Swift 4.2
UIResponder.keyboardAnimationCurveUserInfoKey
```
#### kCAFillModeForwards
```
// Swift 4.1
kCAFillModeForwards
```
```
// Swift 4.2
CAMediaTimingFillMode.forwards
```
#### kCAMediaTimingFunctionEaseInEaseOut
```
// Swift 4.1
kCAMediaTimingFunctionEaseInEaseOut
```
```
// Swift 4.2
CAMediaTimingFunctionName.easeInEaseOut
```
#### kCALineJoinMiter
```
// Swift 4.1
kCALineJoinMiter
```
```
// Swift 4.2
CAShapeLayerLineJoin.miter
```
### 几种从 String 常量变为 Struct 类型
#### UIImagePickerControllerReferenceURL
```
// Swift 4.1
UIImagePickerControllerReferenceURL
```
```
// Swift 4.2
UIImagePickerController.InfoKey.referenceURL
```
#### UIImagePickerControllerOriginalImage
```
// Swift 4.1
UIImagePickerControllerOriginalImage
```
```
// Swift 4.2
UIImagePickerController.InfoKey.originalImage
```
#### UIImagePickerControllerCropRect
```
// Swift 4.1
UIImagePickerControllerCropRect
```
```
// Swift 4.2
UIImagePickerController.InfoKey.cropRect
```
#### UIImagePickerControllerMediaType
```
// Swift 4.1
UIImagePickerControllerMediaType
```
```
// Swift 4.2
UIImagePickerController.InfoKey.mediaType
```
## 类型变更
##### UITableViewCellStyle
```
// Swift 4.1
UITableViewCellStyle
```
```
// Swift 4.2
UITableViewCell.CellStyle
```
##### UIWindowLevelAlert
```
// Swift 4.1
UIWindowLevelAlert
```
```
// Swift 4.2
UIWindow.Level.alert
```
##### UIViewAnimationCurve
```
// Swift 4.1
UIViewAnimationCurve
```
```
// Swift 4.2
UIView.AnimationCurve
```
##### UIAlertActionStyle
```
// Swift 4.1
UIAlertActionStyle
```
```
// Swift 4.2
UIAlertAction.Style
```
##### UIViewContentMode
```
// Swift 4.1
UIViewContentMode
```
```
// Swift 4.2
UIView.ContentMode
```
##### RunLoopMode
```
// Swift 4.1
RunLoopMode
```
```
// Swift 4.2
RunLoop.Mode
```
##### NSAttributedStringKey
```
// Swift 4.1
NSAttributedStringKey
```
```
// Swift 4.2
NSAttributedString.Key
```
##### UIViewAnimationOptions
```
// Swift 4.1
UIViewAnimationOptions
```
```
// Swift 4.2
UIView.AnimationOptions
```
##### UITableViewAutomaticDimension
```
// Swift 4.1
UITableViewAutomaticDimension
```
```
// Swift 4.2
UITableView.automaticDimension
```
##### UIApplicationLaunchOptionsKey
```
// Swift 4.1
UIApplicationLaunchOptionsKey
```
```
// Swift 4.2
UIApplication.LaunchOptionsKey
```
##### UICollectionViewScrollPosition
```
// Swift 4.1
UICollectionViewScrollPosition
```
```
// Swift 4.2
UICollectionView.ScrollPosition
```
##### UIApplicationOpenURLOptionsKey
```
// Swift 4.1
UIApplicationOpenURLOptionsKey
```
```
// Swift 4.2
UIApplication.OpenURLOptionsKey
```
##### UIViewAutoresizing
```
// Swift 4.1
UIViewAutoresizing
```
```
// Swift 4.2
UIView.AutoresizingMask
```
##### AVPlayerStatus
```
// Swift 4.1
AVPlayerStatus
```
```
// Swift 4.2
AVPlayer.Status
```
##### NSUnderlineStyle
NSUnderlineStyle写法更简洁了
```
// Swift 4.1
NSUnderlineStyle.styleSingle
```
```
// Swift 4.2
NSUnderlineStyle.single
```
##### UIButtonType
```
// Swift 4.1
UIButtonType
```
```
// Swift 4.2
UIButton.ButtonType
```
##### UIControlState
```
// Swift 4.1
UIControlState
```
```
// Swift 4.2
UIControl.State
```
##### UIControlEvents
```
// Swift 4.1
UIControlEvents
```
```
// Swift 4.2
UIControl.Event
```
##### UIAlertControllerStyle
```
// Swift 4.1
UIAlertControllerStyle
```
```
// Swift 4.2
UIAlertController.Style
```
##### UICollectionElementKindSectionHeader
```
// Swift 4.1
UICollectionElementKindSectionHeader
```
```
// Swift 4.2
UICollectionView.elementKindSectionHeader
```
```
// Swift 4.1
UICollectionElementKindSectionFooter
```
```
// Swift 4.2
UICollectionView.elementKindSectionFooter
```
##### UIBarButtonItemStyle
```
// Swift 4.1
UIBarButtonItemStyle
```
```
// Swift 4.2
UIBarButtonItem.Style
```
##### NSAttributedStringKey
```
// Swift 4.1
NSAttributedStringKey
```
```
// Swift 4.2
NSAttributedString.Key
```
##### UIApplicationOpenSettingsURLString
```
// Swift 4.1
UIApplicationOpenSettingsURLString
```
```
// Swift 4.2
UIApplication.openSettingsURLString
```
## 方法变更
#### MKCoordinateRegionMake(CLLocationCoordinate2D centerCoordinate, MKCoordinateSpan span)
```
// Swift 4.1
MKCoordinateRegionMake(a, b)
```
```
// Swift 4.2
MKCoordinateRegion(center: a, span: b)
```
#### MKCoordinateSpanMake(CLLocationDegrees latitudeDelta, CLLocationDegrees longitudeDelta)
```
// Swift 4.1
MKCoordinateSpanMake(0.1, 0.1)
```
```
// Swift 4.2
MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
```
#### UIAccessibilityIsVoiceOverRunning()
```
// Swift 4.1
UIAccessibilityIsVoiceOverRunning()
```
```
// Swift 4.2
UIAccessibility.isVoiceOverRunning
```
#### UIEdgeInsetsMake
```
// Swift 4.1
UIEdgeInsetsMake(10, 0, 40, 0)
```
```
// Swift 4.2
UIEdgeInsets(top: 10, left: 0, bottom: 40, right: 0)
```
#### UIEdgeInsetsInsetRect(rect, insets)
```
// Swift 4.1
UIEdgeInsetsInsetRect(rect, insets)
```
```
// Swift 4.2
rect.inset(by: insets)
```
#### NSStringFromCGPoint(CGPoint point);
```
// Swift 4.1
NSStringFromCGPoint(x)
```
```
// Swift 4.2
NSCoder.string(for: x)
```
#### didMove(toParentViewController:)
```
// Swift 4.1
viewController.didMove(toParentViewController: self)
```
```
// Swift 4.2
viewController.didMove(toParent: self)
```
#### addChildViewController()
```
// Swift 4.1
addChildViewController(viewController)
```
```
// Swift 4.2
addChild(viewController)
```
#### removeFromParentViewController
```
// Swift 4.1
viewController.removeFromParentViewController()
```
```
// Swift 4.2
viewController.removeFromParent()
```
##### var childViewControllers:[UIViewController]
```
// Swift 4.1
let array = viewController.childViewControllers
```
```
// Swift 4.2
let array = viewController.children
```
#### bringSubview(toFront:)
```
// Swift 4.1
bringSubview(toFront: view)
```
```
// Swift 4.2
bringSubviewToFront(view)
```
#### sendSubview(toBack: headerView)
```
// Swift 4.1
sendSubview(toBack: headerView)
```
```
// Swift 4.2
sendSubviewToBack(headerView)
```
##### UIImageJPEGRepresentation(,)
```
// Swift 4.1
let data = UIImageJPEGRepresentation(image, 0.6)
```
```
// Swift 4.2
let data = image.jpegData(compressionQuality: 0.6)
```
##### UIDatePickerMode
```
// Swift 4.1
UIDatePickerMode
```
```
// Swift 4.2
UIDatePicker.Mode
```
##### AVAudioSession.RouteChangeReason
```
// Swift 4.1
UIScrollViewDecelerationRateFast
```
```
// Swift 4.2
UIScrollView.DecelerationRate.fast
```
##### UITableViewCellEditingStyle
```
// Swift 4.1
UITableViewCellEditingStyle
```
```
// Swift 4.2
UITableViewCell.EditingStyle
```
##### AVAudioSessionInterruptionType
```
typedef NS_ENUM(NSUInteger, AVAudioSessionInterruptionType)
{
AVAudioSessionInterruptionTypeBegan = 1, /* the system has interrupted your audio session */
AVAudioSessionInterruptionTypeEnded = 0, /* the interruption has ended */
};
```
```
// Swift 4.1
AVAudioSessionInterruptionType
```
```
// Swift 4.2
AVAudioSession.InterruptionType
```
##### CMTimeMake
```
// Swift 4.1
CMTimeMake(0, 1)
```
```
// Swift 4.2
CMTimeMake(value: 0, timescale: 1)
```
#### AVAudioSession setCategory
AVAudioSession的 setCategory不能像之前版本不填写 mode了,新版写法:
```
try? AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.ambient, mode: .default)
```
以上是我在升级 Swift 4.2 过程中的记录,如有遗漏,欢迎补充
[我的微博](http://weibo.com/devlcd)