cocos2d-X 节点(CCUserDefault.h)API

本文来自http://blog.csdn.net/runaying ,引用必须注明出处!

cocos2d-X 节点(CCUserDefault.h)API

温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记

///cocos2d-x-3.0alpha0/cocos2dx/support/user_default

#ifndef __SUPPORT_CCUSERDEFAULT_H__
#define __SUPPORT_CCUSERDEFAULT_H__

#include "platform/CCPlatformMacros.h"
#include <string>
#include "cocoa/CCData.h"

NS_CC_BEGIN

/**
 * @addtogroup data_storage
 * @{
 */

/**
 * UserDefault 作为一个小型数据库. 你可以使用他 保存/获取 基础的类型值.
 * 例如, setBoolForKey("played", true) 会往数据库里面添加一个 bool 值 true.
 * 他的 key 是 "played". 你可以使用 key 获得它的值例如: getBoolForKey("played").
 * 
 * 支持下列基本类型:
 * bool, int, float, double, string
 */
class CC_DLL UserDefault
{
public:
    /**
     * @js NA
     * @lua NA
     */
    ~UserDefault();

    // get value methods

    /**
     @brief 使用 key 获取值(bool), 如果 key 不存在,将返回一个默认值。
     你可以设置默认值, 或者返回 false.
    * @js NA
    */
    bool    getBoolForKey(const char* pKey);
    /**
     * @js NA
     */
    bool    getBoolForKey(const char* pKey, bool defaultValue);
    /**
     @brief 使用 key 获取值(integer), 如果 key 不存在,将返回一个默认值。
     你可以设置默认值, 或者返回 0.
    * @js NA
    */
    int     getIntegerForKey(const char* pKey);
    /**
     * @js NA
     */
    int     getIntegerForKey(const char* pKey, int defaultValue);
    /**
     @brief 使用 key 获取值(Float), 如果 key 不存在,将返回一个默认值。
     你可以设置默认值, 或者返回 0.0f.
    * @js NA
    */
    float    getFloatForKey(const char* pKey);
    /**
     * @js NA
     */
    float    getFloatForKey(const char* pKey, float defaultValue);
    /**
     @brief 使用 key 获取值(double), 如果 key 不存在,将返回一个默认值。
     你可以设置默认值, 或者返回 0.0.
    * @js NA
    */
    double  getDoubleForKey(const char* pKey);
    /**
     * @js NA
     */
    double  getDoubleForKey(const char* pKey, double defaultValue);
    /**
     @brief 使用 key 获取值(string), 如果 key 不存在,将返回一个默认值。
     你可以设置默认值, 或者返回 "".
    * @js NA
    */
    std::string getStringForKey(const char* pKey);
    /**
     * @js NA
     */
    std::string getStringForKey(const char* pKey, const std::string & defaultValue);
    /**
     @brief 使用 key 获取值(binary data),如果 key 不存在,将返回一个默认值。
     你可以设置默认值, 或者返回 null.
     * @js NA
     * @lua NA
     */
    Data* getDataForKey(const char* pKey);
    /**
     * @js NA
     * @lua NA
     */
    Data* getDataForKey(const char* pKey, Data* defaultValue);

    // set value methods

    /**
     @brief 使用 key 设置值(bool).
     * @js NA
     */
    void    setBoolForKey(const char* pKey, bool value);
    /**
     @brief 使用 key 设置值(Integer).
     * @js NA
     */
    void    setIntegerForKey(const char* pKey, int value);
    /**
     @brief 使用 key 设置值(float).
     * @js NA
     */
    void    setFloatForKey(const char* pKey, float value);
    /**
     @brief 使用 key 设置值(double).
     * @js NA
     */
    void    setDoubleForKey(const char* pKey, double value);
    /**
     @brief 使用 key 设置值(string).
     * @js NA
     */
    void    setStringForKey(const char* pKey, const std::string & value);
    /**
     @brief 使用 key 设置值(binary data)  .
     * @js NA
     * @lua NA
     */
    void    setDataForKey(const char* pKey, const Data& value);
    /**
     @brief 将内容保存到XML文件
     * @js NA
     */
    void    flush();

    /** returns the singleton (单例)
     * @js NA
     * @lua NA
     */
    static UserDefault* getInstance();
    /**
     * @js NA
     */
    static void destroyInstance();

    /** 过时不再需要建议使用新的 API. 可以使用 getInstace() 代替 
     * @js NA
     * @lua NA
     */
    CC_DEPRECATED_ATTRIBUTE static UserDefault* sharedUserDefault();
    /**
     * @js NA
     */
    CC_DEPRECATED_ATTRIBUTE static void purgeSharedUserDefault();
    /**
     * @js NA
     */
    const static std::string& getXMLFilePath();
    /**
     * @js NA
     */
    static bool isXMLFileExist();

private:
    UserDefault();
    static bool createXMLFile();
    static void initXMLFilePath();
    
    static UserDefault* _userDefault;
    static std::string _filePath;
    static bool _isFilePathInitialized;
};

// end of data_storage group
/// @}

NS_CC_END

#endif // __SUPPORT_CCUSERDEFAULT_H__





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值