Object-C 学习笔记(四 )@property 属性标志的使用

来源:互联网 发布:python json api接口 编辑:程序博客网 时间:2024/04/29 22:28

@property的使用方法:


声明property的语法为:@property (参数1,参数2) 类型 名字;


1、在头文件中:
@property int count;
等效于在头文件中声明2个方法:
- (int)count;
-(void)setCount:(int)newCount;
2、实现文件(.m)中
@synthesize count;
等效于在实现文件(.m)中实现2个方法。
- (int)count
{
return count;
}
-(void)setCount:(int)newCount
{
count = newCount;
}
以上等效的函数部分由编译器自动帮开发者填充完成,简化了编码输入工作量。


百度百科上讲的很好:http://baike.baidu.com/view/5028218.htm



@property (参数1,参数2) 类型 名字     中的参数的相关解释,请参考:http://blog.csdn.net/wudidalishi/article/details/8275525


自己写的例子:


Person。h



#import <Foundation/Foundation.h>@interface Person : NSObject@property(readwrite,assign) NSString *name;@property(readwrite,assign) int age;@end


Person。m


#import "Person.h"@implementation Person@synthesize name;@synthesize age;@end


main.h


#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]){    @autoreleasepool {        Person *person=[[Person alloc] init];                person.age =12;                person.name=@"杨过";                NSLog(@"姓名: %@, 年龄: %i ",person.name,person.age);                           }    return 0;}





原创粉丝点击