Objective-C setter/getter 命名约定让我抓狂?

发布于 2024-10-01 00:51:07 字数 1024 浏览 2 评论 0原文

我几个小时以来一直在试图理解一些事情,我想听听你的观点。

我的类属性之一有 setter/getter (我注意到我必须在 setter 名称前面添加“set”,否则编译器会说没有 setter):

@property (nonatomic, retain, readwrite, setter=setTopString:, getter=TopString) NSString* m_topString;

当我像这样调用 setter 时,编译器很高兴:

[secureKeyboardController setTopString:@"This action requires that your enter your authentication code."];

但是当我尝试使用“点”约定时,编译器拒绝了我:

                secureKeyboardController.topString = @"This action requires that your enter your authentication code.";

真正奇怪的是点命名约定与此属性配合良好:

@property (nonatomic, readwrite, getter=PINMaxLength, setter=setPINMaxLength:) NSInteger m_PINMaxLength;

在这种情况下我可以这样做:

[secureKeyboardController setPINMaxLength:10];enter code here

或者

secureKeyboardController.PINMaxLength = 10;

在这两种情况下,编译器很高兴。

我真的很想今晚睡得比我现在感觉的不那么愚蠢。因此,任何解释将不胜感激。

问候, 苹果92

I have been trying to understand something for several hours and I would like to get your point of view.

I have setter/getter on one of my class properties (I noticed that I MUST add "set" in front of the setter name else the compiler says that there is no setter):

@property (nonatomic, retain, readwrite, setter=setTopString:, getter=TopString) NSString* m_topString;

When I call the setter like this, the compiler is happy:

[secureKeyboardController setTopString:@"This action requires that your enter your authentication code."];

But when I try to use the "dot" convention, then I am rejected by the compiler:

                secureKeyboardController.topString = @"This action requires that your enter your authentication code.";

What is really weird is that the dot naming convention works fine with this property:

@property (nonatomic, readwrite, getter=PINMaxLength, setter=setPINMaxLength:) NSInteger m_PINMaxLength;

In this case i can do:

[secureKeyboardController setPINMaxLength:10];enter code here

or

secureKeyboardController.PINMaxLength = 10;

In both cases, the compiler is happy.

I really would like to fall asleep tonigh less stupid than I currently feel now. Thus any explanation would be greatly appreciated.

Regards,
Apple92

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你在我安 2024-10-08 00:51:07

您所做的就是声明属性,就像声明实例变量一样。您不应该使用点语法的@property 声明中的gettersetter 属性中的名称;据我所知,它现在起作用并不是设计使然。

属性应该是您使用点语法的属性。由于某种原因 - 我预计您不熟悉 Cocoa 编码约定 - 您将属性命名为 m_topStringm_PINMaxLength。这意味着您应该将它们用作 someObject.m_topStringsomeObject.m_PINMaxLength

如果您想将这些名称用于您决定用于属性后备存储的实例变量,则应在 @synthesize 指令中进行声明。

这就是你的类应该的样子,更符合常规的 Cocoa 和 Objective-C 编码约定:

@interface SomeClass : NSObject {
@private
    NSString *m_topString;
}
@property (nonatomic, readwrite, copy) NSString *topString;
- (id)initWithTopString:(NSString *)initialTopString;
@end

@implementation SomeClass
@synthesize topString = m_topString;
    // this says to use the instance variable m_topString
    // for the property topString's storage

- (id)initWithTopString:(NSString *)initialTopString {
    if ((self = [super init])) {
        m_topString = [initialTopString copy];
            // use the ivar directly in -init, not the property
    }
    return self;
}

- (void)dealloc {
    [m_topString release];
        // use the ivar directly in -dealloc, not the property

    [super dealloc];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"SomeClass (%@)", self.topString];
        // elsewhere in your class, use the property
        // this will call through its getter and setter methods
}
@end

What you're doing is declaring properties as if you were declaring instance variables. You should not be using the names in the getter and setter attributes on the @property declaration with dot syntax; that it happens to be working now is not - so far as I know - by design.

The property should be what you use with dot syntax. For some reason - unfamiliarity with Cocoa coding conventions, I expect - you named your properties m_topString and m_PINMaxLength. That means you should use them as someObject.m_topString and someObject.m_PINMaxLength.

If you want to use those names for the instance variables that you've decided to use for the properties' backing storage, you should declare that in the @synthesize directive instead.

This is how your class should look, to be more in line with regular Cocoa and Objective-C coding conventions:

@interface SomeClass : NSObject {
@private
    NSString *m_topString;
}
@property (nonatomic, readwrite, copy) NSString *topString;
- (id)initWithTopString:(NSString *)initialTopString;
@end

@implementation SomeClass
@synthesize topString = m_topString;
    // this says to use the instance variable m_topString
    // for the property topString's storage

- (id)initWithTopString:(NSString *)initialTopString {
    if ((self = [super init])) {
        m_topString = [initialTopString copy];
            // use the ivar directly in -init, not the property
    }
    return self;
}

- (void)dealloc {
    [m_topString release];
        // use the ivar directly in -dealloc, not the property

    [super dealloc];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"SomeClass (%@)", self.topString];
        // elsewhere in your class, use the property
        // this will call through its getter and setter methods
}
@end
你与昨日 2024-10-08 00:51:07

你试图对抗编译器,编译器会反击。

您试图使用 setter setTopString 和 getter TopString 声明一个名为 m_topString 的属性,这显然是愚蠢的。您正在编写 Objective-C 代码,而不是 C++。你的代码将成为维护的噩梦(除非下一个维护者明智地将你的代码更改为 Objective-C 约定)。

帮自己一个忙,开始编写 Objective-C 代码。只需调用属性 topString,不要为 setter 和 getter 选择您自己的名称,不要为实例变量选择您自己的名称,一切都会正常工作。

You are trying to fight the compiler, and the compiler fights back.

You are trying to declare a property named m_topString with setter setTopString and getter TopString, and that is plainly stupid. You are writing Objective-C code, not C++. Your code will be a maintenance nightmare (unless the next maintainer is just sensible and changes your code to Objective-C conventions).

Do yourself a favour, start writing Objective-C code. Just call the property topString, don't pick your own names for the setter and getter, don't pick your own names for the instance variable, and everything works just fine.

梦中楼上月下 2024-10-08 00:51:07

将 TopString 中的 T 大写,即 secureKeyboardController.TopString
我 90% 确信这会解决你的问题。

Capitalize the T in TopString, i.e. secureKeyboardController.TopString
I'm 90% sure that will fix your problem.

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