0

I have recently used the

Array.forEach{ $0.isHidden = false }

function with NSTextFields for macOS in Xcode 8 using Swift 3. I am trying to do the same thing with NSBoxes. Here is what I am doing:

let Boxes: [NSBox] = [notesBox, manualEditBox, addSubmasterAutoBox, submasterFileDetailsBox]
Boxes.forEach{$0.isTransparent = false}

At the second line, it is giving me an error saying "Cannot use instance member 'notesBox' within property initializer, property initializers run before 'self' is available." I can guarantee that all of these NSBoxes are linked properly and properly referenced using @IBAction.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Hussein Esmail
  • 353
  • 5
  • 21
  • I don't even know what notesBox or manualEditBox is. – El Tomato Sep 18 '17 at 23:48
  • @ElTomato those are the names of my NSBoxes – Hussein Esmail Sep 18 '17 at 23:48
  • Please [search on your error](https://stackoverflow.com/search?q=%5Bswift%5D+Cannot+use+instance+member+within+property+initializer%2C+property+initializers+run+before+%27self%27+is+available) before posting. – rmaddy Sep 18 '17 at 23:54
  • Probably a duplicate of https://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other but need to see more code to know for sure. – rmaddy Sep 18 '17 at 23:54
  • Without more context, it's quite hard to tell whats wrong. From the error it seems that you didn't set up your custom initializer correctly, but without seeing more code, it's impossible to tell for sure. – Dávid Pásztor Sep 18 '17 at 23:55
  • Why are you capitalizing the name of a property? Do you have a subclass of `NSBox` named `Boxes` that your property name could be colliding with? – Wes Sep 19 '17 at 01:44
  • On second thought, that error message sounds like you are trying to initialize that array at the scope of the controller. You have to declare the array and then populate it with other properties (i.e. the boxes) in `viewDidLoad` or whatever method suits you best. – Wes Sep 19 '17 at 01:46
  • @Wes thank-you. I have declared the array as var not let, and empty. I have added the contents in viewDidLoad before my functions. – Hussein Esmail Sep 19 '17 at 01:52
  • @HusseinEsmail no problem, please accept my answer if that was it. – Wes Sep 19 '17 at 16:08

1 Answers1

0

Make sure you don't try to initialize the contents of the array at the scope of the controller. You have to declare the array and then populate it with other properties (i.e. the boxes) in viewDidLoad or whatever method suits you best.

Wes
  • 1,032
  • 7
  • 11