如何在 Objective-C 中实现 Perl 哈希?

发布于 2024-11-03 11:09:01 字数 581 浏览 7 评论 0原文

我已经浏览了许多网站和教程以及 Apple 文档,但仍然没有找到解决方案:在我看来,NSArray、NSDictionary 及其可变对应项根本不像 Perl 哈希的简单功能。我当然希望我是错的。

我需要什么:动态键和值的可变结构(1 个键 - 1 个值,就这么简单)!我的意思是,我事先不知道密钥,我需要轻松检查密钥是否存在,如果存在则检索值或更新它,如果不存在则输入带有新值的新密钥。我需要的值是浮点数,而不是对象或数组。填充完结构后,我需要能够检索键,并最终通过检索到的键循环遍历值。

所有这些都可以在 Perl 中轻松完成,方法如下:

    my %expenses;

    if (exists $expenses{$key}) {
       $expenses{$key} += $amount;
    } else {
       $expenses{$key} = $amount;
    }

[...]

是否有人可以告诉我如何在不使用原始类型的情况下在 Objective-C 中实现类似的功能?

非常感谢您的帮助。 法布里齐奥

I have been looking through many sites and tutorials, and the Apple documentation, and still haven't found a solution: it seems to me that NSArray, NSDictionary and their mutable counterparts are not at all resembling the simple functionalities of a Perl hash. I hope I am wrong of course.

What I need: a mutable structure of dynamic keys and values (1 key - 1 value, as simple as that)! I mean, I don't know keys in advance and I need to easily check whether a key exists and if it exists retrieve a value or update it, if it does not exist enter the new key with the new value. And I need the values to be floats, not objects nor arrays. After I finished populating the structure I need to be able to retrieve the keys and finally looping through the values by the keys I retrieved.

All of this is easily accomplished in Perl with the following:

    my %expenses;

    if (exists $expenses{$key}) {
       $expenses{$key} += $amount;
    } else {
       $expenses{$key} = $amount;
    }

[...]

Is there someone who could tell me how to implement something similar in objective-c without using primitive types?

Thank you so much for any help.
Fabrizio

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

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

发布评论

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

评论(2

暮色兮凉城 2024-11-10 11:09:01

Cocoa 和 Core Foundation 集合类通常面向存储对象而不是原始值;像您这样的问题的通常解决方案是将浮点数包装在 NSNumber 对象中。不幸的是,获取和设置对象的语法比 Perl 的语法更冗长。

除此之外, NSMutableDictionary 应该完全按照您的要求执行。键可以是任何字符串,或任何其他可以复制的对象(即它符合 NSCopying 协议),您可以获取所有键的列表,并且只需尝试即可检查字典中是否“存在”键来获取对应的值。与您的示例相对应的代码可能如下所示:

// NB: there no autovivification in Objective-C. Be sure to initialize this somewhere
// before using it!
NSMutableDictionary *expenses;

if ([expenses objectForKey:key]) {
    // This *could* be done in one statement. But it would be very long, so I split
    // it in two for clarity.
    float currentValue = [[expenses objectForKey:key] floatValue];
    [expenses setObject:[NSNumber numberWithFloat:currentValue + amount] forKey:key];
} else {
    [expenses setObject:[NSNumber numberWithFloat:amount] forKey:key];
}

The Cocoa and Core Foundation collection classes are generally oriented towards storing objects rather than primitive values; the usual solution to a problem like yours is to wrap the floats in NSNumber objects. And unfortunately, the syntax for getting and setting the objects is more verbose than Perl's

Other than that, an NSMutableDictionary should do exactly what you want. The keys can be any string, or any other object that can be copied (i.e. it conforms to the NSCopying protocol), you can get a list of all keys, and you can check if a key "exists" in the dictionary by simply trying to fetch the corresponding value. The code corresponding to your example could look something like this:

// NB: there no autovivification in Objective-C. Be sure to initialize this somewhere
// before using it!
NSMutableDictionary *expenses;

if ([expenses objectForKey:key]) {
    // This *could* be done in one statement. But it would be very long, so I split
    // it in two for clarity.
    float currentValue = [[expenses objectForKey:key] floatValue];
    [expenses setObject:[NSNumber numberWithFloat:currentValue + amount] forKey:key];
} else {
    [expenses setObject:[NSNumber numberWithFloat:amount] forKey:key];
}
晚风撩人 2024-11-10 11:09:01

这有效:

NSString *key=@"fish";
NSMutableDictionary *expenses = [NSMutableDictionary dictionary];
float amount=22.0;

[expenses setObject:[NSNumber numberWithFloat:amount/2] forKey:key];

if ([expenses objectForKey:key]) {
    [expenses setObject:
         [NSNumber numberWithFloat:
             [[expenses objectForKey:key] floatValue] + amount] forKey:key];
} else {
    [expenses setObject:[NSNumber numberWithFloat:amount] forKey:key];
}

NSLog(@"expenses: %@",expenses);

This works:

NSString *key=@"fish";
NSMutableDictionary *expenses = [NSMutableDictionary dictionary];
float amount=22.0;

[expenses setObject:[NSNumber numberWithFloat:amount/2] forKey:key];

if ([expenses objectForKey:key]) {
    [expenses setObject:
         [NSNumber numberWithFloat:
             [[expenses objectForKey:key] floatValue] + amount] forKey:key];
} else {
    [expenses setObject:[NSNumber numberWithFloat:amount] forKey:key];
}

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