-
-
Notifications
You must be signed in to change notification settings - Fork 59
Unable to properly marshall data in iOS when using NSArray / NSDictionary #1262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It looks like you're converting the Swift example you were provided into TypeScript, but have accidentally misinterpreted it. If you switch the example you linked into Objective-C, it might be clearer what the type definition should be in order to get this marshalling to work: const colorDictionary: { [key: NSNumber]: UIColor } = {
0.0: UIColor.clear,
0.01: UIColor.white,
...
}; |
@Tyler-V also check this thread for creating an NSDictionary |
@NickIliev thanks for the resource. I am using the 'only' working way known to create a NSDictionary as indicated by this comment in that thread you linked above. The other suggestions either throw type errors or crash at runtime. Unfortunately, if we want to represent the following as a NSDictionary...
We have to use the following:
That can't be right? But it works. |
Thanks @facetious, Unfortunately, that creates an object and when passed into that method it crashes as I suspect it is expecting a fully marshalled NSDictionary instantiated prior to execution. Building off my comment above,
|
This is actually another way of writing |
I guess that the most natural syntax that you're looking for is this:
|
And actually specifying an object directly also works as expected according to my tests: const colorDictionary1 = {
0.0: UIColor.blueColor,
0.25: UIColor.brownColor,
0.5: UIColor.redColor,
0.75: UIColor.greenColor,
1: UIColor.orangeColor,
};
const heatmapColor = NSExpression.expressionWithFormatArgumentArray(
"mgl_interpolate:withCurveType:parameters:stops:($heatmapDensity, 'linear', nil, %@)",
[colorDictionary1]); I guess that you've forgotten to wrap the argument inside an array as is required by |
The actual exception I got in this case (forgotten array) was:
|
While it does let me create a heatmapColor with the last example provided @mbektchiev, it does not like the input provided when applying the heatmapColor expression. The other input provided may be what it is looking for since it needs a dictionary but I am getting typing errors trying to instantiate it. |
Similar to the last issue you helped me with, and thank you for that again @mbektchiev, I need to construct this object dynamically since my input is of type
I need to be able to iterate over the stops and construct a dictionary dynamically based on that input of type What is the best way to go about this? |
I would suggest you try something like this: function toHeatmapDictionary(a: object): NSDictionary<NSNumber, UIColor> {
const keys = Object.getOwnPropertyNames(a);
let dict = new NSMutableDictionary<NSNumber, UIColor>({ capacity: keys.length });
for (let key of keys) {
dict.setObjectForKey(<any>a[key], <NSNumber><unknown>(NSString.stringWithString(key).intValue));
}
return dict;
}
const colorDictionary1 = {
0.0: UIColor.blueColor,
0.25: UIColor.brownColor,
0.5: UIColor.redColor,
0.75: UIColor.greenColor,
1: UIColor.orangeColor,
};
const nsDict = toHeatmapDictionary(colorDictionary1);
const heatmapColor = NSExpression.expressionWithFormatArgumentArray(
"mgl_interpolate:withCurveType:parameters:stops:($heatmapDensity, 'linear', nil, %@)",
[nsDict]); The whole gymnastics aims to convert the |
You're an absolute wizard @mbektchiev can't thank you enough, that worked great. |
Environment
Provide version numbers for the following components (information can be retrieved by running
tns info
in your project folder or by inspecting thepackage.json
of the project):implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:8.6.2'
pod 'Mapbox-iOS-SDK', '~> 5.6.1'
Describe the bug
When attempting to call methods in the mapbox sdk, I am unable to marshall the data into the correct format shown in the examples depicted in Mapbox's documentation for iOS.
For creating a NSDictionary of [NSNumber: UIColor], in this example provided:
To create this in my plugin, I have to:
Note, the expected input is [zoomLevel, color ...]
What I have to do to get this to work is to recreate it as:
When I call the same plugin method in Android:
This simply works as expected and in-line with the plugin documentation,
To Reproduce
Try to create an NSDictionary of type [NSNumber: UIColor], the values provided are backwards.
Expected behavior
I should be able to create an NSDictionary where the key/value is properly mapped and doesn't require two arrays in reverse order which contain just keys and just values.
Sample project
I would be more than happy to invite you into my plugin's project which has a branch already setup and a demo-angular app which will let you bootstrap and debug this issue with minimal effort to see for yourself. Please let me know otherwise this should be easily reproducible given the steps listed above.
The text was updated successfully, but these errors were encountered: