NSMutableDictionary 的奇怪行为

发布于 2024-12-15 14:25:42 字数 1479 浏览 0 评论 0原文

看看

- (NSMutableDictionary *)getUsersFromServer
{
//here we're getting list of users from the server
NSMutableDictionary * users = [[[NSMutableDictionary alloc] init] autorelease];

userresults = [[NSMutableArray alloc] init];    

//all this will be replaced with users taken from the server. it's needed just for testing
for (int i = 0;i < 19;i++)
{

    int wins    = i ;         float f_wins = (float)wins;
    int losses  = i * 2 ;     float f_losses = (float)losses;
    int withdr  = i * 3 ;     float f_withdr = (float)withdr;
    float win_per = f_wins / ((f_wins + f_losses + f_withdr) / 100.0);

 [userresults setArray:[NSMutableArray arrayWithObjects:[NSNumber numberWithInt:wins],
                                                                                [NSNumber numberWithInt:losses],
                                                                                [NSNumber numberWithInt:withdr],
                                                                                [NSNumber numberWithFloat:win_per],
                                                                                 nil]]; 
[users setObject:userresults forKey:[NSString stringWithFormat:@"user number %i",i]];

}

[userresults release];
return users;
}

每次循环迭代中的这段代码,我用数字填充数组并将其设置为 NSMutableDictionary 中的值。作为每个数组的键,提供格式化字符串,该字符串根据迭代计数器的数量是唯一的。所以...问题是 - 字典总是填充不同键的相同数组。字典中有 19 个数组,而且它们都是最后的!!!!这是上一次迭代的结果。而且每个人都有不同的钥匙!怎么可能发生???这是怎么回事???

Take a look at this piece of code

- (NSMutableDictionary *)getUsersFromServer
{
//here we're getting list of users from the server
NSMutableDictionary * users = [[[NSMutableDictionary alloc] init] autorelease];

userresults = [[NSMutableArray alloc] init];    

//all this will be replaced with users taken from the server. it's needed just for testing
for (int i = 0;i < 19;i++)
{

    int wins    = i ;         float f_wins = (float)wins;
    int losses  = i * 2 ;     float f_losses = (float)losses;
    int withdr  = i * 3 ;     float f_withdr = (float)withdr;
    float win_per = f_wins / ((f_wins + f_losses + f_withdr) / 100.0);

 [userresults setArray:[NSMutableArray arrayWithObjects:[NSNumber numberWithInt:wins],
                                                                                [NSNumber numberWithInt:losses],
                                                                                [NSNumber numberWithInt:withdr],
                                                                                [NSNumber numberWithFloat:win_per],
                                                                                 nil]]; 
[users setObject:userresults forKey:[NSString stringWithFormat:@"user number %i",i]];

}

[userresults release];
return users;
}

in each loop iteration i fill array with numbers and set it as value into NSMutableDictionary. As a key for each array serves formatted string which is unique by number of iteration counter. So... the problem is - the dictionary is always filled with SAME arrays for DIFFERENT keys. There are 19 arrays in the dictionary and ALL THEY ARE LAST ONES!!!! That is from the last iteration. And each one has different key!!! How could it happen??? What's going on???

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

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

发布评论

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

评论(2

南风几经秋 2024-12-22 14:25:42

userresults 指向同一个对象,并且您正在使用 setArray: 方法修改同一个数组。在每次循环迭代中创建一个新数组:

NSArray *userData = [NSArray arrayWithObjects:[NSNumber numberWithInt:wins],
                                              [NSNumber numberWithInt:losses],
                                              [NSNumber numberWithInt:withdr],
                                              [NSNumber numberWithFloat:win_per],
                                              nil];
[users setObject:userData forKey:[NSString stringWithFormat:@"user number %i",i]];

userresults points to the same object, and you're modifying that same array with the setArray: method. Create a new array in each loop iteration instead:

NSArray *userData = [NSArray arrayWithObjects:[NSNumber numberWithInt:wins],
                                              [NSNumber numberWithInt:losses],
                                              [NSNumber numberWithInt:withdr],
                                              [NSNumber numberWithFloat:win_per],
                                              nil];
[users setObject:userData forKey:[NSString stringWithFormat:@"user number %i",i]];
萌化 2024-12-22 14:25:42

userresults 不必要地在循环外部声明。您可以在此处放弃可变数组,而只需在每次迭代时创建一个新的 NSArray。您的问题源于重用循环外部声明的数组并每次都放入新值。试试这个:

[users setObject: [NSArray arrayWithObjects:[NSNumber numberWithInt:wins], [NSNumber numberWithInt:losses], [NSNumber numberWithInt:withdr], [NSNumber numberWithFloat:win_per], nil];
[users setObject:userData forKey:[NSString stringWithFormat:@"user number %i",i]]
forKey:[NSString stringWithFormat:@"user number %i",i]];;

userresults is declared outside the loop unnecessarily. You can forego mutable arrays here and just create a new NSArray with each iteration. Your problem stems from reusing the array declared outside the loop and putting new values in each time. Try this:

[users setObject: [NSArray arrayWithObjects:[NSNumber numberWithInt:wins], [NSNumber numberWithInt:losses], [NSNumber numberWithInt:withdr], [NSNumber numberWithFloat:win_per], nil];
[users setObject:userData forKey:[NSString stringWithFormat:@"user number %i",i]]
forKey:[NSString stringWithFormat:@"user number %i",i]];;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文