>我正在尝试将数组存储在 GVariant 中,以便将其存储在 GSettings。
我正在使用 gtk+3 和 glib2 用 C 编写应用程序。
通常,每行包含两个元素的数组,即服务器名称和设备名称。
所以我有以下代码:
void tux_gsettings_init() {
tux_settings = g_settings_new("com.tuxin.TuxMusicStudio");
}
void tux_gsettings_save() {
GVariant *variant;
GVariantBuilder *builder;
builder = g_variant_builder_new(G_VARIANT_TYPE("a(ss)"));
g_variant_builder_add(builder, "a(ss)", "test1","test2");
g_variant_builder_add(builder, "a(ss)", "testa1","testa2");
variant = g_variant_new("a(ss)", builder);
g_variant_builder_unref(builder);
g_settings_set_value(tux_settings,"audio_devices_in",variant);
}
所以我正在尝试存储两行。
我收到以下错误:
(tuxmusicstudio:4355): GLib-CRITICAL **: g_variant_builder_end: assertion 'is_valid_builder (builder)' failed
(tuxmusicstudio:4355): GLib-CRITICAL **: g_variant_get_type: assertion 'value != NULL' failed
(tuxmusicstudio:4355): GLib-CRITICAL **: g_variant_type_is_array: assertion 'g_variant_type_check (type)' failed
(tuxmusicstudio:4355): GLib-CRITICAL **: g_variant_get_type_string: assertion 'value != NULL' failed
(tuxmusicstudio:4355): GLib-ERROR **: g_variant_new: expected array GVariantBuilder but the built value has type '(null)'
我在这里错过了什么?
g_variant_builder_add()
格式字符串应该描述要添加的数据,而不是整个数组。所以这样的事情应该有效:
g_variant_builder_add (builder, "(ss)", "test1","test2");