Wednesday, November 9, 2016 [Tweets] [Favorites]

NSItemProvider’s NSSecureCoding Reflection

Apple on NSItemProvider.loadItem(forTypeIdentifier:options:completionHandler:):

Call this method when you want to retrieve the item provider’s data. If the item provider object is able to provide data in the requested type, it does so and asynchronously executes your completionHandler block with the results. The block may be executed on a background thread.

The type information for the first parameter of your completionHandler block should be set to the class of the expected type. For example, when requesting text data, you might set the type of the first parameter to NSString or NSAttributedString. An item provider can perform simple type conversions of the data to the class you specify, such as from NSURL to NSData or FileWrapper, or from NSData to UIImage (in iOS) or NSImage (in macOS).

Douglas Hill (via Twitter):

This API makes use of reflection internally: it looks at the type of the block’s first parameter to decide what to return. This is dependent on Objective-C’s looser enforcement of the block signature compared to Swift — and therefore does not work in Swift. (Yes, still.) See this discussion on the developer forums.

springsup:

It’s strange to me that Apple would write such a recent API in such a bizarre way - as far as I know, there is no documented way to reflect block signatures so its fragile anyway. Why not just take a class parameter and give the block an id parameter type?

It’s a strange API.

Comments

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment