2
2

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 5 years have passed since last update.

swiftの演算子に関して

Last updated at Posted at 2015-11-03

##Comparison operators 比較演算子

swift演算子に関して記述していきます。

Equal to (a == b)
Not equal to (a != b)
Greater than (a > b)
Less than (a < b)
Greater than or equal to (a >= b)
Less than or equal to (a <= b)

##Ternary Conditional Operator 三項条件演算子

(question ? answer1 : answer2) = 条件式 ? 式1 : 式2 式の評価がtrueなら式1、falseなら式2を返す。

gender == 0 ? print("male") : print("female")
The example above will evaluate the expression gender == 0. If true, it prints "male". Otherwise, it prints "female".

上記の例では、gender == 0を評価し、trueならmaleを、falseならfemaleを出力する。
Guess the value stored in the "rowHeight" variable value of the following code:
let height = 40 
let isCheck = true 
let rowHeight = height + (isCheck ? 50 : 20) // ??

##Range operators 範囲演算子

Range Operators

Swift offers two range operators, which are shortcuts for expressing a range of values.

The closed range operator (a...b) defines a range running from a to b, and includes the values a and b. The value of a must not be greater than that of b.
1...3 //1, 2, 3

The half-open range operator (a..<b) defines a range that runs from a to b, but does not include b. It is said to be half-open because it contains its first value, but not its final value. As with the closed range operator, the value of a must not be greater than that of b.
1..<3 //1, 2

##Logical operators 論理演算子

Logical Operators

Logical operators modify or combine the Boolean logic values true and false. Swift supports the three standard logical operators found in C-based languages:

Logical NOT operator (!a): Inverts a Boolean value so that true becomes false and false becomes true.

Logical AND operator (a && b): Creates logical expressions in which both values must be true for the overall expression to be true.

Logical OR operator (a || b): An infixed operator made from two adjacent pipe characters. It creates logical expressions in which only one of the two values has to be true for the overall expression to be true.

##Optional type or value オプショナル型

オプショナル型は「その値が宣言した型(Int)か値がない状態を示すnilを保持していること」を表している。

Optionals are used in situations in which a value may be absent. 
An optional says:
-There is a value, and it equals x
or
-There isn't a value at all
var myCode: Int? = 404

An optional Int is written as Int?, not Int. The question mark indicates that the value contained within is optional, meaning that it might contain some Int value, or it might contain no value at all. 

参照 https://programmer-engine.work/archives/16831

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?