0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Swift] 基本的な型とその内容

Last updated at Posted at 2025-11-01

はじめに

この記事はSwift初心者である自分が後で見返す為に書いてます。
間違った箇所がありましたらご容赦ください。

変数と定数

変数[var]

  • 値の再代入ができる
  • varの後に「変数名 = 値」で記述
var variable = "variable"

let 定数[let]

  • 値の再代入ができない
  • letの後に「定数名 = 値」で記述
let constant = "constant"

基本的な型

  • 文字列[String]
let string: String = "string"
  • 整数[Int]
let int: Int = 0 
  • 浮動小数点[Double]
let double: Double = 0.0
  • 真理値[Bool]
let bool: Bool = true //true or false

四則演算子と剰余演算子

  • 「+」足し算
  • 「-」引き算
  • 「*」掛け算
  • 「/」割り算
  • 「%」余りの値を算出
let plus = 1+ 1 
//2となる
let surplus = 10 % 3 
//1となる

比較演算子

結果はBool型になる

  • 「==」右辺と左辺が一致する
  • 「!=」右辺と左辺が一致しない
  • 「>」右辺が左辺よりも小さい
  • 「>=」右辺が左辺以下
  • 「<」右辺が左辺よりも大きい
  • 「<=」右辺が左辺以上
let a = 10
let b = 5
let result = a == b 
// falseとなる

[Array]型

配列を表現する型

  • 「=」の後に[]で囲いその中に値を記述する
  • 「,」で区切ることで複数定義可能
  • 値のことをエレメントとも言う
var stringArray = ["a", "b", "c"]
let a = stringArray[0]
//aとなる

メソッド

  • 「.append」要素として配列の最後に加える
var stringArray = ["a", "b", "c"]
stringArray.append("d")
//["a", "b", "c","d"]となる
  • 「.remove」配列内の要素を削除
var stringArray = ["a", "b", "c"]
stringArray.remove(at: 0)
//["b", "c"]となる
  • 「.map」配列内の全ての要素に任意の処理を与える
var stringArray = ["a", "b", "c"]
let mappedStringArray = stringArray.map({ $0 + "です" })
//"aです","bです","cです"となる
  • 「.contains」渡された値と同一の要素が配列内に存在するかどうかをBool型で返す
let intArray = [1, 2, 3]
let isContain = mappedIntArray.contains(2)
//trueとなる

[Optional]型

値の"ある","なし"の状態を持つ型

  • 値がない状態はnilとなる
  • 最後に?を付けることで型宣言できる
  • 原則そのまま四則演算できない(nilの可能性がある為)
  • 最後に!を付けるとアンラップできる
let int1: Int? = 1
let int2: Int? = 2
let result = int1! + int2!
//3となる
  • nilの場合に参照するデフォルト値を??で定義できる
let int: Int? = nil
let intNil: Int? = nil

let one = int ?? 10
let two = intNil ?? 20
let result = one + two
//30となる

if文

条件分岐を行う構文

  • trueの場合に処理が実行される
  • falseの場合処理が実行されない
let isTrue = true
if isTrue {
    print("「true」です")
}
//「true」です となる
  • 比較演算式でも可能
let number = 1
if number == 1 {
    print("「1」です")
} 
//「1」です となる

else節

  • falseだと実行される処理
let number = 3
if number == 1 {
    print("「1」です")
} else if number == 2 {
    print("「1」ではありません")
} else {
    print("「1」と「2」以外です")
}
//「1」と「2」以外です となる

オプショナルバインディング

  • Optional型を非Optional型へ変換
  • nilの場合実行されない
let optionalInt: Int? = 1
if let int = optionalInt {
    print(int)
}
//1となる

switch文

各パターンごとに実行式を切り替える構文

  • String型以外でも可能
let numberText = "three"

switch numberText {
case "one":
    print("「one」です")
case "two":
    print("「two」です")
default:
    print("「one」「two」以外です")
}
//「one」「two」以外です となる

Range型

  • 範囲を表す型
  • 「1...5」1~5 最後を含む
  • 「1..<5」1~4 最後を含まない
let range1 = 1...5
let range2 = 6..<10

let range = 10

switch range {
case range1:
    print("1〜5です")
case range2:
    print("6〜9です")
default:
    print("1〜9以外です")
}
//1〜9以外です となる

繰り返しの処理

for in文

  • 配列だけでなくRange型も可能
let array = [1, 2, 3]

for number in array {
    print(number)
}
//1 2 3 となる

while文

  • 値がfalseになるまで処理を行う
var number = 1

while number < 5 {
    print(number)
    number += 1
}
//1 2 3 4 となる
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?