Passing an array into struct

Firstly I am an absolute newbie! In Playgrounds on an iPad I have managed to convert a .csv data string to an Array that I use to print (in Console) several Cards. Now that I have transferred this code to the “App” part I find it doesn’t work which I do understand why. My question is how do I pass the index of this Array to the struct part in the App? I am thinking of starting simple by passing it into a “List”. Hope this all makes enough sense to point me in the right direction. Thanks in advance 🙏🏻

Answered by Claude31 in 732802022

Where to put my .csv file (some time down the track I envisage building it within the App and/ or being able to download it from Dropbox or similar

You have several options, depending on where you generate this file

  • copy in the app's documents: afetr reading the fileyou save in the documents folder of the app
  • retrieve directly from a server

Where to put put the code to convert the .csv file to an array

When do you need to use it ? Here also, several options:

  • in appDidFinishLaunching, to be sure it will be available
  • in the viewDidLoad of the view controller where you need to use
  • in a utility function that you can call from anywhere

The code needed to do this: Print on screen rather than the Console selected items from the array

To display on screen, you can create a multilines UILabel or a UITextView and set its content (label.text) with the content of the array. Code would be like:

label.text = ""
for line in myArray {
  label.text = label.text + line + "\n"  // We go to new line at the end
}

You should probably close the thread now on the correct answer and start a new one. It is not good to have a thread that reopens new questions. The good practice is : one thread, one question, a correct answer, close thread. Good continuation.

Subsequent to my initial question: Maybe I should have asked how do I convert the .cav file into an Array and list it all in the Struct? Not sure if I can have the .csv and Array in a non App file with no code?

You're a newbie, so welcome.

It is difficult to answer (and even understand) without seeing code.

Please show what you have written in playground and what you tested in app.

Maybe a hint to your question. When you want to get the index as well as item of array, you can use enumerated():

for (i, item) in myArray.enumerated() {
  // i is the index of item in the myArray
}

Thank you for your time 🙏🏻 This is what I have done in Playgrounds...

var printCards = "" // "No" required if "NOT" printing the cards // Imported .csv file pasted in (var gameImport) below //////////////// var gameImport =    "24,exam.dwindled.span,Short for examination,Reduced gradually in size,The total length of something from end to end,_ _ _ _ . _ w _ _ d _ _ d . _ _ _ n,16,///cracked.shadowed.private,The Vase was (?) when it toppled over,A part of the house is (?) by a tree every day,We like to talk in (?) when we want to keep it           : secret,_ _ _ _ _ _ . _ _ _ _ _ _ . _ _ _ a t e,98,///spilling.dances.backbone,Causing or allowing liquid to flow over the edge          : of its container,A series of steps that match the speed and             : rhythm of a piece of music,The column of bones in the back which sustains           : and gives firmness to the frame,_ p _ l l _ n . a _ c _ s._ a _ _ b _ n ,93,///inwards.habitat.buttered,Toward the centre or interior,Place where anything is commonly found,Covered or spread with butter, _ _ w _ r _ . a b t _ t._ u _ _ e _ e d, a e m x , d i l e n , p a s , d r a c c k e , d s a d o w e h , v r p i , g i s i , n d e , k o b c e , n s i a d , i h a , t b r t" /////////////////////////////////////////////////////////////// var redTeam = "               : RED TEAM :" var blueTeam = "               : BLUE TEAM :" var cardDivider = "......................................................................" var start = "Card Position   : Start" var positionOne = "Card Position   : Position One" var positionTwo = "Card Position   : Position Two" var positionThree = "Card Position   : Position Three"

func printCardsPlease() {    if printCards == "No" {     start = ""     positionOne = ""     positionTwo = ""     positionThree = ""     redTeam = ""     blueTeam = ""   } } let lineSpace = "" // print(lineSpace) to insert a blank line // Red Team Variables var redFirstCardNumber = ""// Red 1st Clue Card var redFirstCardWhat3Words = "" var redFirstCardFirstWordClue = "" var redFirstCardSecondWordClue = "" var redFirstCardThirdWordClue = "" var redFirstCardClueForm = "" var redSecondCardNumber = ""// Red 2nd Clue Card var redSecondCardWhat3Words = "" var redSecondCardFirstWordClue = "" var redSecondCardSecondWordClue = "" var redSecondCardThirdWordClue = "" var redSecondCardClueForm = "" var redThirdCardNumber = ""// Red 3rd Clue Card var redThirdCardWhat3Words = "" var redThirdCardFirstWordClue = "" var redThirdCardSecondWordClue = "" var redThirdCardThirdWordClue = "" var redThirdCardClueForm = "" var redFourthCardNumber = ""// Red 4th Clue Card var redFourthCardWhat3Words = "" var redFourthCardFirstWordClue = "" var redFourthCardSecondWorcClue = "" var redFourthCardThirdWordClue = "" var redFourthCardClueForm = "" var redFirstCardFirstWordLetters = "" // Red 1st Card Letters var redFirstCardSecondWordLetters = "" var redFirstCardThirdWordLetters = "" var redSecondCardFirstWordLetters = "" // Red 2nd Card Letters var redSecondCardSecondWordLetters = "" var redSecondCardThirdWordLetters = "" var redThirdCardFirstWordLetters = "" // Red 3rd Card Letters var redThirdCardSecondWordLetters = "" var redThirdCardThirdWordLetters = "" var redFourthCardFirstWordLetters = "" // Red 4th Card Letters var redFourthCardSecondWordLetters = "" var redFourthCardThirdWordLetters = ""

// etc

// Convert (gameImport) to an Array (gameImportArray) let gameImportArray = gameImport.components(separatedBy: ",") printCardsPlease()

// RED TEAM

// First Card print ("               : RED TEAM :") print (start) redFirstCardNumber = gameImportArray [0] print ("Card Number    : (redFirstCardNumber)") redFirstCardWhat3Words = gameImportArray [1] // print("What3Word     : (redFirstCardWhat3Words)") redFirstCardFirstWordClue = gameImportArray [2] print("First Word Clue  : (redFirstCardFirstWordClue)") redFirstCardSecondWordClue = gameImportArray [3] print("Second Word Clue  : (redFirstCardSecondWordClue)") redFirstCardThirdWordClue = gameImportArray [4] print("Third Word Clue  : (redFirstCardThirdWordClue)") print (lineSpace) redFirstCardClueForm = gameImportArray [5] print("Answer       : (redFirstCardClueForm)") print (lineSpace) redFirstCardFirstWordLetters = gameImportArray [24] print("First Word Letters : (redFirstCardFirstWordLetters)") redFirstCardSecondWordLetters = gameImportArray [25] print("Second Word Letters : (redFirstCardSecondWordLetters)") redFirstCardThirdWordLetters = gameImportArray [26] print("Third Word Letters : (redFirstCardThirdWordLetters)") print (cardDivider)

// etc

Sorry this might be better than the previous post...

var gameImport =   "24,exam.dwindled.span,Short for examination,Reduced gradually in size,The total length of something from end to end,_ _ _ _ . _ w _ _ d _ _ d . _ _ _ n,16,///cracked.shadowed.private,The Vase was (?) when it toppled over,A part of the house is (?) by a tree every day,We like to talk in (?) when we want to keep it           : secret,_ _ _ _ _ _ . _ _ _ _ _ _ . _ _ _ a t e,98,///spilling.dances.backbone,Causing or allowing liquid to flow over the edge          : of its container,A series of steps that match the speed and             : rhythm of a piece of music,The column of bones in the back which sustains           : and gives firmness to the frame,_ p _ l l _ n . a _ c _ s._ a _ _ b _ n ,93,///inwards.habitat.buttered,Toward the centre or interior,Place where anything is commonly found,Covered or spread with butter, _ _ w _ r _ . a b t _ t._ u _ _ e _ e d, a e m x , d i l e n , p a s , d r a c c k e , d s a d o w e h , v r p i , g i s i , n d e , k o b c e , n s i a d , i h a , t b r t" 

// Convert (gameImport) to an Array let gameImportArray = gameImport.components(separatedBy: ",")

redFirstCardNumber = gameImportArray [0] print ("Card Number  (redFirstCardNumber)") redFirstCardWhat3Words = gameImportArray [1] print("What3Word   (redFirstCardWhat3Words)") redFirstCardFirstWordClue = gameImportArray [2] print("First Word Clue  : (redFirstCardFirstWordClue)") redFirstCardSecondWordClue = gameImportArray [3] print("Second Word Clue  : (redFirstCardSecondWordClue)") redFirstCardThirdWordClue = gameImportArray [4] print("Third Word Clue  : (redFirstCardThirdWordClue)") print (lineSpace) redFirstCardClueForm = gameImportArray [5] print("Answer       : (redFirstCardClueForm)") print (lineSpace) redFirstCardFirstWordLetters = gameImportArray [24] print("First Word Letters : (redFirstCardFirstWordLetters)") redFirstCardSecondWordLetters = gameImportArray [25] print("Second Word Letters : (redFirstCardSecondWordLetters)") redFirstCardThirdWordLetters = gameImportArray [26] print("Third Word Letters : (redFirstCardThirdWordLetters)") // etc.

When you post code, use code formatte rtool (</>) to make it readable. It is just impossible to understand (and help) as it is.

To get something like this:

let lineSpace = "" // print(lineSpace) to insert a blank line

// Red Team Variables
var redFirstCardNumber = ""// Red 1st Clue Card
var redFirstCardWhat3Words = ""
var redFirstCardFirstWordClue = ""
var redFirstCardSecondWordClue = ""
var redFirstCardThirdWordClue = ""
var redFirstCardClueForm = ""

var redSecondCardNumber = ""// Red 2nd Clue Card
var redSecondCardWhat3Words = ""
var redSecondCardFirstWordClue = ""
var redSecondCardSecondWordClue = ""
var redSecondCardThirdWordClue = ""
var redSecondCardClueForm = ""

var redThirdCardNumber = ""// Red 3rd Clue Card
var redThirdCardWhat3Words = ""
var redThirdCardFirstWordClue = ""
var redThirdCardSecondWordClue = ""
var redThirdCardThirdWordClue = ""
var redThirdCardClueForm = ""

var redFourthCardNumber = ""// Red 4th Clue Card
var redFourthCardWhat3Words = ""
var redFourthCardFirstWordClue = ""
var redFourthCardSecondWorcClue = ""
var redFourthCardThirdWordClue = ""
var redFourthCardClueForm = ""

var redFirstCardFirstWordLetters = "" // Red 1st Card Letters
var redFirstCardSecondWordLetters = ""
var redFirstCardThirdWordLetters = ""

var redSecondCardFirstWordLetters = "" // Red 2nd Card Letters
var redSecondCardSecondWordLetters = ""
var redSecondCardThirdWordLetters = ""

var redThirdCardFirstWordLetters = "" // Red 3rd Card Letters
var redThirdCardSecondWordLetters = ""
var redThirdCardThirdWordLetters = ""

var redFourthCardFirstWordLetters = "" // Red 4th Card Letters
var redFourthCardSecondWordLetters = ""
var redFourthCardThirdWordLetters = ""


let gameImportArray = gameImport.components(separatedBy: ",")

redFirstCardNumber = gameImportArray [0]
print ("Card Number  \(redFirstCardNumber)")

redFirstCardWhat3Words = gameImportArray [1]
print("What3Word   \(redFirstCardWhat3Words)")

redFirstCardFirstWordClue = gameImportArray [2]
print("First Word Clue  : \(redFirstCardFirstWordClue)")

redFirstCardSecondWordClue = gameImportArray [3]
print("Second Word Clue  : \(redFirstCardSecondWordClue)")

redFirstCardThirdWordClue = gameImportArray [4]
print("Third Word Clue  : \(redFirstCardThirdWordClue)")
print (lineSpace)

redFirstCardClueForm = gameImportArray [5]
print("Answer       : \(redFirstCardClueForm)")
print (lineSpace)

redFirstCardFirstWordLetters = gameImportArray [24]
print("First Word Letters : \(redFirstCardFirstWordLetters)")

redFirstCardSecondWordLetters = gameImportArray [25]
print("Second Word Letters : \(redFirstCardSecondWordLetters)")

redFirstCardThirdWordLetters = gameImportArray [26]
print("Third Word Letters : \(redFirstCardThirdWordLetters)") // etc.

There was at least an error in the print statements:

print ("Card Number  (redFirstCardNumber)")

if you want to print the var by interpolating between the quotes, you need the \

print ("Card Number  \(redFirstCardNumber)")

I am so sorry about this! I did come to realise there was a problem when I copied and pasted into Playgrounds to test what I had posted and saw the absolute mess!! I am not aware of the use of the code format rtool. I will have to find this tool and learn how to use it for the next time. As for the error that might have been a copy and paste error on my part, sorry again.

I am so grateful for you taking the time to help with this and hope I don’t use up all your patience in the process. 🙏🏻

To paste code:

  • use Paste and Match Style in the Edit menu
  • Then select the whole code you pasted and click on the tool at the bottom of the editing window:

Is your problem solved ? If so don't forget to close the thread by marking the correct answer. Do do it, tap on the round with checkmark on the left of correct answer:

If not, explain what is the remaining problem.

I would love to know….

With building an App rather than what I have managed to do in Playgrounds in mind…

  1. Where to put my .csv file (some time down the track I envisage building it within the App and/ or being able to download it from Dropbox or similar
  2. Where to put put the code to convert the .csv file to an array
  3. The code needed to do this
  4. Print on screen rather than the Console selected items from the array

I acknowledge this is asking a lot but these pointers will I hope be enough to get me out of the mud I find myself in!

Thanks in advance

Accepted Answer

Where to put my .csv file (some time down the track I envisage building it within the App and/ or being able to download it from Dropbox or similar

You have several options, depending on where you generate this file

  • copy in the app's documents: afetr reading the fileyou save in the documents folder of the app
  • retrieve directly from a server

Where to put put the code to convert the .csv file to an array

When do you need to use it ? Here also, several options:

  • in appDidFinishLaunching, to be sure it will be available
  • in the viewDidLoad of the view controller where you need to use
  • in a utility function that you can call from anywhere

The code needed to do this: Print on screen rather than the Console selected items from the array

To display on screen, you can create a multilines UILabel or a UITextView and set its content (label.text) with the content of the array. Code would be like:

label.text = ""
for line in myArray {
  label.text = label.text + line + "\n"  // We go to new line at the end
}

You should probably close the thread now on the correct answer and start a new one. It is not good to have a thread that reopens new questions. The good practice is : one thread, one question, a correct answer, close thread. Good continuation.

Passing an array into struct
 
 
Q