CCString

Introduction

CCString is inherited from the CCObject class. CCObject exist primarily as an automatic memory management object. CCString provided a series of interface such as create, convert etc..

Useful Methods

Creation :

 1
 2    /** create a string with std string, you can also pass a c string pointer because the default constructor of std::string can access a c string pointer. 
 3
 4     * @return A CCString pointer which is an autorelease object pointer,
 5
 6     * it means that you needn't do a release operation unless you retain it.
 7
 8     */
 9
10    static CCString* create(const std::string& str);
11
12    /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,
13
14     * if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp file.
15
16     * @return A CCString pointer which is an autorelease object pointer,
17
18     * it means that you needn't do a release operation unless you retain it.
19
20     */ 
21
22    static CCString* createWithFormat(const char* format, );
23
24    /** create a string with binary data 
25
26     * @return A CCString pointer which is an autorelease object pointer,
27
28     * it means that you needn't do a release operation unless you retain it.
29
30     */
31
32    static CCString* createWithData(const unsigned char* pData, unsigned long nLen);
33
34    /** create a string with a file, 
35
36     * @return A CCString pointer which is an autorelease object pointer,
37
38     * it means that you needn't do a release operation unless you retain it.
39
40     */
41
42    static CCString* createWithContentsOfFile(const char* pszFileName);
43

Conversion

The CCString is also allows a CCString variable converts to a variable of another type:

 1
 2    /** convert to int value */
 3
 4    int intValue() const;
 5
 6    /** convert to unsigned int value */
 7
 8    unsigned int uintValue() const;
 9
10    /** convert to float value */
11
12    float floatValue() const;
13
14    /** convert to double value */
15
16    double doubleValue() const;
17
18    /** convert to bool value */
19
20    bool boolValue() const;
21

Useful Macros

1
2#define CCStringMake(str) CCString::create(str)
3
4#define ccs CCStringMake
5

These macros can construct an autorelease CCString object more easily, e.g. If we want to new a lot of CCString object, and add them into a CCArray,the following codes will achieve that and make your code more simple.

 1
 2        CCArray *stringArray = CCArray::create(
 3
 4            ccs("Hello"),
 5
 6            ccs("Variable"),
 7
 8            ccs("Size"),
 9
10            ccs("!"),
11
12            NULL);


come from: http://www.cocos2d-x.org/projects/cocos2d-x/wiki/CCString
posted @ 2013-04-22 14:30  Jzong  阅读(321)  评论(0编辑  收藏  举报