Getting the Screen Resolution of a Macintosh

If you’re creating a macOS program and need to identify the screen resolution of a Macintosh, you can get the height and width of the screen resolution by using the following code:

        let myScreen = NSScreen.main()
        let resolution = myScreen?.frame
        let height = resolution?.size.height
        let width = resolution?.size.width
        print ("The monitor resolution is height = \(height!) and width = \(width!)")

First you need to create a variable to represent the main screen (NSScreen.main()). Then you need to create a second variable (let resolution = myScreen?.frame) to identify the frame of the screen.

Now you can retrieve the width and height of the screen frame using the size.height and size.width properties. The above code uses the print command to retrieve the screen height and width but you could easily just store these values in a variable for your own program to use.

December 18th, 2016 by
HTML Snippets Powered By :