[Cocos2D-x官方文档]:解读CCString类

本文详细介绍了CCString类的功能和使用方法,包括如何通过不同方式创建CCString对象,以及如何将CCString转换为其他类型的数据。此外,还提供了一系列实用的宏定义和示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CCString

简介

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

常用的方法

创建

   
  1. /**使用std::string创建了一个字符串, 你也可以传递一个c字符串指针,因为std::string的构造函数可以访问c字符串指针
  2.  
  3. * @返回的 CCString 指针是一个自动释放对象,
  4.  
  5. *也就意味着你不需要调用release操作,除非你retain了.
  6.  
  7. */
  8.  
  9. static CCString* create(const std::string& str);
  10.  
  11. /**使用格式化方式来创建一个字符串,这个方法和c语言里面的‘sprintf’类似,默认缓存大小是(1024*100)bytes
  12.  
  13. *假如你想要改变这个缓存大小,你可以去CCString.cpp中,更改kMaxStringLen 这个宏定义。
  14.  
  15.  
  16. * @返回的 CCString 指针是一个自动释放对象,
  17.  
  18. *也就意味着你不需要调用release操作,除非你retain了.
  19.  
  20. */
  21.  
  22. static CCString* createWithFormat(const char* format, …);
  23.  
  24. /** 使用二进制数据来创建字符串
  25.  
  26. * @返回的 CCString 指针是一个自动释放对象,
  27.  
  28. *也就意味着你不需要调用release操作,除非你retain了.
  29.  
  30. */
  31.  
  32. static CCString* createWithData(const unsigned char* pData, unsigned long nLen);
  33.  
  34. /**使用一个文件来创建一个字符串,
  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);

转换

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

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

常用的宏定义

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

使用这些宏可以非常方便的构建一个自动释放的CCString对象。假如你想要新建很多的CCString对象并把他们增加到CCArray中。
使用下面的代码就可以实现了,并且这些代码看起来相当简洁。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值