1

I was implementing a MacOS Services plugin and stumbled onto a Swift/ObjC interaction I am not able to solve. In ObjC, the signature for calling Service method looks like this:

- (void) translateService:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error

I am able to get a Swift equivalent built and working, but I don't know what to do about (NSString **) error. I have tried AutoreleasingUnsafeMutablePointer<NSString?>?, but I get a complaint from the compiler: "Cannot assign to value: 'error' is a 'let' constant" when I tried to assign an NSString to the error parameter. Here is what I have (which I am sure is not right)

   @objc func translateService( _  pboard:  NSPasteboard, userData: String,  error: AutoreleasingUnsafeMutablePointer <NSString?>?)  {

    if let types = pboard.types {

        if !types.contains(NSPasteboard.PasteboardType.string) {
            //TODO: Error
            error = NSString("Does not contain String data")
            return
        }

How do you mimic this functionality in Swift? How do you return an error string in that parameter?

D. Spears
  • 31
  • 4
  • Maybe this will help: https://stackoverflow.com/questions/34786115/swift-doesnt-convert-objective-c-nserror-to-throws – koen Apr 13 '20 at 18:46
  • That was a path I was originally wandering down, but it isn't a NSError**/NSErrorPointer in the signature, it is an NSString**, which I don't *think* has automatic bridging to NSError** and thus throws functionality. I *think* I am just supposed to stuff a string in there, but I don't really know the API well enough to be sure – D. Spears Apr 13 '20 at 19:39

1 Answers1

2

I think I might have found the answer. Please correct me if I am wrong.

@objc func translateService( _  pboard:  NSPasteboard, userData: String,  error: AutoreleasingUnsafeMutablePointer<NSString?>?)  {

    if let types = pboard.types {            
        if !types.contains(NSPasteboard.PasteboardType.string) {

            error?.pointee = NSString("Does not contain String data")
D. Spears
  • 31
  • 4