一.字符串的创建
// 直接创建字符串
NSString *str1 = @"jack";
// 调用stringWithFormat方法创建字符串
NSString *str2 = [NSString stringWithFormat:@"age = %i, height = %f", 25, 1.75];
二.计算字符串的长度
1>C语言字符串计算长度
char name[] = "jack";
size_t size1 = sizeof(name); // 计算出来的长度包含了 \0
size_t size2 = strlen(name); // 计算出来的长度不包含 \0
2>OC字符串计算长度
NSString *str1 = @"jack";
NSUInteger len = [str1 length];
三.结构体作为对象的属性
/*
合理的设计一个”学生“类
学生有* 姓名* 生日两个属性和说出自己姓名生日方法
要求利用设计的学生类创建学生对象,并说出自己的姓名和年龄
描述学生类
事物名称: 学生(Student)
属性:姓名(name), 生日(birthday)
行为:说出字节姓名和生日(say)
*/
#import <Foundation/Foundation.h>
// 日期结构体
typedef struct {
int year;
int month;
int day;
} Date;
@interface Student : NSObject
{
@public // 使成员变量为公有
NSString *_name; // 姓名
Date _birthday; // 生日
}
// 说出姓名和生日方法
- (void)say;
@end
@implementation Student
// 说出姓名和生日方法
- (void)say
{
NSLog(@"姓名 = %@, 生日 = %i年 - %i月 - %i日", _name, _birthday.year, _birthday.month, _birthday.day);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 创建一个人
Student *stu = [Student new];
// 姓名
stu->_name = @"jack";
// 生日
// 方法一:强制转换
stu->_birthday = (Date){2015, 8, 27};
// 方法二:定义一个新的结构体,给d赋值,将d赋值给birthday
Date d = {2015, 8, 20};
stu->_birthday = d;
// 方法三:分别赋值
stu->_birthday.year = 2015;
stu->_birthday.month = 7;
stu->_birthday.day = 19;
// 调用say方法
[stu say];
}
return 0;
}
四.对象作为返回值类型
#import <Foundation/Foundation.h>
#pragma mark - 弹夹
@interface Clip : NSObject
{
@public
int _bullet; // 子弹
}
// 上子弹的方法
- (void)addBullet;
@end
@implementation Clip
- (void)addBullet
{
// 上子弹
_bullet = 10;
}
@end
#pragma mark - 枪
@interface Gun : NSObject
{
Clip *clip; // 弹夹
}
// 射击
// 注意: 在企业级开发中千万不要随意修改一个方法
- (void)shoot:(Clip *)c;
@end
@implementation Gun
- (void)shoot:(Clip *)c
{
// 判断有没有弹夹
if (c != nil) { // nil == null == 没有值
// 判断有没有子弹
if (c->_bullet > 0) {
c->_bullet -= 1;
NSLog(@"打了一枪 %i", c->_bullet);
}else
{
NSLog(@"没有子弹了");
}
}else
{
NSLog(@"没有弹夹, 请换弹夹");
}
}
@end
#pragma mark - 士兵
@interface Soldier : NSObject
{
@public
NSString *_name;
double _height;
double _weight;
}
// 开火
- (void)fire:(Gun *)gun;
// 开火, 给士兵一把枪和一个弹夹
- (void)fire:(Gun *)gun clip:(Clip *)clip;
@end
@implementation Soldier
//- (void)fire:(Gun *)gun
//{
// [gun shoot];
//}
- (void)fire:(Gun *)gun clip:(Clip *)clip
{
// 判断是否有枪和子弹
if (gun !=nil &&
clip != nil) {
[gun shoot:clip];
}
}
@end
#pragma mark - 商店
@interface Shop : NSObject
// 买枪
+ (Gun *)buyGun:(int)money;
// 买弹夹
+ (Clip *)buyClip:(int)money;
@end
@implementation Shop
+ (Gun *)buyGun:(int)money
{
// 1.创建一把枪
Gun *gun = [Gun new]; // 通过new创建出来的对象存储在堆中, 堆中的数据不会自动释放
// 2.返回一把枪
return gun;
}
+ (Clip *)buyClip:(int)money
{
Clip *clip = [Clip new];
[clip addBullet];
return clip;
}
@end
#pragma mark - 程序入口
int main(int argc, const char * argv[]) {
// 1.创建士兵
Soldier *sp =[Soldier new];
sp->_name = @"屎太浓";
sp->_height = 1.88;
sp->_weight = 100.0;
// 2.创建一把枪
// Gun *gp = [Gun new];
// Shop *shop = [Shop new];
// 2.购买手枪
Gun *gp = [Shop buyGun:888];
// 3.创建弹夹
// Clip *clip = [Clip new];
// [clip addBullet];
// 3.购买弹夹
Clip *clip = [Shop buyClip:100];
// 4.让士兵开枪
[sp fire:gp clip:clip];
return 0;
}