OC中结构体作为对象属性
在OC中结构体有时候也作为对象的属性
类的定义
#import <Foundation/Foundation.h> typedef struct{ int year; int month; int day; } Date; @interface Student : NSObject { @public NSString *_name; Date _birthday; } -(void) say; @end
类方法
#import "Student.h" @implementation Student -(void) say{ NSLog(@"name=%@;year=%d,month=%d,day=%d",_name,_birthday.year,_birthday.month,_birthday.day); } @end
对象的实例化及方法的实现
#import <Foundation/Foundation.h> #import "Student.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *stu=[[Student alloc] init]; stu->_name=@"喵星人"; //方法1 // stu->_birthday=(Date){1995,2,15}; //方法2 // stu->_birthday.year=1995; // stu->_birthday.month=2; // stu->_birthday.day=25; //方法3 Date date={1995,2,25}; stu->_birthday=date; [stu say]; } return 0; }