在Nativescript应用程序设置模块中,有没有办法迭代所有键值?
答案 0 :(得分:0)
没有合适的方法从application-settings
获取所有密钥并迭代它们。您仍然需要将键保存在某个位置 - 例如在数组中,以便能够获取值。为方便起见,我附上了示例,但是给定的代码并不是最佳实践。
例如:
import { EventData } from 'data/observable';
import { Page } from 'ui/page';
import { HelloWorldModel } from './main-view-model';
import {setString, hasKey, getString, setNumber, getNumber} from "application-settings"
// Event handler for Page "navigatingTo" event attached in main-page.xml
export function navigatingTo(args: EventData) {
// Get the event sender
let page = <Page>args.object;
var arrayKeys=[{value:"string1", type:"string"}, {value:"string2", type:"string"}, {value:"string3", type:"string"}, {value:"number1", type:"number"}];
setString("string1", "stringValue1");
setString("string2", "stringValue2");
setString("string3", "stringValue3");
setNumber("number1", 1);
console.log("--------Get Values----------")
for(var i=0; i<arrayKeys.length;i++){
if(hasKey(arrayKeys[i].value)){
switch (arrayKeys[i].type) {
case "number":
console.log("Number "+getNumber(arrayKeys[i].value))
break;
case "string":
console.log("String "+getString(arrayKeys[i].value))
break;
default:
break;
}
}
}
}