Swiftify drag & drop APIs

Originator:xavier.jurado
Number:rdar://32598504 Date Originated:06-jun-2017 11:41 p. m.
Status:Open Resolved:
Product:iOS + SDK Product Version:11.0
Classification:Enhancement Reproducible:Not Applicable
 
Summary:
The new set of APIs related to drag and drop forces the use of force unwraps and force casts in Swift.

Steps to Reproduce:
// itemProvider is of type NSItemProvider
itemProvider.loadObject(ofClass: UIImage.self) { object, error
…
}

Expected Results:
object is of type UIImage

Actual Results:
Object needs to be casted to UIImage. Eg:
let image = object as! UIImage

Version:
11.0

Notes:
Proposed implementation:

extension NSItemProvider {

    func genericLoadObject<T>(ofClass aClass: T.Type, completionHandler: @escaping (T?, Error?) -> Swift.Void)
    -> Progress where T: NSItemProviderReading {
        return loadObject(ofClass: aClass) { (item, error) in
            if let item = item as? T {
                completionHandler(item, error)
            } else {
                completionHandler(nil, error)
            }
        }
    }
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!