帮助 NSDictionary allKeysForObject: 没有成功
我已经尝试让它工作很长一段时间了,但有些选项已经用完了。我正在尝试使用“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,如果你有一个玩家列表作为键,他们的分数作为对象,要打印该列表,你可以这样做:
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:
键可能是数字而不是字符串。使用
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 storeint
s. You have to use NSNumber. So use[playerResultInTheGame allKeysForObject:[NSNumber numberWithInt:1]]
.