0

I'm trying to create a lookup of CGRect objects but I'm hitting a roadblock.

If I were to initialise a CGRect in viewDidLoad() it would look something like:

let myRect = CGRect(x: (view.frame.width / 2) - 50, y: (view.frame.height / 2) - 50, width: 100, height: 100)

but if that is within a Struct it flags:

'mainViewController.Type' does not have a member named 'view'

It would seem that I'm going about this the wrong way, so could someone shed some light on a better way to implement what I'm after?

Thanks very much.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
SimonTheEngineer
  • 733
  • 1
  • 7
  • 19
  • @ÖzgürErsil No, it can't. You can't access `self` (implicitly or explicitly) in the initialization of a stored property on a class/struct. – Mick MacCallum Jun 19 '15 at 11:16
  • Post the whole method that contains the problem code, and tell us where it's defined (as a member function of the view controller, as an initializer of a struct, etc.) – Duncan C Jun 19 '15 at 11:18
  • 1
    See also http://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other. – Martin R Jun 19 '15 at 11:32
  • @MartinR Thanks, that seems to be the problem! So I just need to create a method where I initialise the struct properties? – SimonTheEngineer Jun 19 '15 at 11:34
  • 1
    @SimonTheEngineer: There are different possible solutions. You can initialize the property in a method (in a view controller `viewDidLoad` is a good place) as in my answer, or define a lazy property as in matt's answer. – Martin R Jun 19 '15 at 11:52

0 Answers0