帮助 NSDictionary allKeysForObject: 没有成功

发布于 2024-10-12 14:45:51 字数 1374 浏览 4 评论 0原文

我已经尝试让它工作很长一段时间了,但有些选项已经用完了。我正在尝试使用“allKeysForObject”来选择某个对象的所有键。

这是尝试选择结果键的测试代码。字典中的对象应该是数字,但是当我想显示它时,我使用 %@ 来获取结果,这对我来说表明这不是数字。

我一直在尝试使用 int、NSString 选择键(如代码示例中所示)并使用 allObject 数组进行选择,但未能成功。由于我对此很陌生,我已经没有选择,必须寻求帮助。

NSDictionary *playerResultInTheGame = [readCurrentGameDataFunction finalResultForCurrentGame];


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

NSArray *allPlayers = [playerResultInTheGame allKeys];
NSArray *allObjects = [playerResultInTheGame allValues];

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

NSMutableArray *myObjectsArray = [[NSMutableArray alloc] init];

allObjects = [allObjects sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"allObjects: %@", allObjects);


NSArray *xxxxx = [playerResultInTheGame allKeysForObject:@"1"];
NSLog(@"xxxxx: %@", xxxxx);

我得到的结果是:

2011-01-17 20:50:34.554 XX[11203:207]playerResultInTheGame: {
巴恩斯皮拉雷 = 2;
Vuxenspelare = 1;
}
2011-01-17 20:50:34.554 XX[11203:207] 所有对象: (
2、
1
) 2011-01-17 20:50:34.555 XX[11203:207] 所有对象: (
1、
2
)
当前语言:自动;目前objective-c
2011-01-17 20:51:50.086 XX[11203:207] xxxxx: (
)
2011-01-17 20:52:24.523 XX[11203:207] 所有玩家: (
巴恩斯佩拉雷,
Vuxenspelare

I have been trying to get this to work for quite some time now and is somewhat running out of options. I am trying to use "allKeysForObject" to select all keys for a certain object.

This is test code to try to select the keys for a result. The objects in the dictionary is suppose to be numbers but when i want to display it i am using %@ to get the result, which for me indicate that this is not number.

I have been playing around to try to select the key with int, NSString (as in the code example) and used the allObject array to do the select but have not been able to succeed. As i am very new at this i am running out of option and have to reach out for help.

NSDictionary *playerResultInTheGame = [readCurrentGameDataFunction finalResultForCurrentGame];


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

NSArray *allPlayers = [playerResultInTheGame allKeys];
NSArray *allObjects = [playerResultInTheGame allValues];

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

NSMutableArray *myObjectsArray = [[NSMutableArray alloc] init];

allObjects = [allObjects sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"allObjects: %@", allObjects);


NSArray *xxxxx = [playerResultInTheGame allKeysForObject:@"1"];
NSLog(@"xxxxx: %@", xxxxx);

The result i get is:

2011-01-17 20:50:34.554 XX[11203:207] playerResultInTheGame: {
Barnspelare = 2;
Vuxenspelare = 1;
}
2011-01-17 20:50:34.554 XX[11203:207] allObjects: (
2,
1
)
2011-01-17 20:50:34.555 XX[11203:207] allObjects: (
1,
2
)
Current language: auto; currently objective-c
2011-01-17 20:51:50.086 XX[11203:207] xxxxx: (
)
2011-01-17 20:52:24.523 XX[11203:207] allPlayers: (
Barnspelare,
Vuxenspelare
)

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

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

发布评论

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

评论(2

帅冕 2024-10-19 14:45:51

好的,如果你有一个玩家列表作为键,他们的分数作为对象,要打印该列表,你可以这样做:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"player1", @"2", @"Player2", @"3", @"player3", nil];


    NSLog(@"%@", [dict objectForKey:@"player1"]);

    NSString *key;
    for (key in dict) {
        NSLog(@"Player: %@, Result: %@", key, [dict objectForKey:key]);
    }

    [pool drain];
    return 0;
}

Okay, so if you have a list of players, as the keys, and their scores as objects, to get that list printed at all, you might do this:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"player1", @"2", @"Player2", @"3", @"player3", nil];


    NSLog(@"%@", [dict objectForKey:@"player1"]);

    NSString *key;
    for (key in dict) {
        NSLog(@"Player: %@, Result: %@", key, [dict objectForKey:key]);
    }

    [pool drain];
    return 0;
}
土豪我们做朋友吧 2024-10-19 14:45:51

键可能是数字而不是字符串。使用 int 肯定是错误的,因为 Cocoa 集合无法存储 int。您必须使用 NSNumber。因此,请使用[playerResultInTheGame allKeysForObject:[NSNumber numberWithInt:1]]

The keys are probably numbers rather than strings. Using an int is definitely wrong, because Cocoa collections cannot store ints. You have to use NSNumber. So use [playerResultInTheGame allKeysForObject:[NSNumber numberWithInt:1]].

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