That's a useful one, thanks! I always wondered how to deal with block-based APIs in CocoaScript properly 👍
Btw, I don't think you need to call
NSApp.endSheet(alert) in the completion handler: it will be dismissed automatically for you.
kevgski I still don’t understand the block signature string at all 🤔
Yeah, Objective-C type encodings are pretty puzzling on their own and even more so when applied to Blocks where some of their types aren't even documented by Apple (e.g. "@?" which seems to be a shortcut for a "block object"). In a nutshell, the type from your snippet could be "decoded" like this:
v — is for "void" return value, the block returns nothing
16 — a size of all arguments combined in bytes; your block has 2 arguments: one implicit (the block object itself) and one explicit (
returnCode), both are 8 byte integers
@?0 — the block object itself; you may think of it as
this (in JS terms) although you don't actually see this argument in code, you just use
0 here means an offset from the beginning of the arguments section or something like that.
q8 — the second argument, an integer at offset
8. This is
The real question here is how do we know which signature to use? There's no easy answer, unfortunately, as we don't have an official API for getting a type information from blocks. There's a private API 👻 though that seems to do the trick.