I'm trying to pass the contents of an array to an NSView to display rectangles x positions. I have set up a version using random numbers and a button and reduced the code to a minimum so hopefully it's more readable . I have tried creating the array as a Global Variable. I have also tried reversing the code from this answer.Pass data between view controllers.I have inserted a comment: "This is what I would like to achieve" which explain what i'm trying to achieve. I'm hoping someone might help ? thanks
Swift 3 Xcode 8.1 OSX Mac OS not iOS
//
// MainWindowController.swift
import Cocoa
class MainWindowController: NSWindowController {
@IBOutlet weak var myView: NSView!
var myArray = [Int]()
override func windowDidLoad() {
super.windowDidLoad()
//just for testing
for i in 0..<6{
myArray.append(i*10)
}
}//EO Overide
//just for testing
@IBAction func myButton(_ sender: AnyObject) {
myFunc()
}
func myFunc(){
for i in 0..<6{
let diceRoll = Int(arc4random_uniform(6) + 1)
myArray[i]=diceRoll * 10
}
myView.needsDisplay = true
}//EO myFunc
}//EnD oF thE wORld
class myGUI: NSView {
override func draw(_ dirtyRect: NSRect) {
for i in..<6{
let fromArray = myArray[i]
let box1 = NSMakeRect(CGFloat(fromArray),0,4,60)//This is what i want to do
let box1Color = NSColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1.0)
let box1Path: NSBezierPath = NSBezierPath(rect: box1)
box1Color.set()
box1Path.fill()
}//EO For
}}