0
1

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 1 year has passed since last update.

タカシAdvent Calendar 2022

Day 24

【Swift】Any型ってなに?

Posted at

Any型

Any型とは任意の型を表す型のことです。
Any型の変数や定数にはどんな型の値も代入することができるので、代入する値が決まっていない場合に使用する。

let str: Any = "あいうえお"
type(of: str) //String.Type

let int: Any = 12345
type(of: int) //Int.Type

Any型の欠点

Any型は変数や定数に代入すると、元の型の情報が失われてしまう。
Int型では四則演算は可能なるが、Any型ではできない。

let a: Any = 1
let b: Int = 2
a + b // コンパイルエラー

なので、可能な限りAny型への代入は避けて、型の情報を保つ方が良い。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?