重写 setter 方法并获取信息
我有一个设置方法(setMinimumNumberOfSides),我想在使用合成后覆盖它。 在其中,我对实例变量施加了约束,以确保 int 在特定范围内。
稍后在自定义 init 方法中,我设置另一个实例变量 (numberOfSides),但我需要确保在范围内正确设置了minimumNumberOfSides 和maximumNumberOfSides。 我尝试将 setter 上的返回值更改为 BOOL,这样如果成功/失败,我可以传回 YES 或 NO,但这创建了一个冲突的方法,我猜是因为我正在使用合成并覆盖 setter。
如何轻松获取信息以检查设置器是否被调用并成功返回?
-(void)setNumberOfSides:(int)sides
{
if ((sides < maximumNumberOfSides) && (sides > minimumNumberOfSides))
{
numberOfSides = sides;
}
else
NSLog (@"Invalid number of sides: %d is outside the constraints allowed", sides);
}
-(void)setMinimumNumberOfSides:(int)minimum
{
if (minimum > 2)
minimumNumberOfSides = minimum;
}
-(void)setMaximumNumberOfSides:(int)maximum
{
if (maximum <= 12)
maximumNumberOfSides = maximum;
}
-(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max
{
if (self = [super init])
{
self.minimumNumberOfSides = min;
self.maximumNumberOfSides = max;
self.numberOfSides = sides;
}
return self;
}
I have a setter method (setMinimumNumberOfSides) that I want to override after using synthesize. In it, I'm putting in a constraint on the instance variable to make sure the int is within certain bounds.
Later in a custom init method, I'm setting another instance variable (numberOfSides), but I need to make sure minimumNumberOfSides and maximumNumberOfSides was set properly within bounds. I tried changing the return value on the setter to a BOOL, so I could pass back a YES or NO if it succeeded/failed, but that created a conflicting method, I'm guessing because I'm using synthesize and overriding the setter.
How can I get the info out easily to check to see if the setter was called and returned successfully?
-(void)setNumberOfSides:(int)sides
{
if ((sides < maximumNumberOfSides) && (sides > minimumNumberOfSides))
{
numberOfSides = sides;
}
else
NSLog (@"Invalid number of sides: %d is outside the constraints allowed", sides);
}
-(void)setMinimumNumberOfSides:(int)minimum
{
if (minimum > 2)
minimumNumberOfSides = minimum;
}
-(void)setMaximumNumberOfSides:(int)maximum
{
if (maximum <= 12)
maximumNumberOfSides = maximum;
}
-(id)initWithNumberOfSides:(int)sides minimumNumberOfSides:(int)min maximumNumberOfSides:(int)max
{
if (self = [super init])
{
self.minimumNumberOfSides = min;
self.maximumNumberOfSides = max;
self.numberOfSides = sides;
}
return self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您计划实现 getter 和 setter,则不必综合 numberOfSides。 如果没有
@synthesize numberOfSides
,您可以选择返回 BOOL。 您需要在界面中相应地声明 getter/setter。顺便说一句,另一种方法是使用合成的 getter/setter 并添加一个单独的方法
-(BOOL)isNumberOfSidesValid
来执行此检查。You don't have to synthesize numberOfSides if you're planning on implementing the getter and setter. Without
@synthesize numberOfSides
you can return a BOOL if you choose. You'll need to declare the getter/setter in your interface accordingly.BTW, another approach would be to use the synthesized getter/setter and add a separate method
-(BOOL)isNumberOfSidesValid
which performs this check.在这种情况下,您最好使用对
assert()
的简单调用,或抛出异常。选择将取决于您如何看待此类的使用。 如果它将成为库的一部分,并且您希望其他开发人员经常为
minimumNumberOfSides
或maximumNumberOfSides
提供不正确的值,那么您可能应该抛出适当的异常。不过,有一句警告。 如果您希望应用程序的用户经常提供不正确的值,那么例外是一个坏主意。 Objective-C 中的异常处理是一项昂贵的操作。 如果这些检查是为了用户的利益而进行的,那么您应该执行输入验证,并以更友好的方式向用户报告错误。
编辑:这里是一些快速示例代码:
编辑:这里是另一个 SO 问题,详细介绍了 Objective-C 中的异常处理。
In a situation like this, you may be better off using a simple call to
assert()
, or throwing an exception.The choice will depend on how you see this class being used. If it will be part of a library, and you expect other developers to frequently supply incorrect values for
minimumNumberOfSides
ormaximumNumberOfSides
, you should probably throw a proper exception.A word of warning, though. If you expect the users of your application to frequently supply incorrect values, then an exception is a bad idea. Exception handling in Objective-C is an expensive operation. If these checks are in place for the sake of the user, you should perform input validation, and report errors to the user in a much more friendly manner.
edit: Here is some quick sample code:
edit: Here is another SO question that goes into detail about exception handling in Objective-C.