gpt4 book ai didi

ios - NSDictionary 键比较失败

转载 作者:行者123 更新时间:2023-11-28 19:08:42 26 4
gpt4 key购买 nike

我正在处理一个非常基本的问题,我有两个字典键需要比较,但是当我这样做时它没有给我正确的答案,不知何故我无法创建逻辑。

所以我在字典的 plist 数组和来自服务器的 json 数组中有这个如果 plist 中存在事件,我想比较它们是否做某事,如果不做另一件事;

问题:
根据我的逻辑,如果 plist 中的两个事件等于来自服务器的其他两个事件,如果逻辑应该为每个来自服务器的字典打印,那么在这种情况下总共打印 2 次,但在我的代码中打印 4 次。基本上每次都是满足条件。

NS日志:

evreka find the id
evreka find the id
evreka find the id
evreka find the id

来源:在 plist 中;

createList (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)

这来自服务器

responseobj (
{
"end_date" = "2013-07-24";
ends = "13:07:00";
"event_id" = 173;
"event_name" = Static;
location = Asdad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-24";
starts = "13:07:00";
"user_id" = 13;
},
{
"end_date" = "2013-07-25";
ends = "13:08:00";
"event_id" = 174;
"event_name" = "Event Delete";
location = Asdsad;
"root_folder" = "dadapof@gmail.com";
"start_date" = "2013-07-25";
starts = "13:08:00";
"user_id" = 13;
}
)

代码:

比较代码;

if ([[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
createList= [NSMutableArray arrayWithContentsOfFile:dataPath];
NSLog(@"createList %@",createList);
NSLog(@"responseobj %@",responseObj);


//compare plist and response object
for (int i=0; i<[responseObj count]; i++) {
NSDictionary *dataDict = [responseObj objectAtIndex:i];

NSString *event_name =[dataDict objectForKey:@"event_name"];
//NSString *event_id =[dataDict objectForKey:@"event_id"];

BOOL eventExistInPlist=NO;


for (int j=0; j<[createList count]; j++) {
NSDictionary *createDict = [createList objectAtIndex:i];

//NSString *create_id =[createDict objectForKey:@"event_id"];
NSNumber *create_id =[createDict objectForKey:@"event_id"];

if ([[dataDict objectForKey:@"event_id"] isEqual:[createDict objectForKey:@"event_id"]]) {
// if (create_id==event_id) {
NSLog(@"evreka find the id");

我尝试过的:我尝试将它们作为字符串(isEqualtoString)、数字(==)和 isEqual: 方法进行比较,它们都失败了。

我做错了什么?

最佳答案

 NSDictionary *createDict = [createList objectAtIndex:i];
^----- HERE

您使用了错误的索引,对于内部循环,它应该是 j 而不是 i

关于ios - NSDictionary 键比较失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17841371/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com