Attention! Translated article might be found on my English blog.

2018年1月4日木曜日

古いswiftプロジェクトをビルドし直してみた

2014年に書いたコードを久々にビルドしようとしたらエラーの嵐でした。
大部分は自動修正(?)で直ったものの、自分で調べて直さないといけないのも結構ありました。

以下、修正時の置き換えメモです。

CGSizeMake, CGPointMake:

CGSizeMake -> CGSize(width:height:)
CGPointMake -> CGPoint(x:y:)

参考: uikit - CGRectMake, CGPointMake, CGSizeMake, CGRectZero, CGPointZero is unavailable in Swift - Stack Overflow

CGVectorMake:

CGVectorMake -> CGVector(dx:dy:)

参考: sprite kit - cgvectormake is unavailable in swift - Stack Overflow

"C-style for statement has been removed in Swift 3":

Swift styleを使う。 for i in 0..<len {}

参考: for loop - Fix warning "C-style for Statement is deprecated" in Swift 3 - Stack Overflow

"Type 'NSCompositingOperation' has no member 'CompositeCopy'":

NSCompositingOperation.copy?


Unary operator '++' cannot be applied to an operand of type '@lvalue NSInteger' (aka '@lvalue Int'):

++や--の代わりに+= 1, -= 1を使う

参考: swift - Unary operator '--' cannot be applied to an operand of type '@lvalue Int?' (aka '@lvalue Optional<Int>') - Stack Overflow


"Cannot convert value of type 'ObjCBool' to expected argument type 'Bool'":

hogeそのものではなくhoge.boolValueを使う

参考: Swift3でObjCBoolとBoolが変換できない件 - 泣きながらプログラミング。


その他:

rand() -> arc4random()
SKPhysicsBody(rectangleOfSize:) -> SKPhysicsBody(rectangleOf:)
func applicationDidFinishLaunching(aNotification : NSNotification) -> func applicationDidFinishLaunching(_ notification: Notification)
func didBeginContact(contact: SKPhysicsContact)  -> func didBegin(_ contact: SKPhysicsContact)


ちなみに、同時期に作っていたObj-Cのプロジェクトはそのままビルドできて普通に動いたという…。