21
23

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] 配列と辞書

Last updated at Posted at 2015-03-24

自分用忘備録 (swift 1.0)

配列

####配列の作成

var array1 = [4, 5, 6]			//型推論
var array2:[Int] = [1, 4, 5]	//型指定
var array3 = [Int](); var array4:[Int] = []  //初期化
var array6 = [Int](1...10)		//範囲を指定[1, 2...10]
var array7 = [Double](stride(from: 8.0, through: 10.0, by: 0.5))    //範囲と増減値を指定 [8.0, 8.5...10.0]
var array8 = [String](count: 5, repeatedValue: "def")    //指定した値を繰り返す ["def"..."def"]
var part = array1[1...2]        //配列の一部から新しい配列を作成[5, 6]

####要素の追加

var tmp1 = [Int]()
tmp1.append(5)      //末尾に追加[5]
tmp1.insert(9, atIndex: 0)  //インデックス番号に挿入[9, 5]
tmp1 += [6]         //末尾に追加[9, 5, 6]
tmp1 = [4] + tmp1   //先頭に追加[4, 9, 5, 6]

####代入と追加、削除

var tmp2 = [1, 4, 5]
tmp2[2] = 9                 //2番を9に[1, 4, 9]
tmp2[1...2] = [8, 6, 10]    //1から2番を8,6,10に[1, 8, 6, 10]
tmp2[0...0] = [4, 0, 6]     //0番を4,0,6に[4, 0, 6, 8, 6, 10]
tmp2[1...3] = []            //1から3番を削除[4, 6, 10]
tmp2.removeAtIndex(2)       //インデックス番号で削除[4, 6]
tmp2.removeLast()           //最後を削除[4]
tmp2.removeAll(keepCapacity: false) //全削除 引数は元の記憶領域を保持するか

####プロパティ

var tmp3 = [1, 5, 6, 2]
tmp3.count      //要素の個数 Int {get}   4
tmp3.first      //最初の要素 T?  {get}   1
tmp3.last       //最後の要素 T?  {get}   2
tmp3.isEmpty    //要素の有無 Bool{get}   false

####ソート

var other = [6,3,2,6,9,8,4,0,1,7]
let reverse = other.reverse()	//逆に並べた配列を返す[7, 1, 0, 4, 8, 9, 6, 2, 3, 6]
other.sort({$0 < $1})			//other自体が変更される[0, 1, 2, 3, 4, 6, 6, 7, 8, 9]
var sorted = other.sorted({$1 < $0})	//元の配列を保持してソート"[9, 8, 7, 6, 6, 4, 3, 2, 1, 0]"

####オプショナル型の配列

var optArray:[Int?] = [5, 3, nil, 8]
let num:Int = optArray[1]!          //返り値はオプショナル型

####多次元配列

var multi: [[Int]] = [[1, 2, 3], [9, 8, 7]]
multi[0]            //[1, 2, 3]
multi[0][2]         //3
multi[1].append(6)  //1番の配列の末尾に6を追加 [[1, 2, 3], [9, 8, 7, 6]]

####要素からインデックス番号を得る

GetIndex
let array = [10,50,80]
let index = find(array, 50)
println(index)		//1

findはSwift 2.0からはサポートされなくなり、新しく.indexOfが追加される。

indexOf
let array = [10,50,80]
let index = array.indexOf(50)
println(index)		//1

辞書

var dic = [key:value]
キーと値の方はそれぞれ統一されている必要がある
辞書から得た値の型はオプショナル型になる
キーはHashableな型でなければならない

型を明示する場合
下記は空の辞書が作られる
var dic = [String:Int] ()
var dic:[String:Int] = [:]

####要素の追加と削除

var dic:[String:Int] = ["zero":0]
dic["one"] = 1
dic["zero"] = nil

####辞書の比較
要素の順序は問わない

var dic1 = ["zero":0, "one":1]
var dic2 = ["one":1, "zero":0]
dic1 == dic2		///true

####辞書から配列を作る

var dic = ["zero":0, "one":1, "two":2]
var keys = [String](dic.keys)	//["zero", "one", "two"]
var values = [Int](dic.values)	//[0, 1, 2]

####メソッドとプロパティ

var dic = ["zero":0, "one":1, "two":2]
//oldには更新前の2が入り、dicの"two"の値が9に更新される キーが存在しなければnilが帰る
let old = dic.updateValue(9, forKey: "two")
//delには削除前の0が入り、dicの"zero"が削除される キーが存在しなければnilが帰る
let del = dic.removeValueForKey("zero")
let num = dic.count		//要素の数(2)
21
23
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
21
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?