# How would you do this?

**URL:** <https://forums.kodeco.com/t/how-would-you-do-this/65991>\
**Category:** iOS\
**Created:** [March 30, 2019, 6:39am UTC](https://forums.kodeco.com/t/how-would-you-do-this/65991 "2019-03-30T06:39:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bar24](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bar24/32/104873_2.png) [@bar24](https://forums.kodeco.com/u/bar24)\
**Post date:** [March 30, 2019, 6:39am UTC](https://forums.kodeco.com/t/how-would-you-do-this/65991/1 "2019-03-30T06:39:49Z")

</div>

Hi All  
Long time listener, first time caller 😉

I am doing a redesign of an app I have on the App Store, [https://itunes.apple.com/au/app/pdta/id1368902048?mt=8](https://itunes.apple.com/au/app/pdta/id1368902048?mt=8) and am looking at adding some different dive tables.

For the non-divers decompression tables are broken into depth and time increments, at the end of that you get a repetitive group and after a surface interval you are allocated a repetitive factor which is used for the next dive. So a dive may look like 18 metres for 30 mins, RG of D, after a surface interval of 45 mins I have an Rf of 1.5. The depth increments are usually 3 metres (10 feet) and dive times can be as small as 5 mins and as large as 120 mins…anyway the bottom line is there is a lot of combinations that can result.

Given that PDTA was my first app I wrote a massive if, else statement…I am wondering how some more experienced developers would tackle the problem.

To get an indication of what a dive table looks like have a look at this link:

> **[Dive Tables](https://divetables.com.au)**
>
> How To Do Scuba Dive Tables . Understanding Scuba Dive Tables. Recreational Dive Planners: Dive Tables explained. The most common scuba dive table used by recreational diving companies and enthusiasts is the recreational dive planner.

Any advice appreciated.

Thanks in advance  
Brett

---

<div class="post-metadata">

**Author:** ![syedfa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/syedfa/32/101500_2.png) [@syedfa](https://forums.kodeco.com/u/syedfa)\
**Post date:** [March 31, 2019, 7:03pm UTC](https://forums.kodeco.com/t/how-would-you-do-this/65991/2 "2019-03-31T19:03:21Z")

</div>

@bar24 Thanks very much for your question!

The first alternative to an if/else statement that comes to mind is a Switch/Case statement. Swift allows for more complex use of the Switch/Case which makes it very powerful, and convenient to use.

Another approach could be to use either a dictionary, or a plist file for the sake of using key/value pairs. Instead of using multiple if/else statements, you simply pass the value you have to the collection holding all of the key/value pairs, and simply retrieve the value that matches the corresponding property you passed in.

Another thing I would do is perhaps create an extension method where you would put all of this additional code so that it doesn’t clutter up your work. This way, the method you create does all of the dirty work for you, and all you do is simply pass a value into it, and the function either returns the appropriate value you want, or carries out the correct action. 🙂

I hope this helps!

All the best!

---

<div class="post-metadata">

**Author:** ![sgerrard](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sgerrard/32/93812_2.png) [@sgerrard](https://forums.kodeco.com/u/sgerrard)\
**Post date:** [April 1, 2019, 2:58am UTC](https://forums.kodeco.com/t/how-would-you-do-this/65991/3 "2019-04-01T02:58:20Z")

</div>

I did a lot of table lookups in an estimating program. This is an example of the kind of thing I would do, done in a playground. You make the tables once, and can then look things up in them quite easily.

I used the last three rows of data from the dive table shown at your second link.  
 ![31%20PM](https://files.chunter.kodeco.com/original/3X/3/e/3e819b8002b9f86d5fa901b46cffd912376a4f54.png)

By just making a long list, it works fine when rows don’t have the same number of columns.

```auto
struct TableEntry {
  let meters: Int
  let group: String
  let minutes: Int
  let adjusted: Int
}

var tableA: [TableEntry] = []

func makeTableA() {
  tableA.removeAll()
  tableA.append(TableEntry(meters: 33, group: "A", minutes: 3, adjusted: 12))
  tableA.append(TableEntry(meters: 33, group: "B", minutes: 6, adjusted: 9))
  tableA.append(TableEntry(meters: 33, group: "C", minutes: 10, adjusted: 5))
  tableA.append(TableEntry(meters: 33, group: "D", minutes: 13, adjusted: 2))
  tableA.append(TableEntry(meters: 36, group: "A", minutes: 3, adjusted: 7))
  tableA.append(TableEntry(meters: 36, group: "B", minutes: 6, adjusted: 4))
  tableA.append(TableEntry(meters: 36, group: "C", minutes: 9, adjusted: 1))
  tableA.append(TableEntry(meters: 39, group: "A", minutes: 3, adjusted: 2))
}

// gets first entry with specified meters and minutes >= specified minutes
// return nil if not found
func lookupEntry(table: [TableEntry], meters: Int, minutes: Int) -> TableEntry? {
  return table
    .filter { $0.meters == meters && $0.minutes >= minutes }
    .first
}

// make the table
makeTableA()

// Test it
if let test1 = lookupEntry(table: tableA, meters: 36, minutes: 5) {
  print (test1)
}

```

**TableEntry(meters: 36, group: “B”, minutes: 6, adjusted: 4)**

---

<div class="post-metadata">

**Author:** ![bar24](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bar24/32/104873_2.png) [@bar24](https://forums.kodeco.com/u/bar24)\
**Post date:** [April 1, 2019, 4:04am UTC](https://forums.kodeco.com/t/how-would-you-do-this/65991/4 "2019-04-01T04:04:41Z")

</div>

Hi syedfa and sgerrard

Thanks for the replies, I really appreciate the time it takes to respond, thanks.

I wrote a switch/case statement in playground for an exercise a few weeks ago…and whilst it works well it is pretty big when I write all of the depth/time variables from 6 to 57 metres, time brackets as big as 120 and as small as 5 minutes, the Rg at the end of that and then the decompression stops that may be required…eg (see below…the first value is depth, the second is time in minutes)

case (54…57, 46…50):  
print(“You have entered Extreme Exposure”)  
print(“Deco: 135 minutes at 3 metres”)  
print(“Deco: 50 minutes at 6 metres”)  
print(“Deco: 22 minutes at 9 metres”)  
print(“Deco: 8 minutes at 12 metres”)  
print(“Deco: 6 minutes at 15 metres”)  
print(“Deco: 5 minutes at 18 metres”)  
print(“Deco: 3 minutes at 21 metres”)  
print(“Deco: 3 minutes at 24 metres”)

I like the suggestion of an extension method…I assume this is another swift file in the project that the view controller refers to? How do I grab the information from that file? Any suggestions for reading you could recommend.

sggerard: thanks mate for the suggestion…as a newbie to this I will need to have a look at your suggestion and work my way through it to understand it.

I think whichever way I go it will need a fair bit of work, but its all a part of learning.!

Thanks  
Brett

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [September 12, 2019, 10:39pm UTC](https://forums.kodeco.com/t/how-would-you-do-this/65991/5 "2019-09-12T22:39:49Z")

</div>

Closed on September 12, 2019, 10:39pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
