LoginSignup
10
3

More than 5 years have passed since last update.

Swiftで(a==1&&a==2&&a==3)を常にtrueにする

Last updated at Posted at 2018-01-22

(a==1&&a==2&&a==3)を常にtrueにするStackOverflowの問題を解いたQiita記事まとめを見て、Swiftでも書いてみました。

get副作用版

var i: Int = 0
var a: Int {
    if i == 3 { i = 0 }
    i += 1
    return i
}

print(a == 1 && a == 2 && a == 3)   // true
print(a == 1 && a == 2 && a == 3)   // true

==オーバーロード版

extension Int {
    static func ==(lhs: Int, rhs: Int) -> Bool {
        return true
    }
}

var a: Int = 0

print(a == 1 && a == 2 && a == 3)   // true
print(a == 1 && a == 2 && a == 3)   // true
10
3
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
10
3