Nativescript: NSDictionary usage

Created on 20 Mar 2016  路  6Comments  路  Source: NativeScript/NativeScript

Trying to do something like this:

NSDictionary.alloc().initWithObjectsAndKeys(kAudioFormatMPEG4AAC, AVFormatIDKey, 44100.0, AVSampleRateKey, 2, AVNumberOfChannelsKey, null);    

Results in this error:

JS ERROR Error: Actual arguments count: "7". Expected: "1". 

Curious how to use NSDictionary/NSMutableDictionary in general. Haven't found any direct and clear examples. I tried passing it 1 argument as mentioned but still fails.

Most helpful comment

@NathanWalker @jasssonpet
This also works great:

        let opts: NSDictionary = NSDictionary.dictionaryWithObjectForKey(0.0, kCIInputSaturationKey)
        let cimg: CIImage = CIImage.new().initWithImage(img)
        let gray: CIImage = cimg.imageByApplyingFilterWithInputParameters(
            'CIColorControls',
            opts
        )
        Tesseract.tess.image = UIImage.imageWithCIImage(gray)

All 6 comments

Turns out this works:

new NSDictionary(['.mp3', 16, 44100.0, 2], ['AVFormatIDKey', 'AVEncoderBitRateKey', 'AVSampleRateKey','AVNumberOfChannelsKey']);

Hey @NathanWalker,

You can also use this syntax:

NSDictionary.dictionaryWithDictionary({
    AVFormatIDKey: '.mp3',
    AVEncoderBitRateKey: 16,
    AVSampleRateKey: 44100,
    AVNumberOfChannelsKey: 2,
});

or pass the JSON literal directly to APIs that expect NSDictionary.

@NathanWalker @jasssonpet
This also works great:

        let opts: NSDictionary = NSDictionary.dictionaryWithObjectForKey(0.0, kCIInputSaturationKey)
        let cimg: CIImage = CIImage.new().initWithImage(img)
        let gray: CIImage = cimg.imageByApplyingFilterWithInputParameters(
            'CIColorControls',
            opts
        )
        Tesseract.tess.image = UIImage.imageWithCIImage(gray)

@NathanWalker i know its a pretty old thread, but I've only seen this style that you mentioned work:

new (NSDictionary as any)(['.mp3', 16, 44100.0, 2], ['AVFormatIDKey', 'AVEncoderBitRateKey', 'AVSampleRateKey','AVNumberOfChannelsKey']);

NOTICE: The any keyword since NSDIctionary expects only one argument according to tns-platform-declarations 3.4.1.

The more readable versions, where one can pass directly a JS Object don't work.
NSDictionary.dictionaryWithDictionary(fooobj) fails to compile
NSDictionary.dictionaryWithObjectsAndKeys([foo, bar]) or,
NSDictionary.dictionaryWithObjectsAndKeys([foo, bar, null]) cause an app crash.

Is there a code-readable way to initialize an NSDictionary, or we are stuck with the single array initialization and using the any keyword to bypass TS checks... ?

Can we (also) get the typings fixed so the "as any" cast is not necessary?

For example:
NSDictionary's constructor is currently:

constructor(o: { objects: NSArray; forKeys: NSArray; });

but should probably be:

constructor(objects: ObjectType[], forKeys: KeyType[]);

Similarly for NSArray:

static arrayWithArray(array: NSArray): NSArray;

should probably be:

static arrayWithArray(array: ObjectType[]): NSArray;

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings