0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Swift】containsメソッドが使えなくて一瞬あれ?と思った

Posted at

取り組もうとしたこと

  • 配列arrayAppleという値があるか検索しようとした

書いたコード

array.hasContain("Apple")

発生したエラー

Value of type '[Element]' has no member 'hasContain'

解決策

  • シンプルにtypoをしていた。
  • hasContainでもcontainでもない、contains(複数形)を利用する
array.contains("Apple")

containsメソッド

  • 対象の配列から引数にセットした値が配列の中に存在するかBoolで結果を返してくれるメソッド。

シーケンスの中から対象に合致する値があればtrueを, そうでなければばfalseを返します。

let cast = ["Vivien", "Marlon", "Kim", "Karl"]
print(cast.contains("Marlon"))
// Prints "true"
print(cast.contains("James"))
// Prints "false"

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?