• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
More elegance than a long if/else
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

More elegance than a long if/else


  • Subject: More elegance than a long if/else
  • From: "Eric E. Dolecki" <email@hidden>
  • Date: Fri, 10 Mar 2017 15:41:53 +0000

I have this super simple code, but I'd like to whittle it down to something
a lot smaller - basically looking for multiples of 10 (100-0) for a value.
I need coffee, what's a great way to do this in Swift 3?

if _myBatteryLevel >= 90 {
            batteryIcon.image = UIImage(named: "10")
        } else if _myBatteryLevel >= 80 {
            batteryIcon.image = UIImage(named: "9")
        } else if _myBatteryLevel >= 70 {
            batteryIcon.image = UIImage(named: "8")
        } else if _myBatteryLevel >= 60 {
            batteryIcon.image = UIImage(named: "7")
        } else if _myBatteryLevel >= 50 {
            batteryIcon.image = UIImage(named: "6")
        } else if _myBatteryLevel >= 40 {
            batteryIcon.image = UIImage(named: "5")
        } else if _myBatteryLevel >= 30 {
            batteryIcon.image = UIImage(named: "4")
        } else if _myBatteryLevel >= 20 {
            batteryIcon.image = UIImage(named: "3")
        } else if _myBatteryLevel >= 10 {
            batteryIcon.image = UIImage(named: "2")
        } else if _myBatteryLevel >= 0 {
            batteryIcon.image = UIImage(named: "1")
        }

Thanks for thinking about my lame code.
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: More elegance than a long if/else
      • From: Jeff Kelley <email@hidden>
  • Prev by Date: Re: needsDisplay and subviews
  • Next by Date: Re: More elegance than a long if/else
  • Previous by thread: Re: NSOutlineView isn't restoring persistent state for expanded/collapsed
  • Next by thread: Re: More elegance than a long if/else
  • Index(es):
    • Date
    • Thread