我有这个协议。如何在objective-c中设置协议属性的默认值?
//MyProtocolClass.h
@protocol MyProtocolDelegate <NSObject>
@optional
@property (nonatomic, assign) int anInteger;
@end
@interface MyProtocolClass:NSObject
@end
然后我使用这个类此协议:
//.h
@interface MyClass:NSObject <MyProtocolDelegate>
@end
//.m
@implementation MyClass
@synthesize anInteger;
-(void) aFunction
{
NSLog(@"%d",self.anInteger);
self.anInteger = 200;
NSLog(@"%d",self.anInteger);
}
@end
我想设置一个默认值100上的可变anInteger
使得它保持该值阉用户设置,或不。
的NSLogs应该输出:
100
200
我知道如何用一个函数来做到这一点,但我可以做到这一点使用属性? 谢谢。
你只能在类方法中设置anInteger = 100',协议不能实现方法 – pNre
所以对于这样的属性可以没有默认值(开箱即用)..对吗? – SudoPlz
是的,你是对的 – pNre