Code Monkey home page Code Monkey logo

Comments (7)

ColinEberhardt avatar ColinEberhardt commented on May 29, 2024

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.

 avatar commented on May 29, 2024

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.

gregheo avatar gregheo commented on May 29, 2024

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.

jackwu95 avatar jackwu95 commented on May 29, 2024

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.

rnystrom avatar rnystrom commented on May 29, 2024

Agree completely with @jackwu95

from swift-style-guide.

ColinEberhardt avatar ColinEberhardt commented on May 29, 2024

I agree also with @jackwu95

from swift-style-guide.

rwenderlich avatar rwenderlich commented on May 29, 2024

Agreed also, let's close this one out.

from swift-style-guide.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.