Help us understand the problem. What is going on with this article?

SwiftでXcode6 Beta7にアップデートして対応したことまとめ

More than 5 years have passed since last update.

apple_swift.jpg

はじめに

9月3日にXcode6 Beta7がリリースされました。今回はリリースノートを見るとをSwiftの変更数は少なく安堵していたのですが、いざ開発中のプロジェクトをコンパイルしてみると怒涛のコンパイルエラー(数百個)が発生! ちまちまとエラーの対応を行い、半日ほどかけてようやくBeta7で動くようになりました。

以下、自分の手元で確認できた変更点です。
【注意】Beta7の変更点をすべて網羅しているわけではありません。

Optionalに変更されたところ

対象の定義がoptionalに変更されたケースです。利用時にForced Unwrapping, Optional Chaining, Optional Bindingをするように変更します。

UIViewController

storyboardnavigationControllerは毎回nilチェックするのはめんどくさいですね。

class UIViewController : UIResponder, NSCoding, UIAppearanceContainer, NSObjectProtocol, UITraitEnvironment, UIContentContainer {
    ...
    var nibName: String? { get } 
    var nibBundle: NSBundle? { get } 
    var storyboard: UIStoryboard? { get }
}

extension UIViewController {    
    ...
    var navigationController: UINavigationController? { get } 
}

extension UIViewController {
    ...
    var toolbarItems: [AnyObject]?
}

UITableViewCell

textLabel, detailTextLabel, imageView, backgroundViewはデフォルトがnilになりました。
扱いにくいですね。。。

class UITableViewCell : UIView, NSCoding, UIGestureRecognizerDelegate, NSObjectProtocol {
    ...    
    var imageView: UIImageView? { get } // default is nil.  image view will be created if necessary.
    var textLabel: UILabel? { get } // default is nil.  label will be created if necessary.
    var detailTextLabel: UILabel? { get } // default is nil.  label will be created if necessary (and the current style supports a detail label).
    ...
    var backgroundView: UIView? 

UIView

extension UIView {    
    var superview: UIView? { get }
    var window: UIWindow? { get }

UIButotn

class UIButton : UIControl, NSCoding {
    ...
    var tintColor: UIColor?
    var titleLabel: UILabel? { get }
    var imageView: UIImageView? { get }

UIGestureRecognizer

class UIGestureRecognizer : NSObject {
    var view: UIView? { get } 

NSURL

class NSURL : NSObject, NSSecureCoding, NSCoding, NSCopying {    
    ...
    var host: String? { get }
    @NSCopying var port: NSNumber? { get }
    var user: String? { get }
    var password: String? { get }
    var path: String? { get }
    var fragment: String? { get }
    var parameterString: String? { get }
    var query: String? { get }
    var relativePath: String? { get }

UIViewControllerContextTransitioning

protocol UIViewControllerContextTransitioning : NSObjectProtocol {
    func viewControllerForKey(key: String!) -> UIViewController?

Outlet

@IBOutlet weak var tableView: UITableView

↓

@IBOutlet weak var tableView: UITableView!
or 
@IBOutlet weak var tableView: UITableView?

Optionalではなくなったところ

segue.identifierはOptionalではなくなった

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let identifier = segue.identifier {
        if identifier == "Hogeidentifier" {
        }
    }
}

↓

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "HogeIdentifier" {
    }
}

Forced Unwrapping ではなくなったところ

UITableViewDataSource/Delegate, UICollectionViewDataSource/Delegate, UIScrollViewDelegate

Delegateの移譲先のメソッドのパラメータの定義が軒並み変更になりました。ここはえいやと置換で対応。

tableView: UITableView!

↓

tableView: UITableView
collectionView: UICollectionView!

↓

collectionView: UICollectionView
scrollView: UIScrollView!

↓

scrollView: UIScrollView
indexPath: NSIndexPath!

↓

indexPath: NSIndexPath
-> UITableViewCell!

↓

-> UITableViewCell
-> UICollectionViewCell!

↓

-> UICollectionViewCell

NSCoping

func copyWithZone(zone: NSZone) -> AnyObject!

↓

func copyWithZone(zone: NSZone) -> AnyObject

UIPanGestureRecognizer

override init(target: AnyObject!, action: Selector) 

↓

override init(target: AnyObject, action: Selector) 

UIViewControllerContextTransitioning

transitionContext: UIViewControllerContextTransitioning!

↓

transitionContext: UIViewControllerContextTransitioning

prepareForSegue

prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)

↓

prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

なぜに。。。

UITableViewControllerUITableViewはUnwrapなのですが、UICollectionViewControllerUICollectionViewはOptionalです。同じ挙動になるべきだと思うのですが。。。

class UITableViewController : UIViewController, ... {
    var tableView: UITableView!    <<---
class UICollectionViewController : UIViewController, ... {
    var collectionView: UICollectionView?   <<---
susieyy
フリーランス - スタートアップエンジニアリングアドバイザー - iOS技術顧問 - プロトタイプ開発
https://susieyy.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした