Use of AutoreleasingUnsafeMutablePointer in Swift

Hi,

I'm no Obj-C developer, but I need to use it when interacting with the Zebra barcode scanner API. I have created a brdging header which works, but I can get this function to return any data.

Code Block swift
func sbtGetAvailableScannersList(_ availableScannersList: AutoreleasingUnsafeMutablePointer<NSMutableArray?>!) -> SBT_RESULT

I read on stackoverflow that I can declare it like this:
Code Block swift
var availableScanners: NSMutableArray?
scanner?.sbtGetAvailableScannersList(&availableScanners)
if let _scanners = availableScanners {
for scanner in _scanners {
if let scanner = scanner as? SbtScannerInfo {
listOfScanners.append(scanner)
}
}
}

but the array is always nil. These is a delegate method that fires whenever a scanner get available, and it works, so I know that at least in theory, the array should contain that scanner.

This is the Obj-C implementation in the demo.app provided by Zebra

Code Block Objective-C
- (void)fillScannersList:(NSMutableArray**)list
{
    NSMutableArray *available = [[NSMutableArray alloc] init];
    NSMutableArray *active = [[NSMutableArray alloc] init];
    if (m_DcsSdkApi != nil)
    {
        if ([m_DcsSdkApi sbtGetAvailableScannersList:&available] == SBT_RESULT_FAILURE)
        {
            dispatch_async(dispatch_get_main_queue(),
                ^{
                    [self showMessageBox:@"Search for available scanners has failed"];
                }
            );
        }
        [m_DcsSdkApi sbtGetActiveScannersList:&active];
        /* nrv364: due to auto-reconnect option some available scanners may have
         changed to active and thus the same scanner has appeared in two lists */
        for (SbtScannerInfo *act in active)
        {
            for (SbtScannerInfo *av in available)
            {
                if ([av getScannerID] == [act getScannerID])
                {
                    [available removeObject:av];
                    break;
                }
            }
        }
        if ((list != nil) && (*list != nil))
        {
            [*list removeAllObjects];
            [*list addObjectsFromArray:available];
            [*list addObjectsFromArray:active];
        }
    }
    [available release];
    [active release];
}



Accepted Reply

This is the Obj-C implementation in the demo.app provided by Zebra

Based on the sample code in Objective-C, you need to set an instance of NSMutableArray before you call sbtGetAvailableScannersList(_:).

Please try something like this:
Code Block
var availableScanners: NSMutableArray? = NSMutableArray()
scanner?.sbtGetAvailableScannersList(&availableScanners)


Replies

This is the Obj-C implementation in the demo.app provided by Zebra

Based on the sample code in Objective-C, you need to set an instance of NSMutableArray before you call sbtGetAvailableScannersList(_:).

Please try something like this:
Code Block
var availableScanners: NSMutableArray? = NSMutableArray()
scanner?.sbtGetAvailableScannersList(&availableScanners)


Thanks @OOPer!