Last active
November 13, 2019 10:24
-
-
Save KentaKudo/f1b42972ff4238ce90f7 to your computer and use it in GitHub Desktop.
Swift演算子まとめ ref: https://qiita.com/KentaKudo/items/a1dd2eb0bf37302c1c81
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let result = 2 + 3 + 4 // 左から | |
var str1: String? | |
var str2: String? | |
let string = str1 ?? str2 ?? "string" // 右から | |
let int1 = 1 | |
let int2 = 2 | |
let int3 = 3 | |
int1 == int2 == int3 // エラー |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (x, y) = (1, 2) | |
// x = 1, y = 2 | |
if x = y { // 判定する値が返らない | |
// エラー | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0...100 ~= 15 // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0...100 ~= 100 // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0..<100 ~= 100 // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isSelected: Bool = true | |
!isSelected // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bit: UInt8 = 0b00001111 // = 15 | |
// ↓ | |
~bit // 0b11110000 = 240 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bit1: UInt8 = 0b11111100 // = 252 | |
let bit2: UInt8 = 0b11110000 // = 240 | |
// ↓ | |
bit1 & bit2 // 0b11110000 = 240 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bit1: UInt8 = 0b11111100 // = 252 | |
let bit2: UInt8 = 0b11110000 // = 240 | |
// ↓ | |
bit1 | bit2 // 0b11111100 = 252 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let bit1: UInt8 = 0b11111100 // = 252 | |
let bit2: UInt8 = 0b11110000 // = 240 | |
// ↓ | |
bit1 ^ bit2 // 0b00001100 = 12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let unsignedInt: UInt8 = 0b11111111 // 255 | |
// ↓ | |
unsignedInt << 4 // 0b11110000 = 240 | |
unsignedInt >> 4 // 0b00001111 = 15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let signedPlusInt: Int8 = 127 // 0b0_1111111 | |
signedPlusInt >> 4 // 0b0_0000111 = 7 | |
signedPlusInt << 4 // 0b1_1110000 = -16 | |
// 空いた桁を0で埋めている | |
let signedMinusInt: Int8 = -127 // 0b1_0000000 | |
signedMinusInt >> 4 // 0b1_1111000 = -8 | |
signedMinusInt << 4 // 0b0_0001111 = 16 | |
// 空いた桁を1で埋めている |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i: Int8 = 127 // Int8の最大数 | |
i++ // エラー |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var int: Int = 3 | |
int += 5 | |
// ↓同じ | |
int = int + 5 | |
var bit: UInt8 = 0b11110000 // 240 | |
bit <<= 4 // 0b00000000 = 0 | |
// ↓同じ | |
bit = bit << 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str: Any = "string" | |
str is String // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str: String = "string" | |
str as NSString |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let str: Any = "string" | |
str as! Int // 実行時エラー | |
str as? Int // nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// * を + に書き換え | |
func * (left: Int, right: Int) -> Int { | |
return left + right | |
} | |
2 * 3 // 5 | |
// 左側;String、右側:Intに + を定義 | |
func + (left: String, right: Int) -> String { | |
return left + String(right) | |
} | |
"Number: " + 3 // "Number: 3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 累乗を計算する演算子を定義 | |
infix operator ** { associativity left precedence 161 } | |
func ** (left: Int, right: UInt) -> Int { | |
var powered = 1 | |
for _ in 0..<right { | |
powered *= left | |
} | |
return powered | |
} | |
2 + 3 ** 2 // 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = (b * 整数) + 余り |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-9 % 4 // (4 * -2) - 1 | |
// -1 | |
-9 % -4 // (-4 * 2) - 1 | |
// -1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 % 2.5 // 0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = 0, b = 0 | |
let c = ++a // 1 | |
let d = b++ // 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var i: Int8 = 127 // Int8の最大数 | |
i &+ 1 // -128: Int8の最小数 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let isSelected = true | |
if isSelected { | |
print("true") | |
} else { | |
print("false") | |
} | |
// ↓同じ | |
isSelected ? print("true") : print("false") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let alpha: Int? = nil | |
let beta: Int = 10 | |
alpha != nil ? alpha! : beta | |
// ↓同じ | |
alpha ?? beta |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment