LoginSignup
51
46

More than 5 years have passed since last update.

Swift 初心者がまとめる Xcode 7.3 で Warning になった Swift の文法

Posted at

Warning の文を見出しとしてまとめます。

Curried function declaration syntax will be removed in a future version of Swift; use a single parameter list

Swift 3.0 でカリー化関数の構文が廃止が決定していますので、今から書き換えておこうね と Xcode が教えてくれます。

カリー化関数ってなんですか…?初めて聞きました…。

Warningになる書き方
func curried(x: Int)(y: String) -> Float {
    return Float(x) + Float(y)!
}
Xcodeの自動修正
func curried(x: Int, y: String) -> Float {
    return Float(x) + Float(y)!
}
GitHubに書かれている代替記述方法
func curried(x: Int) -> (String) -> Float {
    return {(y: String) -> Float in
        return Float(x) + Float(y)!
    }
}

SE-0002: Removing currying func declaration syntax

'var' parameters are deprecated and will be removed in Swift 3

Swift 3.0 で関数の引数に var が使えなくなることが決定していますので、今から書き換えておこうね と Xcode が教えてくれます。

Warningになる書き方
func foo(var i: Int) {
    i += 1
}

Xcodeの自動修正
func foo( i: Int) {
    i += 1
}

Xcode の自動修正だと、結局はエラーになっちゃうんですよね…。

GitHubに書かれている代替記述方法
func foo(inout i: Int) {
    i += 1
}

var x = 1
print(foo(&x))

この print の結果は 2 です。
SE-0003: Removing var from Function Parameters

'++' is deprecated: it will be removed in Swift 3

Swift 3.0 で++演算子の廃止が決定していますので、今から書き換えておこうね と Xcode が教えてくれます。

Warningになる書き方
var x = 1
let a = ++x
let b = x++
Xcodeの自動修正
var x = 1
let a = x += 1
let b = x += 1

いや、for文とかで使うときはこの修正でいいんですけどね…。

代替記述方法
var x = 1
x += 1
let a = x
let b = x
x += 1

successor メソッドを使ってもいいですね。
SE-0004: Remove the ++ and -- operators

'--' is deprecated: it will be removed in Swift 3

Swift 3.0 で--演算子の廃止が決定していますので、今から書き換えておこうね と Xcode が教えてくれます。

Warningになる書き方
var x = 1
let a = --x
let b = x--
Xcodeの自動修正
var x = 1
let a = x -= 1
let b = x -= 1
代替記述方法
var x = 1
x -= 1
let a = x
let b = x
x -= 1

SE-0004: Remove the ++ and -- operators

C-style for statement is deprecated and will be removed in a future version of Swift

これは結構有名かと思われますが、Swift 3.0 で C言語のようなfor文が廃止されます。

Warningになる書き方
for var i = 0; i <= 5; i += 1 {
    print(i)
}

この書き方だと Xcode は自動修正してくれません。

代替記述方法
for i in 0 ..< 5 {
    print(i)
}

for/inループを使うようにすると解消できます。
SE-0007: Remove C-style for-loops with conditions and incrementers

とりあえず、急ぎで書いたので…
気が向いたら追記します…。

51
46
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
51
46