Objective-C:自定义 BOOL 访问器(getter 和 setter)方法

发布于 2024-11-04 16:14:33 字数 1645 浏览 7 评论 0原文

我知道有人已经问过为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

做个少女永远怀春 2024-11-11 16:14:34

您需要类似于

@property (nonatomic, getter=isImmediate) BOOL immediate;

.h 文件中的内容,当然还需要 .m 文件中的 @synthesize 语句。这将创建属性并定义您的 getter 方法名称。

You need something like

@property (nonatomic, getter=isImmediate) BOOL immediate;

in your .h file and of course a @synthesize statement in your .m file. This creates the property AND defines your getter method name.

回忆那么伤 2024-11-11 16:14:34

在@interface中声明这样的属性:(

@interface PasscodeLock : NSObject {

}
@property(dynamic, getter=isImmediate,
                   setter=setImmediate:) BOOL immediate;
// etc.
@end

setter=部分是可选的)

Declare such property in the @interface:

@interface PasscodeLock : NSObject {

}
@property(dynamic, getter=isImmediate,
                   setter=setImmediate:) BOOL immediate;
// etc.
@end

(The setter= part is optional)

要走干脆点 2024-11-11 16:14:34

我认为问题 id 是您的 getter 和 setter 名称不一致。默认情况下,如果您

foo.immediate

的代码中有 假设 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

foo.immediate

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文