我试图将数组存储在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)'
我在这里错过了什么?
答案 0 :(得分:2)
g_variant_builder_add()
格式字符串应描述您要添加的数据,而不是整个数组。所以这样的事情应该有效:
g_variant_builder_add (builder, "(ss)", "test1","test2");