打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
[Cocos2D

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);
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C语言面试题
多线程问题cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (
启明星辰2011校园招C笔试题(一)
string类相关
loadrunner 脚本开发
LightOPC.h中的定义说明之二
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服