Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

CGGetOnlineDisplayList usage in Swift

I realize this might be more of a developer question but I'm not building an app … just trying to get my display resolutions.

I'm not using Xcode … just swift from the command line so I can just have a swift file not a project.



https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/ Quartz_Services_Ref/

gives


funcCGGetOnlineDisplayList(_

maxDisplays
: UInt32,

_

onlineDspys
: UnsafeMutablePointer<CGDirectDisplayID>,

_

dspyCnt
: UnsafeMutablePointer<UInt32>) -> CGError

but not a simple example of usage so I have no idea how to set up the variables and use them afterwards

( which I'm guessing will be looping through the displays and using CGDisplayPixelsHigh and CGDisplayPixelsWide)

Posted on Apr 29, 2015 1:45 PM

Reply
2 replies

Apr 30, 2015 1:52 PM in response to Questor

This seems to work after much tweaking.

Now I need help to compile it to work on older operating systems. Help appreciated.


% swift main.swift

ID is 418876289

dspyCnt is 1

currentDisplay is 418876289

CGDisplayPixelsHigh(currentDisplay) is 1080

CGDisplayPixelsWide(currentDisplay) is 1920


% swiftc main.swift -o main

<unknown>:0: error: cannot load underlying module for 'CoreGraphics'

<unknown>:0: note: did you forget to set an SDK using -sdk or SDKROOT?

<unknown>:0: note: use "xcrun -sdk macosx swift" to select the default OS X SDK installed with Xcode

----------------------------- main.swift -----------------------------


import Foundation

import CoreGraphics


var mainID = CGMainDisplayID()


println("ID is \(mainID)")


var dErr: CGError

let maxDisplays: UInt32 = 16

var onlineDspys: [CGDirectDisplayID] = [mainID]

var dspyCnt: UInt32 = 0


dErr = CGGetOnlineDisplayList(maxDisplays, &onlineDspys, &dspyCnt)


println("dspyCnt is \(dspyCnt)")


for currentDisplay in onlineDspys {

println("currentDisplay is \(currentDisplay)")

println("CGDisplayPixelsHigh(currentDisplay) is \(CGDisplayPixelsHigh(currentDisplay))")

println("CGDisplayPixelsWide(currentDisplay) is \(CGDisplayPixelsWide(currentDisplay))")

}

CGGetOnlineDisplayList usage in Swift

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.