Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@suisuina

Swift 配列の基本

配列の基本について記載

型指定

var ary:Array<Int> = [1,13]

型指定[]での記述

var ary:[Int] = [1,2]

複数の型指定

var ary:Array<Any> = [1,"anything"]

追加

ary.append("add")

指定indexに追加

ary.insert("add0", atIndex: 0)

index値が1以上3未満の値を置き換える

var animal = ["tiger","elephant","dog","cat"]
animal[1..<3] = ["a","b"]
print(animal) //"["tiger", "a", "b", "cat"]\n"

index値が1~3の値を置き換える

var animal = ["tiger","elephant","dog","cat"]
animal[1...3] = ["a","b"]
print(animal) //"["tiger", "a", "b"]\n"

指定indexを削除

var animal = ["tiger","elephant","dog","cat"]
animal.removeAtIndex(2)
print(animal) // "["tiger", "elephant", "cat"]\n"

コピー
(配列のコピーは参照渡しではなく、値のコピーになります。)

var animal = ["tiger","elephant","dog","cat"]
var copyArray = animal
copyArray[0] = "change"
print(copyArray[0]) // change
print(animal[0])  //tiger

以上

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?