LoginSignup
30
29

More than 5 years have passed since last update.

The Swift Programming Language - Basic Operators(基本演算子) をまとめる

Last updated at Posted at 2014-06-04

The Swift Programming Language をまとめるトップ

Basic Operators(基本演算子)

C言語の基本を踏襲している
算術ようの演算子(+,-,*,/,%)は、値が overflow した時にエラーになるのを防ぐ処理ができる
Advanced Operators で後述してある
* 簡単な例

var potentialOverflow = Int16.max
// potentialOverflow は 32767 と等価, Int16 の最大値を保持
potentialOverflow += 1
// 最大値に 1 を足すので overflow エラーになる

var willOverflow = UInt8.max
// willOverflow は 255 と等価, UInt8 の最大値を保持
willOverflow = willOverflow &+ 1
// willOverflow は 0 になる

後、Cとは異なる所で、残余演算(remainder:%)は floating point numbers で計算できる
範囲演算として (a..b) や (a...b) といったものがある
Advanced Operators については Advanced Operators のセクションにて

Terminology(用語)

  • Unary(単項演算子)
    • 符号をつけるもの、(-a, !b, i++)のたぐい
  • Binary (バイナリ演算子)
    • 2つのターゲットからなるもの、(2 + 3)のたぐい
  • Ternary(三項演算子)
    • C言語と同様の、(a ? b : c)のたぐい

Assignment Operator(代入演算子)

(a = b)のように、a に b の値を代入する

let b = 10
var a = 5
a = b;
// a が 10 になる

tuple の例、分解して代入する

let (x, y) = (1, 2)
// x は 1 で y は 2

C言語とは異なり、(=)代入演算子は値を返さない

if x = y {
    // x = y は値を返さないから、無効な処理
}

Swift 的にいうと、代入演算が値を返さないのは、(==)を(=)書き間違えた時に起こるエラーがなくなるよとのこと

Arithmetic Operators(算術演算)

基本形が以下

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

1 + 2 // は 3
5 - 3 // は 2
2 * 3 // は 6
10.0 / 2.5 // は 4.0

C言語やObjCとは異なり、overflow 値がデフォルトで有効でないので、overflow する場合、(a &+ b)とする(Advanced Operators にて別途紹介)

文字の場合

"hellow, " + "world" // は "hello, world" となる

二つの一文字を繋ぐと新しい String になる
(+=)でも繋ぐ事ができる

let dog: Character = "?"
let cow: Character = "?"
let dogCow = dog + cow
// dogCow は "??"

Remainder Operator(残余演算)

(%)のこと(a % b)の場合、a を b で割った結果の残りの数

9 % 4 // は 1

Floating-Point Remainder Calculations(Floating-Point の残余演算)

C言語やObjCとは異なり、Floating-Point が使える

8 % 2.5 // は 0.5

Increment and Decrement Operators(加算子/減算子)

C言語と同様(++)(--) を使う
Prefix と Postfix で使える

var i = 0
++i // は 1

var a = 0
let b = ++a // a と b は 1
let c = a++ // a は 2 で c は 1

Unary Minus Operator(単項マイナス演算子)

(-)の後には whitespace はなし

let three = 3
let minuThree = -three // minusThree は -3
let plusThree = -minusThree // plusThree は 3(or - -3)

Unary Plus Operator(単項プラス演算子)

基本的に何もおこらない

let マイナス6 = -6
let これもまたマイナス6 = +マイナス6 // これもまたマイナス6 は -6

Compound Assignment Operators(複合代入演算)

C言語と同様

var a = 1
a += 2
// a は 3

NOTE
複合代入演算は値を返さない
加算と減算(++,--)とは違う動きをする

Comparison Operators(等価比較演算子)

C言語と同じものをサポート

  • Equal 等しい (a == b)
  • Not equal 等しくない (a != b)
  • Greater than より大きい (a > b)
  • Less than より小さい (a < b)
  • Greater than or equal より大きいか等しい (a >= b)
  • Less than or equal より小さいか等しい (a <= b)

NOTE
(===)と(!==)もある
 二つのオブジェクトのリファレンスが同じかどうかテストをするため
Classes and Structures で解説

Bool値を返す

1 == 1 // true
2 != 1 // true
2 > 1 // true
1 < 2 // true
1 >= 1 // true
2 <= 1 // false

if ステートメントでの例

let name = "world"
if name == "world" {
    println("hello, world")
} else {
    println("私は\(name)でじゃないよ")
}
// prints "hello, world"

Ternary Conditional Operator(三項演算子)

質問 ? 答え1 : 答え2 となる演算子

if 質問 {
答え1
} else {
答え2
}

例えば

let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
// rowHeight は 90

let contentHeight = 40
let hasHeader = true
var rowHeight = contentHeight
if hasHeader {
    rowHeight = rowHeight + 50
} else {
    rowHeight = rowHeight + 20
}

と同じ

Range Operators(範囲演算子)

二種類の範囲演算子がある(a...b)と(a..b)

Closed Range Operator(クローズド範囲演算子)

for index in 1...5 {
    println("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25

Half-Closed Range Operator(ハーフクローズド範囲演算子)

(a..<b)で範囲をしていして、b を含まない
Array を展開する時等に使う

let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
    println("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack

Logical Operators(論理演算子)

C言語にもある三つの基本的な論理演算子をサポート

  • Logical NOT 否定 (!a)
  • Logical AND 両方 (a && b)
  • Logical OR どちらか (a || b)

他、基本的な動作

30
29
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
30
29