如何公开声明 NSDictionary 的键?

发布于 2024-12-27 18:48:56 字数 178 浏览 1 评论 0原文

我定义了一个字典,我想将其传递给各种其他对象。当他们收到这本字典时,他们需要知道它是如何定义的,以便他们可以解压它以获取它的值。我一直在公共标头中使用#define 来定义键。这样,我就可以在编辑时进行编译器检查,以确保我没有使用无用的密钥。但是是否有其他更标准的方法来声明已定义字典的接口,以便其他对象在尝试使用未定义的键时会出现编译错误?

I have defined a dictionary that I would like to pass around to various other objects. When they receive this dictionary, they need to know how it is defined so they can unpack it to get its values. I've been using #define's in my public header to define the keys. That way, I get edit-time compiler checking to ensure I don't use a bum key. But is there some other, more standard way to declare the interface to a defined dictionary so that other objects will get compile errors if they try to use undefined keys?

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

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

发布评论

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

评论(3

芯好空 2025-01-03 18:48:56

#define更好的是使用常量NSString。在您的标头中:(

extern NSString * const MyDictionaryFribbleKey;

这是一个指向 NSString 的常量指针。)在您的实现中:

NSString * const MyDictonaryFribbleKey = @"theFribble";

这就是框架导出常量字符串的方式。这不会阻止使用无效密钥,没有什么能真正做到这一点(它是 C,你可以绕过任何东西),但它提高了标准。

Better than #define is to use constant NSStrings. In your header:

extern NSString * const MyDictionaryFribbleKey;

(That's a constant pointer to an NSString.) And in your implementation:

NSString * const MyDictonaryFribbleKey = @"theFribble";

This is how the frameworks export constant strings. This won't stop the use of invalid keys, nothing will really do that (it's C, you can bypass anything), but it raises the bar higher.

哑剧 2025-01-03 18:48:56

为什么不使用字典而不使用对象呢?

下面的示例表明实际上并没有涉及太多额外的工作。另外,您还可以获得实际使用对象的优势。

对象设置

@interface MyClass : NSObject

@property (nonatomic, strong) NSString *myString;
@property (nonatomic, assign) CGFloat myFloat;

@end

@implementation MyClass

@synthesize myString = _myString;
@synthesize myFloat  = _myFloat;

@end

对象使用

MyClass *myClass = [[MyClass alloc] init];

// Set 
myClass.myString = @"aa";
myClass.myFloat  = 12.0f;

// Get
NSString *myString = myClass.myString
CGFloat   myFloat  = myClass.myFloat;

NSDictionary 设置

.h

extern NSString * const MYClassKeyMyString;
extern NSString * const MYClassKeyMyFloat;

.m

NSString * const MYClassKeyMyString = @"MYClassKeyMyString";
NSString * const MYClassKeyMyFloat  = @"MYClassKeyMyFloat";

NSDictionary 使用

NSDictionary *myDict = [[NSMutableDictionary alloc] init];

// Set
[myDict setObject:@"aa" forKey:MYClassKeyMyString];
[myDict setObject:[NSNumber numberWithFloat:12.0f] forKey:MYClassKeyMyFloat];

// Get
NSString *myString = [myDict objectForKey:MYClassKeyMyString];
CGFloat   myFloat  = [[myDict objectForKey:MYClassKeyMyFloat] floatValue];

Instead of using a dictionary why not use an object?

The example below shows that there really isn't much extra work involved. Plus you gain the advantages of actually using objects.

Object Set up

@interface MyClass : NSObject

@property (nonatomic, strong) NSString *myString;
@property (nonatomic, assign) CGFloat myFloat;

@end

@implementation MyClass

@synthesize myString = _myString;
@synthesize myFloat  = _myFloat;

@end

Object use

MyClass *myClass = [[MyClass alloc] init];

// Set 
myClass.myString = @"aa";
myClass.myFloat  = 12.0f;

// Get
NSString *myString = myClass.myString
CGFloat   myFloat  = myClass.myFloat;

NSDictionary Set up

.h

extern NSString * const MYClassKeyMyString;
extern NSString * const MYClassKeyMyFloat;

.m

NSString * const MYClassKeyMyString = @"MYClassKeyMyString";
NSString * const MYClassKeyMyFloat  = @"MYClassKeyMyFloat";

NSDictionary use

NSDictionary *myDict = [[NSMutableDictionary alloc] init];

// Set
[myDict setObject:@"aa" forKey:MYClassKeyMyString];
[myDict setObject:[NSNumber numberWithFloat:12.0f] forKey:MYClassKeyMyFloat];

// Get
NSString *myString = [myDict objectForKey:MYClassKeyMyString];
CGFloat   myFloat  = [[myDict objectForKey:MYClassKeyMyFloat] floatValue];
坏尐絯 2025-01-03 18:48:56

这是一个非常标准的方式。您还可以使用 -enumerateKeys 方法枚举字典的键。

That's a pretty standard way. You can also enumerate the keys of a dictionary using the -enumerateKeys method.

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