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

时间:2013-03-25 10:02:03

标签: ios xcode nsarray plist nsdictionary

我有一个字典,其值如照片所示,我有书籍ID的整数值。我怎样才能检查bookid是否匹配?enter image description here

这是我的支票代码

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 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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);
        }
    }
}

答案 2 :(得分: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);
    }
}

答案 3 :(得分:0)

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

[singleBookids intValue]==[bookid intValue];