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?

More than 3 years have passed since last update.

最近、配列で使用したメソッド復習

Last updated at Posted at 2021-10-02

今回の内容

  • .contains.reverse().randomElement()について簡単に復習です

コードと簡単解説

    var contentsArray = ["UITableView",
                         "UISearchController",
                         "UIView",
                         "UIColor",
                         "UICollectionView",
                         "UISwich",
                         "UIKit",
                         "UITextField",
                         "UIViewController",
                         "UILabel"]

.contains

  • contentsArrayの中にUIViewの値が存在するのでtrueを返します。

  • contentsArrayの中にUIImageの値が存在しないのでfalseを返します。

.contains
        print(contentsArray.contains("UIView"))
        print(contentsArray.contains("UIImage"))
print結果
        true
        false

.reverse( )

  • 配列内の値の順番を逆にします。
.reverse
        contentsArray.reverse()
        print(contentsArray)
print結果
        ["UILabel", "UIViewController", "UITextField", "UIKit", "UISwich", 
         "UICollectionView", "UIColor","UIView", "UISearchController", "UITableView"]

.randomElement( )

  • 配列内の値からランダムに値を取得します。
.randomElement
        print(contentsArray.randomElement() as Any)
print結果
        Optional("UICollectionView")

終わり

ご指摘、ご質問などありましたら、コメントまでお願い致します。

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?