My 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))

I may well have the wrong way of handling the conversion through the context with all the unsafe pointer types, as I'm still rather new to Swift.
--
John Brownie
In Finland on furlough from SIL Papua New Guinea
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to