oc 能否通过连续点语法给结构体属性的某个成员赋值

2025-02-23 22:31:20
推荐回答(1个)
回答1:

头文件中

Objective C code?

#import

// 定义一个结构体保存生日
typedef struct {
int year;
int month;
int day;
}Date;

// 定义枚举保存性别
typedef enum{KsexMan,KsexWomen} Sex;

//// 定义数组保存学生三科成绩
//int array[3] = {0};

@interface Student : NSObject
{
NSString *_name;
Date *_birthday;
int _age;
float _height;
float _weight;
Sex *_sex;
int _markC;
int _markOc;
int _markIos;
}
@property NSString *name;
@property Date *birthday;
@property int age,markC,markOc,markIos;
@property float height,weight;
@property Sex *sex;

.m文件中

Objective C code?

#import
#import "Student.h"

int main(int argc, const char * argv[]) {
@autoreleasepool {

// 创建一个学生
Student *s = [[Student alloc] init];
Student *s1 = [[Student alloc] init];

// 设置属性
s.name = @"张三";
s.birthday->year = 1970;
s.birthday->month = 5;
s.birthday->day = 21;