Creating NSDictionary in Uno.

Hi,

Tried to create a NSDictionary in Uno. I’m unsure how to do this, so I tried this in extern string:

var nsdict_id = extern <ObjC.ID>"@{@\"CBCentralManagerScanOptionAllowDuplicatesKey\" : [NSNumber numberWithBool:YES]}";

After making sure it is a .mm file with the .uxl file.

Generating code and data
SetMe.uno(20,1): E3356: '@"CBCentralManagerScanOptionAllowDuplicatesKey"' could not be resolved
SetMe.uno(20,1): E6117: '@"CBCentralManagerScanOptionAllowDuplicatesKey":[NSNumber numberWithBool' cannot be chained because it does not return anything
(4,203.20 ms)

This work in XCode:

NSDictionary *dict = @{@"CBCentralManagerScanOptionAllowDuplicatesKey" : [NSNumber numberWithBool:YES]};

The problem is that @{whatever} is how we do UXL macros, so you’d need to escape this somehow (I’m not entirely sure how you’d do that).

Alternatively, you could just use the ObjC bindings. Here’s a helper-method we use internally, it could probably be a bit more generic, but this was sufficient for my needs:

    static internal NSDictionary ToNSDictionary(Dictionary<INSCopying, ObjC.ID> dict)
    {
        var ret = new NSMutableDictionary(NSMutableDictionary._dictionaryWithCapacity((uint)dict.Count));
        foreach (var e in dict)
            ret.setObjectForKey(e.Value, e.Key);
        return ret;
    }

Did you look at how we can use "@{}" in extern string? @{} is used in ObjC, and I need a way to be able to do this.

Hi,

You can escape macros by adding a backslash in front (\@{}), similar to how you escape other things.

Also, to write a backslash in a string literal in Uno, escape the backslash with another backslash (\\).

var nsdict_id = extern <ObjC.ID>"\\@{@\"CBCentralManagerScanOptionAllowDuplicatesKey\" : [NSNumber numberWithBool:YES]}";

I am experiencing similar problem as I am unable to build preview for iOS with barcode scanner library added.

MTBBarcodeScanner.mm(417.1): E3356: 'AVVideoCodecKey' could not be resolved
MTBBarcodeScanner.mm(642.1): E3356: 'NSLocalizedDescriptionKey' could not be resolved
MTBBarcodeScanner.mm(654.1): E3356: 'NSLocalizedDescriptionKey' could not be resolved

I get these errors when trying to build.

The lines that I am unable to escape with a backslash in front

self.stillImageOutput.outputSettings = @{AVVideoCodecKey: AVVideoCodecJPEG};

userInfo:@{NSLocalizedDescriptionKey : @"Still image capture is already in progress. Check with isCapturingStillImage"}];

userInfo:@{NSLocalizedDescriptionKey : @"AVCaptureConnection is closed"}];

Any way to escape this? Thanks!

How is the library included? What do you mean by being unable to escape the code? Does it result in an error, are you not allowed to, or what?