Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何检查NSDictionary中的值和整数值是否相同?_Ios_Xcode_Nsarray_Plist_Nsdictionary - Fatal编程技术网

Ios 如何检查NSDictionary中的值和整数值是否相同?

Ios 如何检查NSDictionary中的值和整数值是否相同?,ios,xcode,nsarray,plist,nsdictionary,Ios,Xcode,Nsarray,Plist,Nsdictionary,我有一本字典,上面的值如图所示,我有图书id的整数值。如何检查bookid是否匹配 这是我的检查代码 NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:metaDataPath]; NSMutableArray *notes=[plistdictionary objectForKey:@"usernotes"]; NSLog(@"notes value %@",not

我有一本字典,上面的值如图所示,我有图书id的整数值。如何检查bookid是否匹配

这是我的检查代码

NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:metaDataPath];
NSMutableArray *notes=[plistdictionary objectForKey:@"usernotes"];
NSLog(@"notes value %@",notes);
NSArray *CollectingBookid=[[NSArray alloc]init];
CollectingBookid=[notes valueForKey:@"bookid"];
NSArray *CollectingPages=[[NSArray alloc]init];
CollectingPages=[notes valueForKey:@"pagenumber"];
NSArray *CollectingNotes=[[NSArray alloc]init];
CollectingNotes=[notes valueForKey:@"notes"];
NSLog(@"collection of book id%@",CollectingBookid);
NSString *bookid=@"95";
NSString *page=@"1";
int c=[CollectingBookid count];
for(int i=0;i<c;i++)
{
    NSString *singleBookids=[CollectingBookid objectAtIndex:i];
    NSString *singlePage=[CollectingPages objectAtIndex:i];
    if([singleBookids isEqualToString:bookid])
        {
            if([singlePage isEqualToString:page])
            {
                NSMutableArray *CompleteUserNotes=[[NSMutableArray alloc]init];
                CompleteUserNotes=[CollectingNotes objectAtIndex:i];
                NSLog(@"Selected Notes%@",CompleteUserNotes);
            }
        }
}
NSMutableDictionary*plistdictionary=[[NSMutableDictionary alloc]initWithContentsOfFile:metaDataPath];
NSMUTABLEARRY*notes=[plistdictionary objectForKey:@“usernotes”];
NSLog(@“注释值%@”,注释);
NSArray*CollectingBookid=[[NSArray alloc]init];
CollectingBookid=[notes valueForKey:@“bookid]”;
NSArray*CollectingPages=[[NSArray alloc]init];
CollectionPages=[notes valueForKey:@“页码”];
NSArray*CollectingNotes=[[NSArray alloc]init];
CollectionNotes=[notes valueForKey:@“notes”];
NSLog(@“图书编号集合%@”,图书编号集合);
NSString*bookid=@“95”;
NSString*页=@“1”;
int c=[CollectingBookid计数];

for(inti=0;i创建一个谓词并通过筛选数组查找userNote

/*As your plist has bookId as string its taken as string. 
But if you have bookId as integer before checking 
convert it to string for predicate to work*/
NSInteger bookId = 92;
NSString *bookIdString = [NSString stringWithFormat:@"%d",bookId];

NSInteger pageNumber = 12;
NSString *pageNumberString = [NSString stringWithFormat:@"%d",pageNumber];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bookid == %@ AND pagenumber == %@",bookIdString,pageNumberString];

//filteredUserNotes will have all notes matching bookId
NSArray *filteredUserNotes =  [notes filteredArrayUsingPredicate:predicate];

//Assuming you only has one entry for bookId and you want a single one
NSDictionary *userNote = [filteredUserNotes lastObject];

您可以使用它来比较整数值

[singleBookids intValue]==[bookid intValue];

您可以使用字符串格式进行正确的比较

如果要与integer进行比较,请使用以下代码:

int bookid=95;
int page=1;
int c=[CollectingBookid count];
for(int i=0;i<c;i++)
{
    NSString *singleBookids=[CollectingBookid objectAtIndex:i];
    NSString *singlePage=[CollectingPages objectAtIndex:i];
    if([singleBookids intValue]==bookid)
    { 
        if([singlePage intValue]==page)
        {
            NSMutableArray *CompleteUserNotes=[[NSMutableArray alloc]init];
            CompleteUserNotes=[CollectingNotes objectAtIndex:i];
            NSLog(@"Selected Notes%@",CompleteUserNotes);
        }
    }
}
int bookid=95;
int page=1;
int c=[CollectingBookid计数];

对于(int i=0;i我将在记事本中写入此内容。对于可能出现的错误,我深表歉意

 NSMutableDictionary *plistdictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:metaDataPath];
NSMutableArray *notes=[plistdictionary objectForKey:@"usernotes"];
int bookid = 95;
int page = 1;
for (NSDictionary *collectingObject in notes)
{
    int collectingBookid = [[collectingObject objectForKey:@"bookid"] intValue];
    int collectingPageid = [[collectingObject objectForKey:@"pagenumber"] intValue];
    if (collectingBookid == bookid && collectingPageid == page)
    {
        NSString *collectingNote = [collectingObject objectForKey:@"notes"];
        NSLog(@"Note is: %@", collectingNote);
    }
}

为什么bookid在pList中是字符串?您想比较整数和整数,对吗?或者您想比较字符串和字符串?我如何在字典中添加整数值?这些与您的图片不匹配:NSArray*CollectingBookid=[[NSArray alloc]init];CollectingBookid=[notes valueForKey:@“bookid”];NSArray*CollectingPages=[[NSArray alloc]init];CollectingPages=[notes valueForKey:@“pagenumber”];NSArray*CollectingNotes=[NSArray alloc]init];CollectingNotes=[notes valueForKey:@“notes”];如何将整数转换为字符串?int number=100500;NSString*stringFromInteger=[NSString stringWithFormat:@“%d”,number];@Naveen您想找到一个与bookId rite匹配的userNote项。在这段代码中,您创建了一个谓词,该谓词将查找bookId与谓词中提供的相同的匹配注释。在筛选结果为数组时,您可以获取该结果并执行您想要的操作,但我需要页码和bookId匹配!!@Naveen我已经编辑了代码。Lei don’我不知道是否有问题。如果我给出的是我的输入,而不是你硬编码的书号和页码,你是说我不需要检查所有的条件,而是我可以使用谓词吗?@Naveen是的,你是对的。我硬编码的值只是为了告诉你。