Search code examples
iosxcodeswiftsize-classesuitraitcollection

I`ve read that MVC can find out its size class environment via this method in UIViewController


let mySizeClass: UIUserInterfaceSizeClass = self.traitCollection.horizontalSizeClass

and the return value will be an enum .Compact or .Regular (or .Unspecified).

When I try it in code compiler shows me a warning: Value of type 'NSObject -> () -> MyViewController' has no member 'traitCollection'.

Please tell me what is wrong. I`m studying Standford's course on iTunesU and I have this example from there.


Solution

  • That's because you are putting the code in the wrong place, namely at the top level of the view controller declaration. Your code needs to go inside a method.