Last active
July 20, 2016 11:33
-
-
Save niwatako/02f5a124e299af30ed42be2277326674 to your computer and use it in GitHub Desktop.
いろいろあった!そして最初にオプショナルのまま比較できることに気づくべきだった。お騒がせしましたm(_ _ )m #CodePiece
This file contains hidden or 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 foo: NSDate? = NSDate() | |
let interval: NSTimeInterval = 1.0 | |
let bar: String = "" | |
// Mark - | |
// Mapで出来るぞ | |
if foo.map({$0.timeIntervalSinceNow < interval}) ?? bar.isEmpty { | |
print("load") | |
} | |
// switch なら細かい条件も網羅できそう | |
if case let foo? = foo where foo.timeIntervalSinceNow < interval || bar.isEmpty {} | |
switch (foo, bar) { | |
case (let foo?, _) where foo.timeIntervalSinceNow < interval: | |
fallthrough | |
case (_, bar) where bar.isEmpty: | |
print("load") | |
} | |
// 配列に要素が残るか?を OR 演算の代わりにする技 | |
if ![(bar.isEmpty ? true : nil), (foo?.timeIntervalSinceNow < interval ? true : nil)].flatMap({ $0 }).isEmpty { | |
print("load") | |
} | |
// オプショナルのままで比較できたじゃん | |
if (foo != nil && foo?.timeIntervalSinceNow < interval ) || bar.isEmpty { | |
print("load") // foo?.timeIntervalSinceNow がnil の時intervalより必ず小さい扱いになるのでnilチェックは必要 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment