HarmonyOS ValueType怎么转成想要的数据来使用?

ValueType怎么转成下面的数据来使用number | string | boolean | Array<number> | Array<string> | Array<boolean> | Uint8Array | object | bigint。

HarmonyOS
14h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa
import data_preferences from '@ohos.data.preferences' 
 
const PREFERENCES_NAME = "my_pre" 
 
export default class PreUtil { 
  private static instance: PreUtil; 
  private preferences; 
 
  private constructor(context) { 
    this.getPre(context) 
  } 
 
  // 获取单例对象 
  public static getInstance(context) { 
    if (this.instance == null) { 
      this.instance = new PreUtil(context) 
    } 
    return this.instance 
  } 
 
  // 获取preferences对象 
  getPre(context) { 
    let promise = data_preferences.getPreferences(context, PREFERENCES_NAME) 
    promise.then((preferences) => { 
      console.info("PreUtil Get preferences successfully.") 
      this.preferences = preferences 
    }).catch((err) => { 
      console.info("PreUtil Get preferences failed. err:" + err) 
    }) 
  } 
 
  // 保存数据 
  putVal(key: string, val) { 
    let promise = this.preferences.put(key, val) 
    promise.then(() => { 
      console.info("PreUtil Put value of " + key + " successfully.") 
      let promise = this.preferences.flush() 
      promise.then(() => { 
        console.info("PreUtil Flushed to file successfully.") 
      }).catch((err) => { 
        console.info("PreUtil Flush to file failed, err: " + err) 
      }) 
    }).catch((err) => { 
      console.info("PreUtil Put value of startup failed, err: " + err) 
    }) 
  } 
 
  // 获取数据 
  getVal(key: string, defaultVal) { 
    let promise = this.preferences.get(key, defaultVal) 
    promise.then((value) => { 
      console.info("PreUtil Get value of " + key + " is " + value) 
    }).catch((err) => { 
      console.info("PreUtil Get value of " + key + " failed, err: " + err) 
    }) 
  } 
 
  // 删除数据 
  deleteVal(key: string) { 
    let promiseDel = this.preferences.delete(key) 
    promiseDel.then(() => { 
      console.info("PreUtil Deleted key " + key + " successfully.") 
    }).catch((err) => { 
      console.info("PreUtil Deleted key " + key + " failed with err:" + err) 
    }) 
  } 
  protected getBoolean(key: string, defValue: boolean): boolean { 
    return this.preferences.getSync(key, defValue) 
  } 
  protected getString(key: string, defValue: string): string { 
    return this.preferences.getSync(key, defValue).toString(); 
  } 
  protected getBoolean1(key: string, defValue: boolean): boolean { 
    return this.preferences.getSync(key, defValue) 
  } 
} 
  
请参考如下代码: 
import dataPreferences from '@ohos.data.preferences'; 
import { Context } from '@kit.AbilityKit'; 
export class PreferencesBase { 
  private TAG_U = 'PreferencesBase'; 
  private pref: dataPreferences.Preferences; 
 
  constructor(p_name: string,context: Context) { 
    let options: dataPreferences.Options = { name: p_name} 
    this.pref = dataPreferences.getPreferencesSync(context, options); 
  } 
 
  protected putString(key: string, value: string) { 
    try { 
      this.pref.putSync(key, value); 
    } catch (err) { 
      // JLog.error(this.TAG_U, 'put the putString failed, err: key=' + key, err); 
    } 
    try { 
      this.pref.flush(); 
    } catch (err) { 
      // JLog.error(this.TAG_U, 'put the flush putString failed, err: key=' + key, err); 
    } 
  } 
 
  protected getString(key: string, defValue: string): string { 
    return this.pref.getSync(key, defValue).toString(); 
  } 
 
  protected putNumber(key: string, value: number) { 
    try { 
      this.pref.putSync(key, value); 
    } catch (err) { 
      // JLog.error(this.TAG_U, 'put the putNumber failed, err: key=' + key, err); 
    } 
    try { 
      this.pref.flush(); 
    } catch (err) { 
      // JLog.error(this.TAG_U, 'put the flush putNumber failed, err: key=' + key, err); 
    } 
  } 
 
  protected getNumber(key: string, defValue: number):number { 
    return this.pref.getSync(key, defValue) as number 
  } 
 
 
  protected putBoolean(key: string, value: boolean) { 
    try { 
      this.pref.putSync(key, value); 
    } catch (err) { 
      // JLog.error(this.TAG_U, 'put the putBoolean failed, err: key=' + key, err); 
    } 
    try { 
      this.pref.flush(); 
    } catch (err) { 
      // JLog.error(this.TAG_U, 'put the flush putBoolean failed, err: key=' + key, err); 
    } 
  } 
 
  protected getBoolean(key: string, defValue: boolean):boolean { 
    return this.pref.getSync(key, defValue) as boolean 
  } 
}
分享
微博
QQ
微信
回复
10h前
相关问题
HarmonyOS json数据如何转成map
795浏览 • 1回复 待解决
想要Harmonyos实战
9481浏览 • 3回复 待解决
HarmonyOS 想要实现图中这样UI
139浏览 • 1回复 待解决
想把APK文件转成HAP怎么弄?
3890浏览 • 2回复 待解决
HarmonyOS HasMap 如何转成对象
124浏览 • 1回复 待解决
HarmonyOS 如何把对象转成HasMap
504浏览 • 2回复 待解决
Array快速转成ArrayList
952浏览 • 1回复 待解决
HarmonyOS 汉字转成特定格式
221浏览 • 1回复 待解决
HarmonyOS 数据数据更新怎么监听?
20浏览 • 0回复 待解决