9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Xcode10] Swift4.2からの Arrayの操作の変更点について first / firstIndex (where:) / firstIndex(of:)

Last updated at Posted at 2018-09-21

Swift4.2 でのAPIの変更点

Swift4.2 でのAPI変更点である、「New Sequence Methods」 によって、
うそ松の判別法の書き方が下記のように変わります。 (ネタです)

Playground.swift
let osomatsuBrothers = ["おそ松", "カラ松", "チョロ松", "一松", "ウソ松", "十四松", "トド松"]

// Swift 4.1 first / index (where:) / index (of:)

if let usomatsu = osomatsuBrothers.first(where: {$0.hasPrefix("ウソ")}),
    let usomatsuIndex = osomatsuBrothers.index(where:{$0.hasPrefix("ウソ")}),
    let tyoromatsuIndex = osomatsuBrothers.index(of: "チョロ松") {
    print("\(usomatsuIndex + 1) 番目が \(usomatsu) くん.") // 5 番目が ウソ松 くん.
    print("チョロ松のindexは \(tyoromatsuIndex + 1)") // チョロ松のindexは 3
} else {
    print("ウソが付く兄弟がいなかった")
}

Swift4.1まではこうだったのが、Swift4.2からはこうなります。

Playground.swift

let osomatsuBrothers = ["おそ松", "カラ松", "チョロ松", "一松", "ウソ松", "十四松", "トド松"]

// Swift 4.2 first / firstIndex (where:) / firstIndex(of:)

if let usomatsu = osomatsuBrothers.first(where: {$0.hasPrefix("ウソ")}),
    let usomatsuIndex = osomatsuBrothers.firstIndex(where: {$0.hasPrefix("ウソ")}),
    let tyoromatsuIndex = osomatsuBrothers.firstIndex(of: "チョロ松") {
    print("\(usomatsuIndex + 1) 番目が \(usomatsu) くん.") // 5 番目が ウソ松 くん.
    print("チョロ松のindexは \(tyoromatsuIndex + 1)") // チョロ松のindexは 3
} else {
    print("ウソが付く兄弟がいなかった")
}

ついでに lastIndexについても同じような操作で対応できます。

Playground.swift

let osomatsuBrothers = ["おそ松", "カラ松", "チョロ松", "一松", "ウソ松", "十四松", "トド松"]

// Swift 4.2 last / lastIndex (where:) / lastIndex(of:)

if let usomatsu = osomatsuBrothers.last(where: {$0.hasPrefix("ウソ")}),
    let usomatsuIndex = osomatsuBrothers.lastIndex(where: {$0.hasPrefix("ウソ")}),
    let lastMajorIndex = osomatsuBrothers.lastIndex(of: "チョロ松") {
    print("\(usomatsuIndex + 1) 番目が \(usomatsu) くん.") // 5 番目が ウソ松 くん.
    print("チョロ松のindexは \(lastMajorIndex + 1)") // チョロ松のindexは 3
} else {
    print("ウソが付く兄弟がいなかった")
}

元ネタはこちらの記事です。

What's new in Swift 4.2 - New Sequence Methods

Playground.swift

let ages = ["ten", "tweleve", "thirteen", "nineteen", "eighteen", "seventeen", "fourteen", "eighteen", "fifteen", "sixteen", "eleven"]

// Swift 4.1

if let firstTeen = ages.first(where: {$0.hasSuffix("teen")}),
    let firstIndex = ages.index(where:{$0.hasSuffix("teen")}),
    let firstMajorIndex = ages.index(of: "eighteen") {
    print("Teenager number \(firstIndex + 1) is \(firstTeen) years old.")
    print("Teenager number \(firstMajorIndex + 1) isn't a minor anymore")
} else {
    print("No Teenagers around here")
}

// Swift 4.2

if let firstTeen = ages.first(where: {$0.hasSuffix("teen")}),
    let firstIndex = ages.firstIndex(where: {$0.hasSuffix("teen")}),
    let firstMajorIndex = ages.firstIndex(of: "eightenn") {
    print("Teenager number \(firstIndex + 1) is \(firstTeen) years old.")
    print("Teenager number \(firstMajorIndex + 1) isn't a minor anymore")
} else {
    print("No Teenagers around here")
}

if let lastTeen = ages.last(where: {$0.hasSuffix("teen")}),
    let lastIndex = ages.lastIndex(where: {$0.hasSuffix("teen")}),
    let lastMajorIndex = ages.lastIndex(of: "eighteen") {
    print("Teenager number \(lastIndex + 1) is \(lastTeen) years old.")
    print("Teenager number \(lastMajorIndex + 1) isn't a minor anymore")
} else {
    print("No Teenagers around here")
}

また、 allSatisfyが新しく加わりました。
これによってシーケンスの全ての要素が条件を満たすかどうかを取得したい場合の書き方が下記のように書けるようになります。

Playground.swift
let values = [10, 8 ,12, 20] // [10, 8, 12, 20]
let allEven = values.allSatisfy{ $0 % 2 == 0}
allEven // true
9
5
0

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
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?