Hello all, I want to create an app right now that can generate SwiftUI views from csv tables. They have two columns, one with a name and the other contains a hex color code (e.g. Apple,#FFFFFF). I want to read the csv file and then use the first column of each row as text and the second row of the column as the color of the text. But I cannot seem to get this to work. Currently I read the csv with: var filecontent = String(contentsOf: file); where file is defined as a URL(fileURLWithPath: „colors.csv“) How do I now separate the two columns from each other and how do I implement this in a ForEach struct to get this kind of „prodcedural“ item generation?
CSV to SwiftUI
How do I now separate the two columns from each other
You could use a HStack
.
how do I implement this in a ForEach struct
If I understand correctly,
- I would create 2 computed var to hold each of the columns content.
- And use these var in 2 ForEach in the HStack
I must have been imprecise, my problems are:
- I don’t know how to read the two different columns per row of a csv into two seperate lists
- I want to have one vstack containing a scrollable list of all the company names in the corresponding color. E.g. with the csv file:
Apple,#FFFFFF Adobe,#FF0000
I want a white text element reading “Apple” and a red text with “Adobe underneath
First, you have to separate the reading of the data into a model from the rendering the model data into the GUI!
ForEach
is only usable to render existing model data, you can't read or create the data in this loop. This is not the way SwiftUI works.
For reading table data see TabularData data framework, Swift RegEx or String.split(separator:..)