My macOS app shows a representation of the contents of various folders, so using FSEvents to track modifications from outside the app seemed to be the way to go. I am running into difficulty with writing the code with a callback, all in Swift. I must be doing something wrong, as I get a crash in the callback.

Code in my ViewController class:
        let scanTarget = [<the folders I monitor>]
let scanCallback: FSEventStreamCallback = { (streamRef: ConstFSEventStreamRef, context: UnsafeMutableRawPointer?, count: Int, streamPtr: UnsafeMutableRawPointer, flags: UnsafePointer<FSEventStreamEventFlags>?, eventID: UnsafePointer<FSEventStreamEventId>?) in
            // Convert to appropriate pointer
let theController = context?.bindMemory(to: ViewController.self, capacity: 1)
            // The following line crashes with a bad address exception
            theController?.pointee.<use the pointer to call a method>
        }
        var mutableSelf = self
var theContext = FSEventStreamContext(version: 0, info: &mutableSelf, retain: nil, release: nil, copyDescription: nil)
        let scanInterval: CFTimeInterval = 5.0
eventStream = FSEventStreamCreate(nil, scanCallback, &theContext, scanTarget as CFArray, FSEventStreamEventId(kFSEventStreamEventIdSinceNow), scanInterval, FSEventStreamCreateFlags(kFSEventStreamCreateFlagUseCFTypes))

The pointer I get back (as theController.pointee) is not valid, so I'm obviously doing this incorrectly. I had an idea that the mutableSelf variable should be a class variable, but that didn't really help.

So, how do I get a pointer to self inside the callback?
--
John Brownie
In Finland on furlough from SIL Papua New Guinea
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to