0

I'm New to Coding and this is my first post on S/O I'm working on a little project Single View App with multiple pages.

I am trying to create a secondary array from an existing string array with references. I don't want to replicate all the data as the mainArray would handle any future changes.

I have over 50 other arrays to setup each with same and different Items all taken from the mainArray. The mainArray contains around 170 items in my code.

I have replicated the concept of what I need in this example, I cannot find the answer anywhere online, maybe I'm not searching using the correct terminology?

I want to set the coding to name a command within a func setSecondaryArray() 'nameArray' and this will determine which array the UICollectionView reads and displays in the page using a UIButton elsewhere in code. I can't get the basic secondaryArray to recognise the mainArray items. Any help would be appreciated.

Thank you.

let mainArray = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7", "Item 8", 
"Item 9" ]

let secondArray = [ mainArray[0], mainArray[4], mainArray[6] ]

let thirdArray = [ mainArray[1], mainArray[3], mainArray[7], mainArray[8] ]


func setSecondArray() {
    nameArray = secondArray
}


 //nameArray.count code going here to form a layout of the UIButtons
David_2877
  • 257
  • 4
  • 7
  • you want to pass array by reference from what I got. correct? – subodh1989 Dec 07 '19 at 17:54
  • you can use inout array and pass array by reference – subodh1989 Dec 07 '19 at 17:55
  • Yes, just use the reference to create the second array so that if I need to change one item in the future or change languages for the app, I only need to change it in the original array. Please could you give an example for inout array, I've not come across this one before. I'm 2 months in with my coding. – David_2877 Dec 07 '19 at 18:37
  • It is not clear to me what the issue is with your code. What exactly goes wrong? – Joakim Danielson Dec 07 '19 at 19:16
  • 1
    You are using the word "reference" which has a technical meaning in programming but it is not clear you are using it that way and this is leading people to what might be the wrong help. If you didn't intend, or don't know, the technical meaning you might want to edit the question to describe your problem using a different English word/phrase – something involving "copy" maybe? If you are using it with its technical meaning you probably need to elaborate on *when* you might "need to change one item ..., I only need to change it in the original array", i.e. during building or running? HTH – CRD Dec 07 '19 at 19:44
  • Sorry, I'm new to many aspects of coding. I've renamed the title of the question, and other parts in the question. So, i want the secondArray to have copied the items "Item 0", "Item 5" and "Item 7". Then, in thirdArray I want "item 2", "Item 4", "Items 8" and "Item 9". The Error I am getting is: 'Cannot use instance member 'mainArray' within property initializer; property initializers run before 'self' is available'. I hope that makes some sense. – David_2877 Dec 07 '19 at 21:07
  • It's an error that appears prior to building the app, where the Red label appears as you are typing. The error is: 'Cannot use instance member 'allMtsDescriminatorsArray' within property initializer; property initializers run before 'self' is available'. So my main goal is for items in the first array to appear in the secondArray without re-typing them into secondArray. – David_2877 Dec 07 '19 at 21:14
  • I solved my problem. So I ended up using a second variable from the first variable and use this second variable to call the array from a second UIView Controller. For some reason this worked and I can now make the second array made up from selected items of the main array using for example: var objMainVC3 = CardSets() in the swift file with the new data and the second View Controller. let otherVC3 = CardSets() secondArray2 = otherVC3.secondArray. I'm not sure how else to explain this being new to this. But I got it working. – David_2877 Dec 10 '19 at 06:08

0 Answers0