协议中的 Objective-C 常量
在我的 Objective-C 项目中,我有一个这样的协议:
@protocol MyProtocol
-(id) get:(NSString *) key;
-(void) set:(NSString *) key withValue:(id) value;
-(NSValue *) getSize;
-(void) setSize:(NSValue *) value;
-(NSValue *) getBounds;
-(void) setBounds:(NSValue *) value;
@end
OBJC_EXPORT const NSString *MYPROTOCOL_SIZE;
OBJC_EXPORT const NSString *MYPROTOCOL_BOUNDS;
基本上,这些特定方法(getSize
、getBounds
、setSize
、setBounds
) 被假定为应分别存储在 MYPROTOCOL_SIZE
和 MYPROTOCOL_BOUNDS
中的值。
但是,我找不到通过连接其他方法的结果来设置这些常量字符串的有效方法,因为当我尝试直接设置它们时,它会给出错误:initializer element is not Constant
。有没有一种方法可以保证对象始终被初始化。 (例如在类 load
方法中),而无需在程序运行时手动调用代码?
In my objective-c project, I have a protocol like this:
@protocol MyProtocol
-(id) get:(NSString *) key;
-(void) set:(NSString *) key withValue:(id) value;
-(NSValue *) getSize;
-(void) setSize:(NSValue *) value;
-(NSValue *) getBounds;
-(void) setBounds:(NSValue *) value;
@end
OBJC_EXPORT const NSString *MYPROTOCOL_SIZE;
OBJC_EXPORT const NSString *MYPROTOCOL_BOUNDS;
And basically, those specific methods (getSize
, getBounds
, setSize
, setBounds
) are supposed the value that is supposed to be stored in MYPROTOCOL_SIZE
and MYPROTOCOL_BOUNDS
, respectively.
However, I cannot find an effective way to set those constant strings, by concatenating the results of other methods, because it gives me the error: initializer element is not constant
when I try to set them directly. Is there a way I can guarantee that the objects will always be initialized. (e.g. in a classes load
method), without having to manually call code when my program runs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该了解您拥有的访问器的命名约定 - (Type);和 - (void)set:(Type)value;而在你的情况下,你做了: - (Type)get;和 - (void)set:(Type)value;
我建议您也将 @property 用于您的大小和边界访问器。
现在关于 NSString 变量声明中的“const”,它没有意义。 Const 适用于其左侧的类型,如果它位于行的开头,则它直接适用于其右侧的标记。所以你拥有的是一个“const NSString”,它没有意义,因为 NSString 已经是不可变的,并且向 const 对象发送变异消息不会发出任何警告或错误......
你真正想要的是“NSString *const “其中指出指向 NSString 的指针是常量,您只能在初始化时分配它,然后它就不会改变...
现在关于协议...您确定要在您的情况下使用协议吗?而不是一个将 2 NSString 作为只读访问器的抽象类?
Well first of all, you should learn the naming convention, for accessors you have - (Type); and - (void)set:(Type)value; whereas in your case you did: - (Type)get; and - (void)set:(Type)value;
I advise you to use @property for your size and bounds accessors too.
Now about the "const" in the NSString variable declaration, it doesn't make sense. Const applies to the type on its left and in case it is at the beginning of the line it applies to the token directly on its right. So what you have is a "const NSString" which doesn't make sense because NSString is already immutable, and sending mutating messages to a const object doesn't issue any warning or errors...
What you actually want is "NSString *const" which states that the pointer to your NSString is constant, you can only assign it at initialization and then it doesn't change...
Now about the protocol... Are you sure you want a protocol in your case ? And not an abstract class that would have your 2 NSString as readonly accessors ?