mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
ObjectiveC: Add support for uninitialised variants when converting to an NSDictionary
This commit is contained in:
parent
70651f1c67
commit
4d098faaac
4 changed files with 28 additions and 16 deletions
|
|
@ -96,20 +96,17 @@ inline var jsonDataToVar (NSData* jsonData)
|
|||
return JSON::parse (nsStringToJuce ([jsonString autorelease]));
|
||||
}
|
||||
|
||||
inline NSDictionary* varObjectToNSDictionary (const var& varToParse)
|
||||
// If for any reason the given var cannot be converted into a valid dictionary
|
||||
// an empty dictionary will be returned instead
|
||||
inline NSDictionary* varToNSDictionary (const var& varToParse)
|
||||
{
|
||||
jassert (varToParse.isObject());
|
||||
|
||||
if (! varToParse.isObject())
|
||||
return nullptr;
|
||||
|
||||
NSError* error { nullptr };
|
||||
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData: varToJsonData (varToParse)
|
||||
options: NSJSONReadingMutableContainers
|
||||
error: &error];
|
||||
|
||||
jassert (error == nullptr);
|
||||
jassert (dictionary != nullptr);
|
||||
if (dictionary == nullptr || error != nullptr)
|
||||
return @{};
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
|
@ -127,7 +124,7 @@ inline NSData* jsonObjectToData (const NSObject* jsonObject)
|
|||
return jsonData;
|
||||
}
|
||||
|
||||
inline var nsDictionaryToVar (NSDictionary* dictionary)
|
||||
inline var nsDictionaryToVar (const NSDictionary* dictionary)
|
||||
{
|
||||
return jsonDataToVar (jsonObjectToData (dictionary));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,8 +49,11 @@ public:
|
|||
Array<var> array { 45, 67.8, true, "Hello array!" };
|
||||
data->setProperty ("array", array);
|
||||
|
||||
auto *nsDictionary = varObjectToNSDictionary (data.get());
|
||||
auto clone = nsDictionaryToVar (nsDictionary);
|
||||
const auto* nsDictionary = varToNSDictionary (data.get());
|
||||
expect (nsDictionary != nullptr);
|
||||
|
||||
const auto clone = nsDictionaryToVar (nsDictionary);
|
||||
expect (clone.isObject());
|
||||
|
||||
expect (clone.getProperty ("integer", {}).isInt());
|
||||
expect (clone.getProperty ("double", {}).isDouble());
|
||||
|
|
@ -62,7 +65,19 @@ public:
|
|||
expect (clone.getProperty ("double", {}) == var { 2.3 });
|
||||
expect (clone.getProperty ("boolean", {}) == var { true });
|
||||
expect (clone.getProperty ("string", {}) == var { "Hello world!" });
|
||||
expect (clone.getProperty ("array", {}) == array);
|
||||
expect (clone.getProperty ("array", {}) == var { array });
|
||||
}
|
||||
|
||||
beginTest ("varToNSDictionary converts a void variant to an empty dictionary");
|
||||
{
|
||||
var voidVariant;
|
||||
|
||||
const auto* nsDictionary = varToNSDictionary (voidVariant);
|
||||
expect (nsDictionary != nullptr);
|
||||
|
||||
const auto result = nsDictionaryToVar (nsDictionary);
|
||||
expect (result.isObject());
|
||||
expect (result.getDynamicObject()->getProperties().isEmpty());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ struct PushNotificationsDelegateDetails
|
|||
action.title = juceStringToNS (a.title);
|
||||
action.behavior = a.style == Action::text ? UIUserNotificationActionBehaviorTextInput
|
||||
: UIUserNotificationActionBehaviorDefault;
|
||||
action.parameters = varObjectToNSDictionary (a.parameters);
|
||||
action.parameters = varToNSDictionary (a.parameters);
|
||||
action.activationMode = a.triggerInBackground ? UIUserNotificationActivationModeBackground
|
||||
: UIUserNotificationActivationModeForeground;
|
||||
action.destructive = (bool) a.destructive;
|
||||
|
|
@ -120,7 +120,7 @@ struct PushNotificationsDelegateDetails
|
|||
|
||||
auto triggerTime = Time::getCurrentTime() + RelativeTime (n.triggerIntervalSec);
|
||||
notification.fireDate = [NSDate dateWithTimeIntervalSince1970: (double) triggerTime.toMilliseconds() / 1000.0];
|
||||
notification.userInfo = varObjectToNSDictionary (n.properties);
|
||||
notification.userInfo = varToNSDictionary (n.properties);
|
||||
|
||||
auto soundToPlayString = n.soundToPlay.toString (true);
|
||||
|
||||
|
|
@ -152,7 +152,7 @@ struct PushNotificationsDelegateDetails
|
|||
else if (soundToPlayString.isNotEmpty())
|
||||
content.sound = [UNNotificationSound soundNamed: juceStringToNS (soundToPlayString)];
|
||||
|
||||
auto* propsDict = (NSMutableDictionary*) varObjectToNSDictionary (n.properties);
|
||||
auto* propsDict = (NSMutableDictionary*) varToNSDictionary (n.properties);
|
||||
[propsDict setObject: juceStringToNS (soundToPlayString) forKey: nsStringLiteral ("com.juce.soundName")];
|
||||
content.userInfo = propsDict;
|
||||
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ namespace PushNotificationsDelegateDetailsOsx
|
|||
notification.title = juceStringToNS (n.title);
|
||||
notification.subtitle = juceStringToNS (n.subtitle);
|
||||
notification.informativeText = juceStringToNS (n.body);
|
||||
notification.userInfo = varObjectToNSDictionary (n.properties);
|
||||
notification.userInfo = varToNSDictionary (n.properties);
|
||||
|
||||
auto triggerTime = Time::getCurrentTime() + RelativeTime (n.triggerIntervalSec);
|
||||
notification.deliveryDate = [NSDate dateWithTimeIntervalSince1970: (double) triggerTime.toMilliseconds() / 1000.0];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue