CCString 分类: Cocos2d-x 2015-02-09 18:22 122人阅读 评论(0) 收藏

CCString

简介

CCString继承至CCObject,CCObjecte这个基类主要是为了自动内存管理而建立的。CCString提供一系列的接口,例如create,convert等等。缓存

经常使用的方法

建立:

    /**使用std::string建立了一个字符串, 你也能够传递一个c字符串指针,由于std::string的构造函数能够访问c字符串指针

     * @返回的 CCString 指针是一个自动释放对象,

     *也就意味着你不须要调用release操做,除非你retain了.

     */

    static CCString* create(const std::string& str);

    /**使用格式化方式来建立一个字符串,这个方法和c语言里面的‘sprintf’相似,默认缓存大小是(1024*100)bytes

     *假如你想要改变这个缓存大小,你能够去CCString.cpp中,更改kMaxStringLen 这个宏定义。

     * @返回的 CCString 指针是一个自动释放对象,

     *也就意味着你不须要调用release操做,除非你retain了.

     */ 

    static CCString* createWithFormat(const char* format, …);

    /** 使用二进制数据来建立字符串 

     * @返回的 CCString 指针是一个自动释放对象,

     *也就意味着你不须要调用release操做,除非你retain了.

     */

    static CCString* createWithData(const unsigned char* pData, unsigned long nLen);

    /**使用一个文件来建立一个字符串, 

     * @return A CCString pointer which is an autorelease object pointer,

     * it means that you needn't do a release operation unless you retain it.

     */
    static CCString* createWithContentsOfFile(const char* pszFileName);

转换

CCString容许CCString实例变量转换为另外类型的变量。less

    /** convert to int value */

    int intValue() const;

    /** convert to unsigned int value */

    unsigned int uintValue() const;

    /** convert to float value */

    float floatValue() const;

    /** convert to double value */

    double doubleValue() const;

    /** convert to bool value */

    bool boolValue() const;    

经常使用的宏定义

    #define CCStringMake(str) CCString::create(str)

    #define ccs CCStringMake

使用这些宏能够很是方便的构建一个自动释放的CCString对象。假如你想要新建不少的CCString对象并把他们增长到CCArray中。函数

使用下面的代码就能够实现了,而且这些代码看起来至关简洁。 ui

    CCArray *stringArray = CCArray::create(

        ccs("Hello"),

        ccs("Variable"),

        ccs("Size"),

        ccs("!"),

        NULL);
相关文章
相关标签/搜索