if-let statement in Objective-C

I just found an equivalent to Swift’s if-let construct, and thought to share it ๐Ÿ™‚

Swift:
[swift gutter=”false”]
if let result = computeResult() {
// do something with result
}
[/swift]

Objective-C:
[objc gutter=”false”]
for(SomeClass *result = [self computeResult]; result != nil; result = nil) {
// do something with result
}
[/objc]

You no longer need an extra line to declare the variable, and also the variable is visible only inside theย if branch. Just like in Swift ๐Ÿ™‚

1 comment on if-let statement in Objective-C

Leave a Reply to snow Cancel reply

Your email address will not be published. Required fields are marked *