-2

我有一个字典,其值如图所示,我有书籍 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);
            }
        }
}
4

4 回答 4

2

创建谓词并通过过滤数组找到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];
于 2013-03-25T10:14:11.283 回答
2

我在记事本里写这个。抱歉可能出现的错误。

 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);
    }
}
于 2013-03-25T10:25:54.737 回答
2

您使用字符串格式进行正确比较。

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

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);
        }
    }
}
于 2013-03-25T10:24:09.503 回答
0

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

[singleBookids intValue]==[bookid intValue];
于 2013-03-25T10:15:32.517 回答