WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Need help comparing a user entered value against a table

Forums > SwiftUI

Trying to get the user to enter an age(month) and value(weight) which gets compared against the values in the table and then the code returns where the input value lies (ex. between 1 and 2 SD).

I'm having trouble with: 1.Storing the tables. There are 10+. I tried converting from pdf to excel to JSON but it was becoming very complex. I was basically getting one JSON file for every row in the table.

2.Comparing the user entered values to the appropriate values in the table, ie. having the code select the appropriate row, and then decide between which two values our entered value lies. I thought about using dictionaries, like [ age1: [val1, val2, val3,], 1. age2: [val1, val2, val3,], ] and so on, and then using a for loop to check against each item in the array. a)I don't know if this work b)I'm having trouble getting all this table data into dictionaries. I don't want to enter all the values by hand since there are literally hundreds.

Hopelessly stuck in trying to implement this, I'd really appreciate any help.

1      

I'm doing something vaguely similar. My tables are stored as CSV files and loaded at startup like this:


struct Table {
    let id: String
    let table: [[Double]]
}

enum LoadError: Swift.Error {
    case missingTable(String)
}

private var tables = [Table]()    // used only in one program file

func loadTables() {
    guard tables.count==0 else { return }       // don't do it again

    do {
        for i in 1...18 {
            try tables.append(loadTable("t\(i)"))
        }

        for i in ["a", "b", "c", "d", "em", "ef", "fm", "ff"] {
            try tables.append(loadTable(i))
        }

        for i in ["atf-0.25", "atf-0.75", "atf0", "atm-0.25", "atm-0.75", "atm0"] {
            try tables.append(loadTable(i))
        }
    } catch LoadError.missingTable(let t) {
        fatalError("Can't load table \(t)")
    } catch {
        fatalError("Unexpected error loading tables: \(error)")
    }
}

private func loadTable(_ t: String) throws -> Table {
    guard let tableURL = Bundle.main.url(forResource: t, withExtension: "txt", subdirectory: "tables"),
          let rawText = try? String(contentsOf: tableURL)
    else { throw LoadError.missingTable(t) }

    let lines = rawText.components(separatedBy: "\n")
    var table = [[Double]]()
    for j in 0..<lines.count where lines[j].count>1 {
        let ageRow = lines[j].components(separatedBy: ",")
        let ageRowDouble = ageRow.map { Double($0) ?? 0 }
        table.append(ageRowDouble)
    }
    return (Table(id: t, table: table))
}

It's not awfully efficient, and as this is my first Swift program there may be a better way to do it, but it takes no noticeable time (on an iPhone 8) to load the 32 tables and convert the Strings to Doubles. The tables differ in size: the largest is 125 x 125 (they're the Ogden Tables, published by the UK Government, FWIW, which I've converted from Excel to CSV; this means that I know that simple decoding by comma-separation will work).

Jeremy

PS: this seems more of a Swift than a SwiftUI question, so you might be better off asking in the other forum.

1      

Thank you for your reply Jeremy! That seems pretty neat, I'll see if I can manage something similar. I'll ask on the other forum too, thanks!

1      

Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Fernando's book will guide you in fixing bugs in three real, open-source, downloadable apps from the App Store. Learn applied programming fundamentals by refactoring real code from published apps. Hacking with Swift readers get a $10 discount!

Read the book

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.