LoginSignup
0
1

More than 1 year has passed since last update.

タプル型について

Last updated at Posted at 2021-07-24

・タプル型とは?

・タプル型とは、複数の型をまとめて1つの型として扱うことができる型である。

・タプル型の値をタプルという。

・タプル型の定義とタプルの生成

// タプル型の定義
var sampleTuple = (Int, String)

// タプルの生成
sampleTuple = (1,"Hello")

・要素へのアクセス

・要素へのアクセス方法は3つ

①インデックスによるアクセス

②要素名によるアクセス

③代入によるアクセス

・①インデックスによるアクセス

let tuple = (100, "Hello")

print(tuple.0) // 100
print(tuple.1) // Hello

・②要素名によるアクセス

let tuple = (int: 100, string: "Hello")

print(tuple.int) // 100
print(tuple.string) // Hello

・③代入によるアクセス

let int: Int
let string: String

(int, string) = (100, "Hello")

print(int)  // 100
print(string) // Hello


// この方法でも宣言できる
let (int, string) = (100, "Hello")

print(int)
print(string)



参考

・Swift実践入門

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