Last active
June 21, 2023 13:11
-
-
Save kakajika/0bb3fd14f4afd5e5c2ec to your computer and use it in GitHub Desktop.
A port of Kotlin's scope functions to Swift.
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
protocol ScopeFunc {} | |
extension ScopeFunc { | |
@inline(__always) func apply(block: (Self) -> ()) -> Self { | |
block(self) | |
return self | |
} | |
@inline(__always) func letIt<R>(block: (Self) -> R) -> R { | |
return block(self) | |
} | |
} | |
extension NSObject: ScopeFunc {} |
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 imageView = UIImageView().apply { | |
$0.contentMode = .ScaleAspectFit | |
$0.opaque = true | |
$0.frame = CGRectMake(...) | |
$0.setImageWithURL(NSURL(string: "...")) | |
} | |
CAKeyframeAnimation(keyPath: "transform.rotation").apply { | |
$0.beginTime = CACurrentMediaTime()+delay | |
$0.duration = 0.2 | |
$0.repeatCount = 10 | |
$0.values = [ 0.005*M_PI, -0.005*M_PI, 0.005*M_PI ] | |
imageView.layer.addAnimation($0, forKey: "wiggle") | |
} |
@Guang1234567 the use of the Int constructor is just for the purpose of illustration, the point is that let can useful with optional chaining.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@langleyd
note:
So
equals
equals