Comments (7)
You first example isn't quite right. The first example does not need the if statement:
someOptional?.someFunc()
or
if let s = someOptional {
s.someFunc()
}
My vote is to use the first form when you only perform a single operation on the optional, but to use the second form if you need to perform multiple operations. For example:
if let s = someOptional {
s.someFunc()
s.someOtherFunc()
}
from swift-style-guide.
Yeah, I wasn't clear with my examples. I'm really only interested in the multiline examples -- things where you want to use an if statement.
I agree for single line things we should do the ? inline.
--------- Original Message --------- Subject: Re: [swift-style-guide] unwrapping optionals (#18)
From: "ColinEberhardt" [email protected]
Date: 6/13/14 1:16 am
To: "raywenderlich/swift-style-guide" [email protected]
Cc: "elephantronic" [email protected]
You first example isn't quite right. The first example does not need the if statement:
someOptional?.someFunc() or
if let s = someOptional { s.someFunc() } My vote is to use the first form when you only perform a single operation on the optional, but to use the second form if you need to perform multiple operations. For example:
if let s = someOptional { s.someFunc() s.someOtherFunc() } -
Reply to this email directly or view it on GitHub.
from swift-style-guide.
I like the second style myself since it avoids the unwrapping within a nice familiar let. But it does depend on naming the optional properly to make it read nicely.
from swift-style-guide.
I think it should be like this:
Use optional binding: if let s = so { }
when you only need to unwrap once and have multiple operations to perform on so
Use optional chaining: so?.moreOptionals?.function()
if you need to unwrap more than once or only have one operation to perform on so
if so { } else { }
should only be used if you just need to check for so
's existence and thus it doesn't make sense to bind it to another variable
from swift-style-guide.
Agree completely with @jackwu95
from swift-style-guide.
I agree also with @jackwu95
from swift-style-guide.
Agreed also, let's close this one out.
from swift-style-guide.
Related Issues (20)
- Make the linter a dotfile HOT 2
- [email protected] HOT 1
- New line in functions. Not good. HOT 2
- Please add `no_extension_access_modifier` as a default SwiftLint rule HOT 1
- Please Add SwiftUI style guide
- https://ideas.accredible.com/changelog/functionality-for-a-messagenotification-to-be-added-over-an-issued-credential
- edge://credits/ HOT 1
- Clean code
- Function Declarations HOT 1
- 4-space indentation. HOT 2
- False positive from array_constructor custom rule
- how to write switch statement HOT 1
- Preferred type inference of static vars inside extensions HOT 4
- Pls update Readme file to reflect the newest changes in `if let` syntax introduced in Swift 5.7 HOT 1
- unused_import does not seem to be working HOT 1
- Function Calls vs Function Declarations HOT 2
- `unused_import` should be listed in the `analyzer_rules`
- SwiftLint 'attributes' rule flags property wrappers HOT 2
- Function calls format
- Revisit ternary operator section
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-style-guide.