1

我有这个协议。

//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

我知道如何使用函数来做到这一点,但我可以使用属性来做到这一点吗?谢谢你。

4

2 回答 2

1

属性只是一对方法,一个getter和一个setter 。而已。它没有说明如何实现 getter 和 setter。根本没有理由假设存在支持的“价值”。

于 2013-10-05T02:12:03.417 回答
0

设置属性值,

-(void)setAnInteger:(int)anInteger
{
    self.anInteger = anInteger;
}

像这样打电话,

[self setAnInteger:100];
[self setAnInteger:200];
于 2013-10-04T11:19:45.073 回答