「Swift」firstIndex(of:)メソッドで指定要素の最初の番号を返す

2020年10月25日

説明
firstIndex(of:)メソッドで指定要素が最初の要素番号を返します。
ない場合はnilを返します。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let fruits = ["A001", "B002", "C003", "B002"]
/// 最初の要素番号を返す
if let index = fruits.firstIndex(of: "B002") {
  // some code
} else {
/// some code
}
let fruits = ["A001", "B002", "C003", "B002"] /// 最初の要素番号を返す if let index = fruits.firstIndex(of: "B002") {   // some code } else { /// some code }
let fruits = ["A001", "B002", "C003", "B002"] 
 
/// 最初の要素番号を返す
if let index = fruits.firstIndex(of: "B002") {
    // some code
} else {
    /// some code
}

 

Swift

Posted by arkgame