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);
相關文章
相關標籤/搜索