error handling - swift, optional unwrapping, reversing if condition -
Assume that I have a function that gives alternate if error and value if success:
func foo () - & gt; times? {...}I can use the following code to work with this function:
let fooResultOpt = foo () FooResult = fooResultOpt {/
Although there are some problems with this approach for any non-trivial code:
Finally displaying the display Error in and something is easy to remember This is much better when the error handling code occurs after the call.
The correct operating code is indented from a level if we have another task to call, we have to indent once.
Typically, something like this can write with:
bar * fooResult = foo (); If (fooResult == null) {// handle error and return} // Continue right here
I found two ways to get the same code style with Swift, but I like Not so either
Let fooResultOpt = foo () if fooResult == blue {// handle error and return} // use fooResultOpt! FooResult = fooResultOpt from here! // or other variables
if I "!" I will write everywhere, it just looks bad for my taste. I can introduce another variable, but it does not look good, ideally I would like to see the following:
if! Let fooResult = foo () {// handle error and return} // is the bar type of fooResult and can be used in the top level
Do I miss something in the specification or What is the other way to write a good looking swift code?
Your assumptions are correct "Swift does not have" unauthorized if-two "syntax < P> I suspect a reason for this that the integrity of grammar can be Swift (and generally in other C-inspirational languages), if you have a statement that can tie local symbols (i.e. name is new variable and Give them the value) and it may contain a block body (for example, while, for), those blocks The block is called the Scotch, which will be inconsistent by giving a block statement bind signal for its closing scope.
It's still a fair thing, though I have to think about it and see if What does Apple do about it?
Comments
Post a Comment