How would you do this?

Hi All
Long time listener, first time caller :wink:

I am doing a redesign of an app I have on the App Store, 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:

Any advice appreciated.

Thanks in advance
Brett

@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. :slight_smile:

I hope this helps!

All the best!

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

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

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)

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

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