最近在面试和上级笔试中,遇到了这样问题很多关于@property的问题,下面,说一下在@protocol中如何实现@property吧!
1.首先在@protocol,我们先问一下自己,为何要这样做?我觉得,目的希望遵守该协议的对象能实现该属性,在遵守该协议的对象中,自己实现set,get方法:
#import <Foundation/Foundation.h>
@protocol personDelegate <NSObject>
@property (nonatomic, copy) NSString *name;
@end
@interface Person : NSObject
@end
#import <Foundation/Foundation.h>
#import "Person.h"
@interface Student : NSObject <personDelegate>
{
NSString *_name;
}
@end
#import "Student.h"
@implementation Student
- (void)setName:(NSString *)name
{
_name = name;
}
- (NSString *)name
{
return _name;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
Student *stu = [[Student alloc]init];
stu.name = @"asd";
NSLog(@"%@", stu.name);
}