一个案例供你参考
#import <Foundation/Foundation.h> #pragma mark 学生类的声明 typedef struct{ int year; int month; int day; }Mydate; @interface Student:NSObject { @public; NSString *_name; //学生生日,结构体变量 Mydate _brithday; } @end @implementation Student
@end #pragma mark 学生类的实现 int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu=[Student new]; stu->_name=@"张三丰"; //第一种方法 stu->_brithday=(Mydate){2016,5,28};//{2016,5,28}被强制转化成结构类型 NSLog(@"%d.%d.%d",stu->_brithday.year,stu->_brithday.month,stu->_brithday.day); //第二种方法是 //在栈区划分一个存储空间,定义一个结构体变量 Mydate d1={1992,12,13}; stu->_brithday=d1;//将结构体整体赋值给_brithday NSLog(@"%d.%d.%d",stu->_brithday.year,stu->_brithday.month,stu->_brithday.day); //第三种方法:逐个赋值 stu->_brithday.year=1993; stu->_brithday.month=3; stu->_brithday.day=23; NSLog(@"%d.%d.%d",stu->_brithday.year,stu->_brithday.month,stu->_brithday.day); } return 0; }
|