您的位置:首页 > 其它

OC中结构体作为对象属性

2016-01-04 20:24 253 查看
在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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: