Objective-C:自定义 BOOL 访问器(getter 和 setter)方法
我知道有人已经问过为 BOOL 变量编写 getter 和 setter。但是,如果我定义一个自定义 getter & setter 方法 setImmediate
& isImmediate
分别,我希望 passcode.immediate = NO
也能工作。
我没有任何实例变量,但也许我应该?我可以为 NSDate *lastUnlocked
添加一个。
以下是到目前为止的相关代码:
// PasscodeLock.h
extern NSString *const kPasscodeLastUnlocked;
@interface PasscodeLock : NSObject {
}
- (BOOL)isImmediate;
- (void)setImmediate:(BOOL)on;
- (NSDate *)lastUnlocked;
- (void)resetLastUnlocked;
- (void)setLastUnlocked:(NSDate *)lastUnlocked;
@end
// PasscodeLock.m
#import "PasscodeLock.h"
NSString *const kPasscodeLastUnlocked = @"kPasscodeLastUnlocked";
@implementation PasscodeLock
#pragma mark PasscodeLock
- (BOOL)isImmediate {
return self.lastUnlocked == nil;
}
- (void)setImmediate:(BOOL)on {
if (on) {
[self resetLastUnlocked];
} else {
self.lastUnlocked = nil;
}
}
- (NSDate *)lastUnlocked {
return [[NSUserDefaults standardUserDefaults] objectForKey:kPasscodeLastUnlocked];
}
- (void)resetLastUnlocked {
NSDate *now = [[NSDate alloc] init];
self.lastUnlocked = now;
[now release];
}
- (void)setLastUnlocked:(NSDate *)lastUnlocked {
[[NSUserDefaults standardUserDefaults] setObject:lastUnlocked forKey:kPasscodeLastUnlocked];
}
然后,在将 PasswordLock *passwordLock
作为实例变量的视图控制器中,我想做 passcode.immediate = NO
,但我得到错误“在‘PasscodeLock *’类型的对象上找不到属性‘立即’。”
如何让 passcode.immediate = NO
正常工作?
I know someone already asked about Writing getter and setter for BOOL variable. But, if I'm defining a custom getter & setter methods setImmediate
& isImmediate
, respectively, I'd like passcode.immediate = NO
to work too.
I do not have any instance variables, but maybe I should? I could add one for NSDate *lastUnlocked
.
Here's the relevant code so far:
// PasscodeLock.h
extern NSString *const kPasscodeLastUnlocked;
@interface PasscodeLock : NSObject {
}
- (BOOL)isImmediate;
- (void)setImmediate:(BOOL)on;
- (NSDate *)lastUnlocked;
- (void)resetLastUnlocked;
- (void)setLastUnlocked:(NSDate *)lastUnlocked;
@end
// PasscodeLock.m
#import "PasscodeLock.h"
NSString *const kPasscodeLastUnlocked = @"kPasscodeLastUnlocked";
@implementation PasscodeLock
#pragma mark PasscodeLock
- (BOOL)isImmediate {
return self.lastUnlocked == nil;
}
- (void)setImmediate:(BOOL)on {
if (on) {
[self resetLastUnlocked];
} else {
self.lastUnlocked = nil;
}
}
- (NSDate *)lastUnlocked {
return [[NSUserDefaults standardUserDefaults] objectForKey:kPasscodeLastUnlocked];
}
- (void)resetLastUnlocked {
NSDate *now = [[NSDate alloc] init];
self.lastUnlocked = now;
[now release];
}
- (void)setLastUnlocked:(NSDate *)lastUnlocked {
[[NSUserDefaults standardUserDefaults] setObject:lastUnlocked forKey:kPasscodeLastUnlocked];
}
Then, in a view controller that has PasswordLock *passwordLock
as an instance variable, I want to do passcode.immediate = NO
, but I get the error "Property 'immediate' not found on object of type 'PasscodeLock *'."
How do I get passcode.immediate = NO
to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要类似于
.h 文件中的内容,当然还需要 .m 文件中的
@synthesize
语句。这将创建属性并定义您的 getter 方法名称。You need something like
in your .h file and of course a
@synthesize
statement in your .m file. This creates the property AND defines your getter method name.在@interface中声明这样的属性:(
setter=
部分是可选的)Declare such property in the @interface:
(The
setter=
part is optional)我认为问题 id 是您的 getter 和 setter 名称不一致。默认情况下,如果您
的代码中有 假设 getter 和 setter 分别命名为
-immediate
和-setImmediate:
。您的 getter 命名不正确。解决这个问题的最佳方法是像 Mark 和 Kenny 所说的那样声明一个属性,但您也可以更改 getter 的名称。要点是,您不需要声明属性来使用点语法,但如果您要使用点语法,那么声明属性是声明 getter 和 setter 的推荐方法。
I think the issue ids that your getter and setter names are not consistent. By default, if you have
in your code, it is assumed that the getter and setter are named
-immediate
and-setImmediate:
respectively. Your getter is not named correctly. The best way around this is to declare a property as Mark and Kenny have already said but you could also change the name of your getter.The point is that you do not need declared properties to use dot syntax, but if you are going to use dot syntax then declared properties are the recommended way of declaring the getter and setter.