如果喜歡這篇文章巍棱,歡迎點贊或者點個關(guān)注:[我的微博](http://weibo.com/devlcd) 梆造,以后發(fā)布文章,會第一時間在微博通知
## 依賴庫
> 如果你看的時候項目依賴的所有庫都支持了 Swift 4.2 請忽略這部分內(nèi)容,直接從下節(jié)開始
因為現(xiàn)在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
## 系統(tǒng)代理方法變更
每次升級 Swift 最坑的就是系統(tǒng)代理方法變更,而自己沒有發(fā)現(xiàn)亡鼠,修改完語法之后以為沒問題赏殃,結(jié)果因為系統(tǒng)代理方法變更引起各種奇怪的 bug
建議升級版本時,先搞定已變更的代理方法
4.1 -> 4.2同樣也有方法變更间涵,以下是我遷移過程中發(fā)現(xiàn)的變更仁热,如有遺漏,歡迎補充:
### UIImagePickerControllerDelegate
在 Swift 4.2 中 `UIImagePickerControllerReferenceURL`? `UIImagePickerControllerOriginalImage` 等由常量變?yōu)榱?Struct:
```
public struct InfoKey : Hashable, Equatable, RawRepresentable {
? ? public init(rawValue: String)
}
```
所以以下方法也需要跟著修改勾哩,如果不改是不會執(zhí)行該代理方法的:
```
// 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
```
## 其他變更
以下是我在升級過程中遇到的變更情況,大致整理為「通知相關(guān)思劳,常量變更物蝙,類型變更,方法變更」四類敢艰,共大家參考:
## 通知相關(guān)
#### 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 由常量變?yōu)榱薝IView 的 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 常量變?yōu)?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)