Objective-C setter/getter 命名约定让我抓狂?
我几个小时以来一直在试图理解一些事情,我想听听你的观点。
我的类属性之一有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您所做的就是声明属性,就像声明实例变量一样。您不应该使用点语法的
@property
声明中的getter
和setter
属性中的名称;据我所知,它现在起作用并不是设计使然。属性应该是您使用点语法的属性。由于某种原因 - 我预计您不熟悉 Cocoa 编码约定 - 您将属性命名为
m_topString
和m_PINMaxLength
。这意味着您应该将它们用作someObject.m_topString
和someObject.m_PINMaxLength
。如果您想将这些名称用于您决定用于属性后备存储的实例变量,则应在
@synthesize
指令中进行声明。这就是你的类应该的样子,更符合常规的 Cocoa 和 Objective-C 编码约定:
What you're doing is declaring properties as if you were declaring instance variables. You should not be using the names in the
getter
andsetter
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
andm_PINMaxLength
. That means you should use them assomeObject.m_topString
andsomeObject.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:
你试图对抗编译器,编译器会反击。
您试图使用 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.
将 TopString 中的 T 大写,即
secureKeyboardController.TopString
我 90% 确信这会解决你的问题。
Capitalize the T in TopString, i.e.
secureKeyboardController.TopString
I'm 90% sure that will fix your problem.