比较 3 个数组。测验应用程序。如何先显示错误答案?
我有三个数组:
NSArray *Q = [1+1, 2+2, 3+3, 4+4, 5+5];
NSArray *A = [2, 4, 6, 8, 10];
NSArray *UserAnswers = [2, 4, 10, 17, 26];
在表格视图中,我想显示结果,但错误答案(在本例中为 10,17,26)必须在正确答案(在表格视图顶部)之前显示。
我将其发布在这里是因为我需要一个干净而智能的解决方案来解决这个问题。有什么想法吗?
这是肮脏的方式,有人知道更简单/更干净的方式吗?
NSMutableArray *wrongs = [NSMutableArray new];
NSMutableArray *rights = [NSMutableArray new];
NSMutableArray *dataSource = [NSMutableArray new];
for (int x = 0; x<questions.count; x++) {
NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:[questions objectAtIndex:x],@"q",[answers objectAtIndex:x],@"a",[userAnswers objectAtIndex:x], "ua", nil];
if ([[userAnswers objectAtIndex:x]isEqual:[answers objectAtIndex:x]])
[rights addObject:temp];
else
[wrongs addObject:temp];
}
[dataSource addObjectsFromArray:wrongs];
[dataSource addObjectsFromArray:rights];
[wrongs release];
[rights release];
I have three arrays:
NSArray *Q = [1+1, 2+2, 3+3, 4+4, 5+5];
NSArray *A = [2, 4, 6, 8, 10];
NSArray *UserAnswers = [2, 4, 10, 17, 26];
In a tableview I want to show the results but the wrong answers(in this case 10,17,26) must be shown before the correct answers(At top of the tableView).
I post it here because I need a clean and smart solution to solve this. Any ideas?
this is the dirty way, Anyone know an easier/cleaner way?
NSMutableArray *wrongs = [NSMutableArray new];
NSMutableArray *rights = [NSMutableArray new];
NSMutableArray *dataSource = [NSMutableArray new];
for (int x = 0; x<questions.count; x++) {
NSDictionary *temp = [[NSDictionary alloc]initWithObjectsAndKeys:[questions objectAtIndex:x],@"q",[answers objectAtIndex:x],@"a",[userAnswers objectAtIndex:x], "ua", nil];
if ([[userAnswers objectAtIndex:x]isEqual:[answers objectAtIndex:x]])
[rights addObject:temp];
else
[wrongs addObject:temp];
}
[dataSource addObjectsFromArray:wrongs];
[dataSource addObjectsFromArray:rights];
[wrongs release];
[rights release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自评论
你的方式看起来相当干净;也许在用户回答时将答案添加到正确的数组中?
From comments
Your way looks reasonably clean; perhaps add answers to the correct arrays as the user answers?