DATA

Hello. I have data I need to add to my new app.

Below is a simple example of my plants and the data I am adding to my app

  1. Aloe Vera

a. Medicinal Properties (skin healer) b. How to harvest (Cut from the middle) c. How much water does it need (once every three days) d. Recipe (cut the leaf, use the gel inside for your rash)

  1. Almond

a. Medicinal Properties (skin healer) b. How to harvest (cut the young leaves) c. How much water does it need (daily) d. Recipe (soak in water overnight, rinse, blend)

  1. Raspberry

a. Medicinal Properties (skin healer) b. How to harvest (cut from mid branch) c. How much water does it need (twice a day) d. Recipe (mix raspberry with coconut water and blend.)

I understand that the above date (information about the four plants) needs to be organized in a Numbers file.

The questions are:

  1. Where in the app do you import the Numbers file?
  2. What is the code? I mean the code that you write for this. So when you click on the Raspberry button, the app jumps to another page with four other buttons (Medicinal Properties, How to harvest, How much water does it need, recipe?
  3. Is there a video that shows this?

Thanks so much for your time.

I don't know what you mean about the 'Numbers' file. To start, you should create a struct to store your plant's data and collectively, form an array of plants. Something like the following

struct Plant: Hashable, Identifiable {
    let id = UUID()
    var plantName = ""
    var medicinalProperties: [MedProp]? = nil
    var harvestInstructions = ""
    var waterRequirement = ""

    init?(plantName: String = "", medicinalProperties: [MedProp]? = nil, harvestInstructions: String = "", waterRequirement: String = "") {
        self.plantName = plantName        
        if let medicinalProperties = medicinalProperties {
            self.medicinalProperties = medicinalProperties
	      }
        self.harvestInstructions = harvestInstructions
        self.waterRequirement = waterRequirement
    }
}

struct MedProp: Hashable, Identifiable {
    let id = UUID()

    ...

}

There's a lot more to this than just the data models. If you want to learn SwiftUI, I suggest following Apple's excellent SwiftUI Landmarks tutorial. This will help you through the whole process while introducing you to SwiftUI.

In addition to @SpaceMan 's answer you can import some sort of database:

You can export a CSV file and have something sort of like this.

Aloe Vera,     skin healer,     Cut from middle,          once every three days,    cut the leave and use the gel inside for your rash
Almond,        skin healer,     cut the young leaves,     daily,                    soak in water overnight rinse and blend
......

and then that's easy.

struct Plant: Identifiable {
    var id: UUID {
        get {
            return .init()
        }
    }
    var plantName: String
    var mdedicalProperties: String
    var harvestInstructions: String
    var waterRequirements: String
    var recipe: String
}
// load your file with your way, I'll use file as a variable for the string contents of the database
var plantModel = [Plant]
let plantLines = file.split(separator: "\n")
let plantArray2D: Array<Array<String>> = []
for i in plantLines {
    let modified = i.replacingOccurrences(of: " ", with: "")
    plantArray2D.append(i.split(separator: ","))
}
for i in plantArray2D {
    plantModel.append(Plant(plantName: i[0], medicicalProperties: i[1], harvestInstructions: i[2], waterRequirements: i[3], recipe: i[4]))
}

You can export a CSV file and have something sort of like this.

While that’ll work, if you’re doing anything serious with CSV files I strongly recommend that you check out the TabularData framework. See the code snippet below.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"


import Foundation
import TabularData

let csv = """
Aloe Vera,     skin healer,     Cut from middle,          once every three days,    cut the leave and use the gel inside for your rash
Almond,        skin healer,     cut the young leaves,     daily,                    soak in water overnight rinse and blend
"""

func main() throws {
    var options = CSVReadingOptions()
    options.hasHeaderRow = false
    let frame = try DataFrame.init(csvData: Data(csv.utf8), options: options)
    print(frame)
// ┏━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━┳…
// ┃   ┃ Column 0  ┃ Column 1            ┃ Column 2                  ┃…
// ┃   ┃ <String>  ┃ <String>            ┃ <String>                  ┃…
// ┡━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━╇…
// │ 0 │ Aloe Vera │      skin healer    │      Cut from middle      │…
// │ 1 │ Almond    │         skin healer │      cut the young leaves │…
// └───┴───────────┴─────────────────────┴───────────────────────────┴…
// 2 rows, 5 columns
}

try! main()
DATA
 
 
Q