19
13

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.

[Swift]String型の配列からInt型の配列に変換する方法でつまづいた

Last updated at Posted at 2014-12-26

俺の名はケンヤ。難事件をいくつも迷宮入りさせたニート。しかし ある時謎の組織に入社し薬を飲まされ身体が縮んで Braian になっちゃった。

##mapを使用した変換

var numbersString: [String] = ["1", "2", "3"] 
var numbersInt: [Int] = []

numbersInt = numbersString.map({ (value: String) -> Int in
                return value.toInt()!
            })
            // [1, 2, 3]

##forを使用した変換

var numStrings: [String] = ["1", "2", "3"]
var numIntegers: [Int] = []

for str in numStrings {
    numIntegers.append(str.toInt()!)
}
// [1, 2, 3]

これですね!!
個人的には基本的にはmapが好きです!!

他の書き方やこうしたほうがいいなどあればコメントくださいませ!!
おねがいします。

もっと良い書き方を教えて頂きました!!!
koherさんありがとうございます。

修正.png

19
13
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
19
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?