//struct 后面那个要不要都行
typedef struct {//typedef struct后面也可以加个名字
__unsafe_unretained NSString*name;
int age;
int sex;
char depment;
char height;
char weight;
}Person;
typedef enum {//typedef enum 后面也可以加名字
spring,
summer,
autumn,
winter
}Season;
@interface ViewController : UIViewController
@property(nonatomic,assign)Season season;
@property(nonatomic,assign)Person per;
/*结构体也是一种数据类型,和int,NSString一样,只不过结构体可以自己定义是什么数据类型,一般有三种方法定义结构体
struct 结构体名字 {
类型 名字;
类型 名字;
类型 名字;
...
};
*/
#pragma mark 方法一
-(void)firstStruct{
struct student{
char *namePinYin;
int age;
__unsafe_unretained NSString*name;//在结构体中定义字符串需这样
};
//此时student就代表一种数据类型,然后可以声明这种类型的变量 stu1
struct student stu1={"zqk",27,@"郑乾坤"};
struct student stu2;
stu2.namePinYin="zhangsan";
stu2.age=26;
stu2.name=@"张三";
NSLog(@"%s %i %@",stu1.namePinYin,stu1.age,stu1.name);
}
#pragma mark 方法二
-(void)secondStruct{
struct {
char *namePinYin;
int age;
__unsafe_unretained NSString*name;
}stu;
stu.namePinYin="lisi";
stu.age=25;
stu.name=@"李四";
}
#pragma mark 方法三
-(void)thirdStruct{
//typedef 是给已有数据类型起个别名;
typedef struct student {
char *namePinYin;
int age;
__unsafe_unretained NSString*name;
}defStuden;
struct student stu;
stu.namePinYin="wangwu";
defStuden stu2;
stu2.namePinYin="wang";
}
#pragma mark 枚举
-(void)EnumText{
_season=winter;
switch (_season) {
case spring:
NSLog(@"春");
break;
case summer:
NSLog(@"夏");
break;
case autumn:
NSLog(@"秋");
break;
case winter:
NSLog(@"冬");
break;
default:
break;
}
}
网友评论