let initialBits: UInt8 = 0b00001111
let invertedBits = ~initialBits // equals 11110000 240
let firstSixBits: UInt8 = 0b11111100
let lastSixBits: UInt8 = 0b00111111
let middleFourBits = firstSixBits & lastSixBits // equals 00111100
let someBits: UInt8 = 0b10110010
let moreBits: UInt8 = 0b01011110
let combinedbits = someBits | moreBits // equals 11111110
let firstBits: UInt8 = 0b00010100
let otherBits: UInt8 = 0b00000101
let outputBits = firstBits ^ otherBits // equals 00010001
let shiftBits: UInt8 = 4 // 00000100 in binary
shiftBits << 1 // 00001000
shiftBits << 2 // 00010000
shiftBits << 5 // 10000000
shiftBits << 6 // 00000000
shiftBits >> 2 // 00000001
์์๊ฐ์ ์ ์ฅํ๊ธฐ ์ํด ์ธ์ฝ๋ฉ, ๋์ฝ๋ฉ ํ๋ ์์ .
let pink: UInt32 = 0xCC6699
let redComponent = (pink & 0xFF0000) >> 16 // redComponent is 0xCC, or 204
let greenComponent = (pink & 0x00FF00) >> 8 // greenComponent is 0x66, or 102
let blueComponent = pink & 0x0000FF // blueComponent is 0x99, or 153
์ฅ์
1. ๋ถํธ๋นํธ๋ ๊ทธ๋ฅ ๋๊ณ ๋๋จธ์ง ์๋ฆฌ๋ก ์ด์ง ๋ง์
์ ํ๊ณ , int ํฌ๊ธฐ๋ฅผ ๋ฒ์ด๋๋ ๊ฒ ์๋ฅด๋ฉด ๋จ.
2. ๋นํธ๋ฅผ ์ผ์ชฝ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ ์ ์๋ค.
ํ์ง๋ง, ์ผ์ชฝ์ ๋น์ด์๋ ๋ชจ๋ ๋นํธ์ 0์ด ์๋๋ผ ํด๋น ๋ถํธ ๋นํธ๋ฅผ ์ฑ์ฐ๊ฒ ๋๋ค. -> ๋ถํธ๋ฅผ ์ ์งํ๋ฉด์ ์ฐ์ฐ.
&+
&-
&*
๋ฅผ ์ด์ฉํ์.var unsignedOverflow = UInt8.min
// unsignedOverflow equals 0, which is the minimum value a UInt8 can hold
unsignedOverflow = unsignedOverflow &- 1
// unsignedOverflow is now equal to 255
var signedOverflow = Int8.min
// signedOverflow equals -128, which is the minimum value an Int8 can hold
signedOverflow = signedOverflow &- 1
// signedOverflow is now equal to 127
2 + 3 % 4 * 5 // this equals 17
2 + ((3 % 4) * 5)
NOTE : C์ธ์ด ์ฐ์ ์์์ ์๋ฒฝํ๊ฒ ์ผ์นํ์ง ์์ ์๋ ์์ผ๋ฏ๋ก ํฌํ ์ ์ฃผ์ํ์.
// ๋ฒกํฐ์ ๋ง์
์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ
struct Vector2D {
var x = 0.0, y = 0.0
}
extension Vector2D {
static func + (left: Vector2D, right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x, y: left.y + right.y)
}
}
let vector = Vector2D(x: 3.0, y: 1.0)
let anotherVector = Vector2D(x: 2.0, y: 4.0)
let combinedVector = vector + anotherVector
// combinedVector is a Vector2D instance with values of (5.0, 5.0)
-3
+3
b!
extension Vector2D {
static prefix func - (vector: Vector2D) -> Vector2D {
return Vector2D(x: -vector.x, y: -vector.y)
}
}
let positive = Vector2D(x: 3.0, y: 4.0)
let negative = -positive
// negative is a Vector2D instance with values of (-3.0, -4.0)
let alsoPositive = -negative
// alsoPositive is a Vector2D instance with values of (3.0, 4.0)
extension Vector2D {
static func += (left: inout Vector2D, right: Vector2D) {
left = left + right
}
}
var original = Vector2D(x: 1.0, y: 2.0)
let vectorToAdd = Vector2D(x: 3.0, y: 4.0)
original += vectorToAdd
// original now has values of (4.0, 6.0)
NOTE : = ๊ณผ ์ผํญ์ฐ์ฐ์๋ ์ค๋ฒ๋ก๋ฉ ๋ถ๊ฐ๋ฅ.
==
!=
๋ฅผ ์ ์ํ ์ ์๋ค.extension Vector2D: Equatable {
static func == (left: Vector2D, right: Vector2D) -> Bool {
return (left.x == right.x) && (left.y == right.y)
}
}
let twoThree = Vector2D(x: 2.0, y: 3.0)
let anotherTwoThree = Vector2D(x: 2.0, y: 3.0)
if twoThree == anotherTwoThree {
print("These two vectors are equivalent.")
}
// Prints "These two vectors are equivalent."
prefix operator +++
extension Vector2D {
static prefix func +++ (vector: inout Vector2D) -> Vector2D {
vector += vector
return vector
}
}
var toBeDoubled = Vector2D(x: 1.0, y: 4.0)
let afterDoubling = +++toBeDoubled
// toBeDoubled now has values of (2.0, 8.0)
// afterDoubling also has values of (2.0, 8.0)
prefix operator **
prefix func ** (value: Int) -> Int {
return value * value
}
**5 // 25
// +- ๋ + - ๋ฅผ ์ฐ๋๊น ๊ทธ๊ฒ๊ณผ ๊ฐ์ ์ฐ์ ์์๋ฅผ ๊ฐ์ง๋ค.
infix operator +-: AdditionPrecedence
extension Vector2D {
static func +- (left: Vector2D, right: Vector2D) -> Vector2D {
return Vector2D(x: left.x + right.x, y: left.y - right.y)
}
}
let firstVector = Vector2D(x: 1.0, y: 2.0)
let secondVector = Vector2D(x: 3.0, y: 4.0)
let plusMinusVector = firstVector +- secondVector
// plusMinusVector is a Vector2D instance with values of (4.0, -2.0)
NOTE : ์ ๋์ฌ, ์ ๋ฏธ์ฌ ์ฐ์ฐ์๋ ์ฐ์ ์์๋ฅผ ์ง์ ํ์ง ์์ง๋ง, ๊ฐ์ ํผ์ฐ์ฐ์์ ์ ๋ ์ ๋ฏธ๊ฐ ๋ค ๋ถ์ผ๋ฉด ์ ๋ฏธ์ฌ๊ฐ ๋จผ์ ์ ์ฉ๋๋ค.
if
for
๊ฐ์ ๊ตฌ๋ฌธ์ ์ถ๊ฐํ ์ ์๋ค.protocol Drawable {
func draw() -> String
}
struct Line: Drawable {
var elements: [Drawable]
func draw() -> String {
return elements.map { $0.draw() }.joined(separator: "")
}
}
struct Text: Drawable {
var content: String
init(_ content: String) { self.content = content }
func draw() -> String { return content }
}
struct Space: Drawable {
func draw() -> String { return " " }
}
struct Stars: Drawable {
var length: Int
func draw() -> String { return String(repeating: "*", count: length) }
}
struct AllCaps: Drawable {
var content: Drawable
func draw() -> String { return content.draw().uppercased() }
}
let name: String? = "Ravi Patel"
let manualDrawing = Line(elements: [
Stars(length: 3),
Text("Hello"),
Space(),
AllCaps(content: Text((name ?? "World") + "!")), // ์ด์ฐ ๋ณต์กํด... ResultBuilder ๋ฅผ ์ฌ์ฉํ์.
Stars(length: 2),
])
print(manualDrawing.draw())
// Prints "***Hello RAVI PATEL!**"
@resultBuilder ๋ผ๋ ํค์๋๋ฅผ ๋ถ์ฌ์ ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ธํ๋ค.
@resultBuilder
struct DrawingBuilder {
static func buildBlock(_ components: Drawable...) -> Drawable {
return Line(elements: components)
}
static func buildEither(first: Drawable) -> Drawable {
return first
}
static func buildEither(second: Drawable) -> Drawable {
return second
}
}
func draw(@DrawingBuilder content: () -> Drawable) -> Drawable {
return content()
}
func caps(@DrawingBuilder content: () -> Drawable) -> Drawable {
return AllCaps(content: content())
}
func makeGreeting(for name: String? = nil) -> Drawable {
let greeting = draw {
Stars(length: 3)
Text("Hello")
Space()
caps {
if let name = name {
Text(name + "!")
} else {
Text("World!")
}
}
Stars(length: 2)
}
return greeting
}
let genericGreeting = makeGreeting()
print(genericGreeting.draw())
// Prints "***Hello WORLD!**"
let personalGreeting = makeGreeting(for: "Ravi Patel")
print(personalGreeting.draw())
// Prints "***Hello RAVI PATEL!**"
if-else ์์ ๊ฐ๊ฐ DrawingBuilder ์ ๋ฉ์๋๋ฅผ ํธ์ถํ ์๋ ์๋ค.
let capsDrawing = caps {
let partialDrawing: Drawable
if let name = name {
let text = Text(name + "!")
partialDrawing = DrawingBuilder.buildEither(first: text)
} else {
let text = Text("World!")
partialDrawing = DrawingBuilder.buildEither(second: text)
}
return partialDrawing
}
extension DrawingBuilder {
static func buildArray(_ components: [Drawable]) -> Drawable {
return Line(elements: components)
}
}
let manyStars = draw {
Text("Stars:")
for length in 1...3 {
Space()
Stars(length: length)
}
}
for ๋ฌธ ์ง์๋ ๊ฐ๋ฅํ๋ค.
extension DrawingBuilder {
static func buildArray(_ components: [Drawable]) -> Drawable {
return Line(elements: components)
}
}
let manyStars = draw {
Text("Stars:")
for length in 1...3 {
Space()
Stars(length: length)
}
}
์ด๋ป๊ฒ ๋ณํ๋๋์ง๋ ์ฌ๊ธฐ
์ฐธ๊ณ
https://bbiguduk.gitbook.io/swift/language-guide-1/advanced-operators