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];

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

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 // 射击
 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.创建一把Qiang
 85     Gun *gun = [Gun new]; // 通过new创建出来的对象存储在堆中, 堆中的数据不会自动释放
 86     // 2.返回一把Qiang
 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.创建ShiBing
100     Soldier *sp =[Soldier new];
101     sp->_name = @"屎太浓";
102     sp->_height = 1.88;
103     sp->_weight = 100.0;
104     
105     // 2.创建一把Qiang
106 //    Gun *gp = [Gun new];
107     
108 //    Shop *shop = [Shop new];
109     // 2.购买ShouQiang
110     Gun *gp = [Shop buyGun:888];
111     
112     // 3.创建Danjia
113 //    Clip *clip = [Clip new];
114 //    [clip addBullet];
115     
116     // 3.购买Danjia
117     Clip *clip = [Shop buyClip:100];
118     
119     // 4.让士兵开枪
120     [sp fire:gp clip:clip];
121     
122     return 0;
123 }

 

posted @ 2015-09-03 16:04  文刂Rn  阅读(185)  评论(0编辑  收藏  举报