Add array of strings to string array

I have an array of strings, call it plants = [Plant](), where each of the strings is a plant characteristic. I read in the plant characteristics from a spreadsheet, breaking each record into individual characteristics with tabs for each cell. One characteristic is an image name that I use for a closeup image. I would like to replace the image name with an array of image names that I pass to a UICollectionView in place of the UIImageView I show for the single image. So I would like to modify the spreadsheet to have a cell that could be either image1, as now, or image1, image2, image 3, ... and read the resulting array in for the collection, which could have one or more images. I have no problem with the collectionview from string literals, but reading in the multiple images would seem to involve mixing an array of strings into my string array, and I am not clear whether that is possible. Is there an obvious way to do this? Sorry for the novice question.

Accepted Reply

hi,


it's a little hard to see exactly where you're going, especially when you write


"I have an array of strings, call it plants = [Plant](), where each of the strings is a plant characteristic."


in syntax, does this mean that Plant is simply a string, or that Plant is a struct of several String characteristics, one of which is the image name.


assuming that it's the latter, one might guess you have something like


struct Plant {
    let characteristic1: String
    let characteristic2: String
    ......
    let imageName: String
}


in this is case, you'd simply be replacing the type of imageName to be [String], rather than String.


as for the unseen spreadsheet in your question, one guesses here that you have the various characteristics of plants as its columns, and individual plants as its rows. you probably also have some code that reads the spreadsheet into you program by parsing the tab-delimited rows, one-by-one, and creating Plant structs for each.


if that's the case, consider using the column for the image name (now to be image names) to be as many names as you wish, separated by commas (there are no commas in the names themselves, right?). then parse the field for image name(s) into an array of Strings by separating everything on the commas.


happy to see some code if you have some!


and hope that helps,

DMG

Replies

hi,


it's a little hard to see exactly where you're going, especially when you write


"I have an array of strings, call it plants = [Plant](), where each of the strings is a plant characteristic."


in syntax, does this mean that Plant is simply a string, or that Plant is a struct of several String characteristics, one of which is the image name.


assuming that it's the latter, one might guess you have something like


struct Plant {
    let characteristic1: String
    let characteristic2: String
    ......
    let imageName: String
}


in this is case, you'd simply be replacing the type of imageName to be [String], rather than String.


as for the unseen spreadsheet in your question, one guesses here that you have the various characteristics of plants as its columns, and individual plants as its rows. you probably also have some code that reads the spreadsheet into you program by parsing the tab-delimited rows, one-by-one, and creating Plant structs for each.


if that's the case, consider using the column for the image name (now to be image names) to be as many names as you wish, separated by commas (there are no commas in the names themselves, right?). then parse the field for image name(s) into an array of Strings by separating everything on the commas.


happy to see some code if you have some!


and hope that helps,

DMG

Thank you. Yes. I have a class (following the examples in Apple's App Development with Swift, though I imagine I could use a struct. I read in the spreadsheet, carve it into lines, carve a line at a time into strings at tabs, associate each string with a variable in the class, and then append the line to the Plant array. I will do as you suggest, parsing the image variable into an array of strings as needed by separating the image names at commas. I should have seen this. Thank you for the help.

hi,


first, happy to help 🙂


second, i have a similar project that reads a spreadsheet to create records (structs), although my spreadsheet has titles in the first row. so i read the string titles in the first row, and for each subsequent row, create a Dictionary whose keys are the strings of the first row (the titles) and whose values are the associated strings in the current row. i then pass that dictionary along as an argument to create a struct similar to your Plant type.


an example of creating one dictionary per row:


NAME ADDRESS AGE

Fred 4 N St 29


gets parsed into a dictionary ["NAME" : "Fred", "ADDRESS" : "4 N St", "AGE" : "29"].


that may sound like an extra layer of complexity, but if for any reason you change the spreadsheet columns, it's real easy to update your code -- e.g., changing the order of the columns has no effect whatsoever on your code.


good luck,

DMG