1.2 NSString字符串

一.字符串的创建

  
  
  1. // 直接创建字符串
  2. NSString *str1 = @"jack";
  3. // 调用stringWithFormat方法创建字符串
  4. NSString *str2 = [NSString stringWithFormat:@"age = %i, height = %f", 25, 1.75];

二.计算字符串的长度

1>C语言字符串计算长度

  
  
  1. char name[] = "jack";
  2. size_t size1 = sizeof(name); // 计算出来的长度包含了 \0
  3. size_t size2 = strlen(name);  // 计算出来的长度不包含 \0
2>OC字符串计算长度
   
   
  1. NSString *str1 = @"jack";
  2. NSUInteger len = [str1 length];

三.结构体作为对象的属性

   
   
  1. /*
  2. 合理的设计一个”学生“类
  3. 学生有* 姓名* 生日两个属性和说出自己姓名生日方法
  4. 要求利用设计的学生类创建学生对象,并说出自己的姓名和年龄
  5. 描述学生类
  6. 事物名称: 学生(Student)
  7. 属性:姓名(name), 生日(birthday)
  8. 行为:说出字节姓名和生日(say)
  9. */
  10. #import <Foundation/Foundation.h>
  11. // 日期结构体
  12. typedef struct {
  13.    int year;
  14.    int month;
  15.    int day;
  16. } Date;
  17. @interface Student : NSObject
  18. {
  19.    @public // 使成员变量为公有
  20.    NSString *_name; // 姓名
  21.    Date _birthday; // 生日
  22. }
  23. // 说出姓名和生日方法
  24. - (void)say;
  25. @end
  26. @implementation Student
  27. // 说出姓名和生日方法
  28. - (void)say
  29. {
  30.    NSLog(@"姓名 = %@, 生日 = %i年 - %i月 - %i日", _name, _birthday.year, _birthday.month, _birthday.day);
  31. }
  32. @end
  33. int main(int argc, const char * argv[]) {
  34.    @autoreleasepool {
  35.        
  36.        // 创建一个人
  37.        Student *stu = [Student new];
  38.        
  39.        // 姓名
  40.        stu->_name = @"jack";
  41.        
  42.         //  生日
  43.        // 方法一:强制转换
  44.        stu->_birthday = (Date){2015, 8, 27};
  45.        
  46.        // 方法二:定义一个新的结构体,给d赋值,将d赋值给birthday
  47.        Date d = {2015, 8, 20};
  48.        stu->_birthday = d;
  49.        
  50.        // 方法三:分别赋值
  51.        stu->_birthday.year = 2015;
  52.        stu->_birthday.month = 7;
  53.        stu->_birthday.day = 19;
  54.        
  55.        // 调用say方法
  56.        [stu say];
  57.        
  58.    }
  59.    return 0;
  60. }

四.对象作为返回值类型

   
   
  1. #import <Foundation/Foundation.h>
  2. #pragma mark - 弹夹
  3. @interface Clip : NSObject
  4. {
  5. @public
  6.    int _bullet; // 子弹
  7. }
  8. // 上子弹的方法
  9. - (void)addBullet;
  10. @end
  11. @implementation Clip
  12. - (void)addBullet
  13. {
  14.    // 上子弹
  15.    _bullet = 10;
  16. }
  17. @end
  18. #pragma mark -
  19. @interface Gun : NSObject
  20. {
  21.    Clip *clip; // 弹夹
  22. }
  23. // 射击
  24. // 注意: 在企业级开发中千万不要随意修改一个方法
  25. - (void)shoot:(Clip *)c;
  26. @end
  27. @implementation Gun
  28. - (void)shoot:(Clip *)c
  29. {
  30.    // 判断有没有弹夹
  31.    if (c != nil) { // nil == null == 没有值
  32.        // 判断有没有子弹
  33.        if (c->_bullet > 0) {
  34.            c->_bullet -= 1;
  35.            NSLog(@"打了一枪 %i", c->_bullet);
  36.        }else
  37.        {
  38.            NSLog(@"没有子弹了");
  39.        }
  40.    }else
  41.    {
  42.        NSLog(@"没有弹夹, 请换弹夹");
  43.    }
  44. }
  45. @end
  46. #pragma mark - 士兵
  47. @interface Soldier : NSObject
  48. {
  49. @public
  50.    NSString *_name;
  51.    double _height;
  52.    double _weight;
  53. }
  54. // 开火
  55. - (void)fire:(Gun *)gun;
  56. // 开火, 给士兵一把枪和一个弹夹
  57. - (void)fire:(Gun *)gun clip:(Clip *)clip;
  58. @end
  59. @implementation Soldier
  60. //- (void)fire:(Gun *)gun
  61. //{
  62. //    [gun shoot];
  63. //}
  64. - (void)fire:(Gun *)gun clip:(Clip *)clip
  65. {
  66.    // 判断是否有枪和子弹
  67.    if (gun !=nil &&
  68.        clip != nil) {
  69.        
  70.        [gun shoot:clip];
  71.    }
  72. }
  73. @end
  74. #pragma mark - 商店
  75. @interface Shop : NSObject
  76. // 买枪
  77. + (Gun *)buyGun:(int)money;
  78. // 买弹夹
  79. + (Clip *)buyClip:(int)money;
  80. @end
  81. @implementation Shop
  82. + (Gun *)buyGun:(int)money
  83. {
  84.    // 1.创建一把枪
  85.    Gun *gun = [Gun new]; // 通过new创建出来的对象存储在堆中, 堆中的数据不会自动释放
  86.    // 2.返回一把枪
  87.    return gun;
  88. }
  89. + (Clip *)buyClip:(int)money
  90. {
  91.    Clip *clip = [Clip new];
  92.    [clip addBullet];
  93.    return clip;
  94. }
  95. @end
  96. #pragma mark - 程序入口
  97. int main(int argc, const char * argv[]) {
  98.    
  99.    // 1.创建士兵
  100.    Soldier *sp =[Soldier new];
  101.    sp->_name = @"屎太浓";
  102.    sp->_height = 1.88;
  103.    sp->_weight = 100.0;
  104.    
  105.    // 2.创建一把枪
  106. //    Gun *gp = [Gun new];
  107.    
  108. //    Shop *shop = [Shop new];
  109.    // 2.购买手枪
  110.    Gun *gp = [Shop buyGun:888];
  111.    
  112.    // 3.创建弹夹
  113. //    Clip *clip = [Clip new];
  114. //    [clip addBullet];
  115.    
  116.    // 3.购买弹夹
  117.    Clip *clip = [Shop buyClip:100];
  118.    
  119.    // 4.让士兵开枪
  120.    [sp fire:gp clip:clip];
  121.    
  122.    return 0;
  123. }











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值