Xcode - Add Custom Documentation for Swift Closure - swift

Question:
What is the correct format to add documentation (using built in functionality not 3rd party) to a custom Swift closure in Xcode 8 ?
Could you provide the Apple documentation link to refer ?
How to specify the closure parameters ?
How to specify the closure return type ?
Example:
struct S1 {
/// This block is executed after completion
var completion : (String, Int) -> (Bool)
}
Note:
Xcode 8 has built in functionality to add documentation to custom code.
This can be done, by doing one of the following:
Command + Option + Click on code
Keep cursor on code and Editor > Structure > Add documentation

Reference:
For reference see Markup Formatting Reference namely the section "Formatting Quick Help"
Workaround:
If those tags are not supported for the given location the only possible workaround seems to be right now:
struct S1 {
/// description
///
/// __returns__
/// blah blah
///
/// __parameters__
/// blah blah
var completion : (String, Int) -> (Bool)
}

Related

How to return a default Intent configuration for Widget

I want to return a
default INIntent for my widget. In the automatically generated file, there is an optional method of the INIntentHandlerProviding protocol that allows you to specify the default configuration for your widgets.
For me, it looks something like:
defaultIntentParameter(for intent: ConfigurationIntent) -> MyCustomINObject
I manage to make this work, but it only works once. I can't change this dynamically based on date or any other parameters.
Has anyone else had experience with this and managed to get around it?
/*! #abstract Default values for parameters with dynamic options
#discussion Called to query the parameter default value.
*/
#available(iOS 14.0, macOS 10.16, watchOS 7.0, *)
#objc(defaultChartTypeForConfiguration:)
optional func defaultChartType(for intent: ConfigurationIntent) -> Chart?
In your IntentHandler file, add the default function so will display default as well.
Change the Task to be the parameter from intent
TaskIntent is the name of the Type
class IntentHandler: INExtension, DynamicTaskIntentHandling {
func defaultTask(for _: DynamicTaskIntent) -> TaskIntent? {
TaskIntent(identifier: "default", display: "Test")
}

MacOS Quartz Event Tap listening to wrong events

I am trying to intercept mouse move events using the CGEvent.tapCreate(tap:place:options:eventsOfInterest:callback:userInfo:) method as shown below:
let cfMachPort = CGEvent.tapCreate(tap: CGEventTapLocation.cghidEventTap,
place: CGEventTapPlacement.headInsertEventTap,
options: CGEventTapOptions.defaultTap,
eventsOfInterest:CGEventMask(CGEventType.mouseMoved.rawValue),
callback: {(eventTapProxy, eventType, event, mutablePointer) -> Unmanaged<CGEvent>? in event
print(event.type.rawValue) //Breakpoint
return nil
}, userInfo: nil)
let runloopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, cfMachPort!, 0)
let runLoop = RunLoop.current
let cfRunLoop = runLoop.getCFRunLoop()
CFRunLoopAddSource(cfRunLoop, runloopSource, CFRunLoopMode.defaultMode)
I pass as event type eventsOfInterest mouseMoved events with a raw value of 5 as seen in the documentation. But for some reason my print() is not executed unless I click with the mouse. Inspecting the send mouse event in the debugger gives me a raw value of 2, which according to the documentation is a leftMouseUp event.
In the documentation for CGEvent.tapCreate(tap:place:options:eventsOfInterest:callback:userInfo:) it says:
Event taps receive key up and key down events [...]
So it seems like the method ignores mouseMoved events in general?! But how am I supposed to listen to mouseMoved events? I am trying to prevent my cursor (custom cursor) from being replaced (for example when I hover over the application dock at the bottom of the screen).
You need to bitshift the CGEventType value used to create the CGEventMask parameter. In Objective-C, there is a macro to do this: CGEventMaskBit.
From the CGEventMask documentation:
to form the bit mask, use the CGEventMaskBit macro to convert each constant into an event mask and then OR the individual masks together
I don't know the equivalent mechanism in swift; but the macro itself looks like this:
*/ #define CGEventMaskBit(eventType) ((CGEventMask)1 << (eventType))
In your example, it's sufficient to just manually shift the argument; e.g.
eventsOfInterest:CGEventMask(1 << CGEventType.mouseMoved.rawValue),
I would point out that the code example given in the question is a little dangerous; as it creates a default event tap and then drops the events rather than allowing them to be processed. This messes up mouse click handling and it was tricky to actually terminate the application using the mouse. Anyone running the example could set the event tap type to CGEventTapOptions.listenOnly to prevent that.
Here is a way to listen for mouseMove global events (tested with Xcode 11.2+, macOS 10.15)
// ... say in AppDelegate
var globalObserver: Any!
var localObserver: Any!
func applicationDidFinishLaunching(_ aNotification: Notification) {
globalObserver = NSEvent.addGlobalMonitorForEvents(matching: .mouseMoved) { event in
let location = event.locationInWindow
print("in background: {\(location.x), \(location.y)}")
}
localObserver = NSEvent.addLocalMonitorForEvents(matching: .mouseMoved) { event in
let location = event.locationInWindow
print("active: {\(location.x), \(location.y)}")
return event
}
...
There's another thing incorrect in your code, although you might be lucky and it isn't normally causing a problem.
As documented for the mode parameter to CFRunLoopAddSource: "Use the constant kCFRunLoopCommonModes to add source to the set of objects monitored by all the common modes."
That third parameter should instead be CFRunLoopMode.commonModes.
What you have, CFRunLoopMode.defaultMode aka kCFRunLoopDefaultMode, is instead for use when calling CFRunLoopRun.

On property change event handler

In a VSCode extension I'm looking for a way to react when the user modify an extension property defined in contributes.configuration section of package.json.
Does it exists an event like onPropertyChange or some other way to register an event handler?
After re-reading vscode documentation I found myself an answer:
workspace.onDidChangeConfiguration callback receive a ConfigurationChangeEvent when a config property is modified.
with the method affectsConfiguration it is then possible to react at the specific property change, for example:
export function activate(context: vscode.ExtensionContext) {
vscode.workspace.onDidChangeConfiguration(event => {
let affected = event.affectsConfiguration("riot.compiler");
if (affected) {
// rebuild cpp project settings
setup();
}
})
...

Xcode Markup Link Reference Within Same File

I want to make a link reference within the same file in Swift kind of like the way you can in JavaDoc, but I can't seem to find documentation describing how to do this. You can only reference external URLS. I want to be able to do something like this:
class myClass : UIView {
/// The label that displays the scale of this view
/// - seealso: [showScaleView](showScaleView())
private lazy var scaleView: UIView = UIView()
/// Shows the scale view
/// - seealso: [scaleView](scaleView)
private func showScaleView() {
...
}
}
This known issue in Xcode since Jan 2018... :(
Here is radar: https://github.com/lionheart/openradar-mirror/issues/19263

Backbone.Marionette overriding methods

In 0.9.3, the region manager has changed with the following entry in the changelog:
BREAKING Changed the implementation of Region to allow easier
overriding of how the new view is added to the DOM
What is the best way of overriding the open method?
Currently I am doing the following which does work but I am curious to know what the recommended way is:
_.extend(Backbone.Marionette.Region.prototype, {
open: (view) ->
#$el.after(view.el)
})
This change has also broken some code for me because in some cases I was calling show like this:
region.show(documentsView, 'after')
And in others I was calling it like this:
region.show unitsView
How can I override open to take both these instances into account or do I need to override show?
This works:
_.extend(Backbone.Marionette.Region.prototype, {
show: (view, appendMethod) ->
#ensureEl()
#close()
view.render()
#open(view, appendMethod)
#currentView = view
open: (view, appendMethod) ->
appendMethod = appendMethod || "html"
#$el[appendMethod](view.el)
})